Multilpe screen in wxpython - user-interface

all.
I'd like to be able to switch between multiple screens. Meaning, the first one is the main, then when with a button or an external switch is activated I can see the page #2, in that one I may have an other button to return to the first one, or going to #3, etc. Cause I have a main screen for a big RPM meter, but I may want to see instead all three meter on the same page, or view the raw data in an other page, or go to the set-up page or elsewhere in the future development. I'm using the full screen space for my graphic. Maybe something like "hide" or "show" a page with an event of some kind. I have a single class script for every pages so far, but unable to group them in a single one. Thanks for your help

I wrote about this concept several years ago here. I went ahead an reproduced the example from that article:
import wx
import wx.grid as gridlib
class PanelOne(wx.Panel):
""""""
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
txt = wx.TextCtrl(self)
class PanelTwo(wx.Panel):
""""""
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
grid = gridlib.Grid(self)
grid.CreateGrid(25,12)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(grid, 0, wx.EXPAND)
self.SetSizer(sizer)
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"Panel Switcher Tutorial")
self.panel_one = PanelOne(self)
self.panel_two = PanelTwo(self)
self.panel_two.Hide()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.panel_one, 1, wx.EXPAND)
self.sizer.Add(self.panel_two, 1, wx.EXPAND)
self.SetSizer(self.sizer)
menubar = wx.MenuBar()
fileMenu = wx.Menu()
switch_panels_menu_item = fileMenu.Append(wx.ID_ANY,
"Switch Panels",
"Some text")
self.Bind(wx.EVT_MENU, self.onSwitchPanels,
switch_panels_menu_item)
menubar.Append(fileMenu, '&File')
self.SetMenuBar(menubar)
def onSwitchPanels(self, event):
""""""
if self.panel_one.IsShown():
self.SetTitle("Panel Two Showing")
self.panel_one.Hide()
self.panel_two.Show()
else:
self.SetTitle("Panel One Showing")
self.panel_one.Show()
self.panel_two.Hide()
self.Layout()
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
The basic idea here is to Hide() one panel and Show() another. You might also want to look at the Notebook controls that wxPython provides as they have a similar functionality.

Related

wxPython gives different results on Windows and Ubuntu

I have developed a small wxPython program that gives radically different output in Windows and Ubuntu (I am more than happy to be told I've programmed it incorrectly and I would regard that as a result- provided we can get it to work)
The program displays four shapes the right hand side. Double clicking on the right hand images moves them to the left hand side and vice versa.
Issues on Windows (see screen shots taken after the same actions in Windows and Ubuntu): The buttons don't render correctly; when I click on the cross, cicle or square they move correctly to the left of the screen, but multiple images of the triangle remain; an image appears in the top left corner
None of these issues appear in Ubuntu
import wx
class ImageSizer(wx.Frame):
def __init__(self, parent, title):
super(ImageSizer, self).__init__(parent, title=title,
size=(250, 200))
self.ShapeTypes=['available','selected']
self.MainSizer=wx.GridBagSizer()
self.SetSizer(self.MainSizer)
cmdNew=wx.Button(self, label='New')
cmdNew.Bind(wx.EVT_BUTTON, self.onNewClick)
cmdCancel=wx.Button(self, label='Cancel')
cmdCancel.Bind(wx.EVT_BUTTON, self.CancelClick)
self.MainSizer.Add((500,0), pos=(0,0), span=(1,2)) #dummy to position Available
self.MainSizer.Add((0,200), pos=(1,0), span=(1,1)) #dummy to position Buttons
self.MainSizer.Add(cmdNew, pos=(2,2), flag=wx.LEFT|wx.TOP, border=10)
self.MainSizer.Add(cmdCancel, pos=(2,3), flag=wx.RIGHT|wx.BOTTOM|wx.TOP|wx.ALIGN_RIGHT, border=10)
self.SetBackgroundColour((246, 244, 242))
self.Initialise()
self.Center()
self.Fit()
self.Show()
def DisplayImages(self):
availableSizer=ShapeSizer(self, self.AvailableShapes, self.ShapeTypes.index('available'))
self.RefreshSizerCell(self.MainSizer, availableSizer, (1,2), (1,2))
selectedSizer=ShapeSizer(self, self.SelectedShapes, self.ShapeTypes.index('selected'))
self.RefreshSizerCell(self.MainSizer, selectedSizer, (1,1), (1,1))
def Initialise(self):
self.AvailableShapes=['square','circle','triangle','cross']
self.SelectedShapes=[]
self.DisplayImages()
def RefreshSizerCell(self, sizer, item, pos, span, flag=wx.ALL, border=10):
self.Freeze()
oldItem=sizer.FindItemAtPosition(pos)
if (oldItem !=None) and oldItem.IsWindow():
oldItem.GetWindow().Destroy()
sizer.Add(item, pos=pos, span=span, flag=flag, border=border)
self.Layout()
self.Thaw()
def GetShapeName(self, event):
imgCtrl=event.GetEventObject()
return imgCtrl.GetName()
def onAvailableShapeDClick(self, event):
shape=self.GetShapeName(event)
self.AvailableShapes.remove(shape)
self.SelectedShapes.append(shape)
self.DisplayImages()
def onSelectedShapeDClick(self, event):
shape=self.GetShapeName(event)
self.SelectedShapes.remove(shape)
self.AvailableShapes.append(shape)
self.DisplayImages()
def onNewClick(self, event):
self.Initialise()
def CancelClick(self, event):
self.Destroy()
class ShapeSizer(wx.Panel):
def __init__(self, frame, shapes, shapeType):
wx.Panel.__init__(self, frame, id=wx.ID_ANY)
if shapeType==frame.ShapeTypes.index('available'):
size=40
action=frame.onAvailableShapeDClick
elif shapeType==frame.ShapeTypes.index('selected'):
size=80
action=frame.onSelectedShapeDClick
shapeSizer=wx.GridBagSizer()
shapes.sort()
for ii in range(0, len(shapes)):
bitmap=wx.Bitmap(shapes[ii]+'.png',wx.BITMAP_TYPE_PNG)
bitmap=self.ScaleBitmap(bitmap, size, size)
img=wx.StaticBitmap(self, wx.ID_ANY, bitmap, name=shapes[ii])
img.Bind(wx.EVT_LEFT_DCLICK, action)
shapeSizer.Add(img, pos=(0,ii), flag=wx.RIGHT, border=10)
self.SetSizer(shapeSizer)
def ScaleBitmap(self, bitmap, width, height):
image = wx.ImageFromBitmap(bitmap)
image = image.Scale(width, height, wx.IMAGE_QUALITY_HIGH)
result = wx.BitmapFromImage(image)
return result
if __name__ == '__main__':
app = wx.App()
ImageSizer(None, title='Image Sizer')
app.MainLoop()
The problem here is the lines
self.Freeze()
and
self.Thaw()
in
RefreshSizerCell()
Don't seem to be of any use in Windows :(
The line
Self.Layout()
also seems to be redundant

Running Pyqt UIs in tabs using TabWidget

I want to run my app in different tabs. Rightnow, it is running in main window. I have done some search work on how to create tabs. I found this to be useful, but not sufficient to meet my requirements
Create TAB and create textboxes that take data in the TAB-page
I want to have a feature of adding a new tab (like new tab in chrome)
Below is my code sample. I described what i require in the comments.
from PyQt4 import Qt, QtCore, QtGui
import sys
class Map(QtGui.QMainWindow):
def __init__(self,parentQExampleScrollArea=None,parentQWidget = None):
super(Map,self).__init__()
self.initUI()
#Initialize the UI
def initUI(self):
#Initilizing Menus toolbars
#This must be maintained in all tabbed panes
filename = ""
#Filename is obtained through open file button in file menu
self.filename = filename
def paintEvent(self, e):
qp = QtGui.QPainter()
qp.begin(self)
self.drawPoints(qp,self.filename)
qp.end()
def drawPoints(self, qp,FILENAME=""):
#Read contents in file
#Get the necessary coordinates
#A separate class for storing the info of all the coordinates
#Run a for loop for all the coordinates in the list
#Actually, object is created here and image of that object is set
# as a square using the coordinates
qp.setBrush(QtGui.QColor(255, 0, 20, 200))
qp.drawRect(20,20,75,75)
qp.drawRect(100,20,75,75)
self.update()
#There are many functions to handle keyboard and mouse events
def main():
#How do I modify so that I can have multiple tabs
#And show images of the coordinates in files
#Basically I want to have the feature of opening many files
# and displaying them in UI
#Basically a feature to add a new tab
#like that of in eclipse netbeans sublime etc
app = QtGui.QApplication(sys.argv)
myQExampleScrollArea = Map()
myQExampleScrollArea.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Thanks in advance.. :)
It's simply to use method int QTabWidget.addTab (self, QWidget widget, QString) to create widget in tab. In each tab, I suggest use QtGui.QWidget more than QtGui.QMainWindow;
Example;
import sys
from PyQt4 import QtGui
class QCustomWidget (QtGui.QWidget):
# Your widget to implement
# Put your override method here
def paintEvent (self, eventQPaintEvent):
currentQPainter = QtGui.QPainter()
currentQPainter.begin(self)
currentQPainter.setBrush(QtGui.QColor(255, 0, 20, 200))
currentQPainter.drawRect(20, 20, 75, 75)
currentQPainter.drawRect(100, 20, 75, 75)
self.update()
currentQPainter.end()
class QCustomTabWidget (QtGui.QTabWidget):
def __init__ (self, parent = None):
super(QCustomTabWidget, self).__init__(parent)
self.addTab(QtGui.QPushButton('Test'), 'Tab 1')
self.addTab(QCustomWidget(), 'Tab 2')
myQApplication = QtGui.QApplication([])
myQCustomTabWidget = QCustomTabWidget()
myQCustomTabWidget.show()
sys.exit(myQApplication.exec_())
So handle with more tab, It's bad to create many line call int QTabWidget.addTab (self, QWidget widget, QString). Anyway, All widget has add in QTabWidget is can reference in ifself. So, your can control element in it by call QWidget QTabWidget.widget (self, int index).
Example to call widget in tab widget;
import os
import sys
from PyQt4 import QtCore, QtGui
class QCustomLabel (QtGui.QLabel):
def __init__(self, imagePath, parentQWidget = None):
super(QCustomLabel, self).__init__(parentQWidget)
self.setPixmap(QtGui.QPixmap(imagePath))
class QCustomWidget (QtGui.QWidget):
def __init__ (self, parentQWidget = None):
super(QCustomWidget, self).__init__(parentQWidget)
self.addQPustButton = QtGui.QPushButton('Open image')
self.addQPustButton.setMaximumWidth(120)
self.addQPustButton.released.connect(self.openImage)
self.workSpaceQTabWidget = QtGui.QTabWidget()
self.workSpaceQTabWidget.setTabsClosable(True)
self.workSpaceQTabWidget.tabCloseRequested.connect(self.closeImage)
allQVBoxLayout = QtGui.QVBoxLayout()
allQVBoxLayout.addWidget(self.addQPustButton)
allQVBoxLayout.addWidget(self.workSpaceQTabWidget)
self.setLayout(allQVBoxLayout)
def openImage (self):
path = QtGui.QFileDialog.getOpenFileName(self, 'Open image')
if not path.isEmpty():
self.workSpaceQTabWidget.addTab(QCustomLabel(path), QtCore.QString(os.path.basename(str(path))))
def closeImage (self, currentIndex):
currentQWidget = self.workSpaceQTabWidget.widget(currentIndex)
currentQWidget.deleteLater()
self.workSpaceQTabWidget.removeTab(currentIndex)
myQApplication = QtGui.QApplication([])
myQCustomWidget = QCustomWidget()
myQCustomWidget.show()
sys.exit(myQApplication.exec_())

CheckBox Event in wxPython not working

I'm sorry if this is so simple, but I'm trying to bind an event to a checkbox in a menubar with wxPython. For some reason, it won't work! I've tried a variety of different ways to get it to print a statement, but nothing happens when I check the box. Is it not binding correctly? This is just a simple app I wrote to demonstrate the problem...
import wx
class Frame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
panel = wx.Panel(self)
menuBar = wx.MenuBar()
menu = wx.Menu()
self.checkbox = menu.AppendCheckItem(-1, "Check me")
menuBar.Append(menu,'&check box')
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_CHECKBOX, self.onCheck, self.checkbox)
def onCheck(self, e):
print self.checkbox.IsChecked()
app = wx.App()
test = Frame(None, -1, "Test")
test.Show()
app.MainLoop()
I've figured it out. I needed to change the Bind event from wx.EVT_CHECKBOX to wx.EVT_MENU.

Making tooltip texts on wxpython Ribbon control

I'm trying to create tooltip texts for a ribbon control. I have set the help_strings but don't know how to display them. Even if it is just a tooltip text as wxpython displays for a button would be fine for me.
I have attached the code sample (a modified version of the RibbobDemo.py) on which I would like to have the help_strings on mouseover.
I appreciate code examples or pointing references.
import wx
import wx.lib.agw.ribbon as RB
class RibbonFrame(wx.Frame):
def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
wx.Frame.__init__(self, parent, id, title, pos, size, style)
self._ribbon = RB.RibbonBar(self, wx.ID_ANY)
home = RB.RibbonPage(self._ribbon, wx.ID_ANY, "Examples", wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN))
toolbar_panel = RB.RibbonPanel(home, wx.ID_ANY, "Toolbar", wx.NullBitmap, wx.DefaultPosition,
wx.DefaultSize, RB.RIBBON_PANEL_NO_AUTO_MINIMISE)
toolbar = RB.RibbonToolBar(toolbar_panel, wx.ID_ANY)
# this is just a simple tool
toolbar.AddTool(wx.ID_ANY, wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN), help_string=" the first tool")
toolbar.AddTool(wx.ID_ANY, wx.ArtProvider.GetBitmap(wx.ART_ERROR), help_string=" the second tool")
toolbar.AddTool(wx.ID_ANY, wx.ArtProvider.GetBitmap(wx.ART_INFORMATION), help_string=" the third tool")
toolbar.AddSeparator()
self._ribbon.Realize()
self._logwindow = wx.TextCtrl(self, wx.ID_ANY, "", wx.DefaultPosition, wx.DefaultSize,
wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_LEFT | wx.TE_BESTWRAP | wx.BORDER_NONE)
s = wx.BoxSizer(wx.VERTICAL)
s.Add(self._ribbon, 0, wx.EXPAND)
s.Add(self._logwindow, 1, wx.EXPAND)
self.SetSizer(s)
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = RibbonFrame(None, -1, "wxPython Ribbon Sample Application", size=(800, 600))
frame.CenterOnScreen()
frame.Show()
app.MainLoop()
I got wxPython3.0 Docs and Demos installed from http://www.wxpython.org/download.php. In that, under the Advanced Generic Widgets, I found the demo for Pure-Python RibbonBar which has a tooltip text display. It displays as a usual button tooltip. However, I decided to satisfy myself by that. I thank everyone involved in compiling the docs and demos. It will be very very useful!

Putting stuff in windows

How can I put stuff in the main window? I want to create a line edit in the main window(beneath the menu bar, maybe with some decription laber in front of it). How is this done? I used grid layout and this box layout, nothing works.
(sry for another trivial question, there are only few tutorials on pyside out there, and most of them only cover how to create single windows with buttons ect.)
import sys
from PySide import QtGui, QtCore, QtWebKit
class FirstClass(QtGui.QMainWindow, QtGui.QWidget):
def __init__(self):
super(FirstClass, self).__init__()
self.startingUI()
def startingUI(self):
self.setWindowTitle('Hauptfenster')
self.resize(800, 400)
self.statusBar()
#Menueinstellungen an sich
menue = self.menuBar()
#Actions des Menues:
#datei menue
menuleiste_datei = menue.addMenu('File')
datei_exit = QtGui.QAction('Exit', self)
datei_exit.setStatusTip('Close the programm')
menuleiste_datei.addAction(datei_exit)
datei_exit.triggered.connect(self.close)
#Einstellungen menue
menuleiste_configurations = menue.addMenu('Configurations')
configurations_settings = QtGui.QAction('Settings', self)
configurations_settings.setStatusTip('Configurations(Settings)')
menuleiste_configurations.addAction(configurations_settings)
configurations_settings.triggered.connect(self.newwindow)
self.lineedit = QtGui.QLineEdit()
self.layout = QtGui.QHBoxLayout()
self.layout.addWidget(self.lineedit)
self.setLayout(self.layout)
self.show()
def newwindow(self):
self.wid = QtGui.QWidget()
self.wid.resize(250, 150)
self.setWindowTitle('NewWindow')
self.wid.show()
def main():
app = QtGui.QApplication(sys.argv)
start = FirstClass()
sys.exit(app.exec_())
if __name__== '__main__':
main()
I do not believe creating a class with a multiple inheritance is recommended best practice. If an attribute is not found in FirstClass, then it searches left to right (QtGui.QMainWindow to QtGui.QWidget). From my perspective, this would turn into a nightmare to support and debug. My guess this is why the self.layout is not working properly.
I made separate classes for QtGui.QMainWindow and QtGui.QWidget. FirstWindowClass sets the central widget as FirstWidgetClass. FirstWidgetClass has your QLineEdit and I went ahead and inserted a label. I changed QHBoxLayout to QGridLayout to help you understand how it works.
Some tips from my learning experiences with Python and Pyside these past couple months:
Remember you can always look at PyQt examples and majority will work directly with PySide modules.
I recommend looking over http://srinikom.github.io/pyside-docs/index.html as a lot of the modules have simple examples.
For my personal project, a lot of the solutions to my Qt questions were in C++ so do not be afraid to convert it to python.
import sys
from PySide import QtGui, QtCore, QtWebKit
class FirstWindowClass(QtGui.QMainWindow):
def __init__(self):
super(FirstWindowClass, self).__init__()
self.setWindowTitle('Hauptfenster')
self.resize(800, 400)
self.statusBar()
# Set central widget that expands to fill your window
self.main_widget = FirstWidgetClass(self)
self.setCentralWidget(self.main_widget)
#Menueinstellungen an sich
menue = self.menuBar()
#Actions des Menues:
#datei menue
menuleiste_datei = menue.addMenu('File')
datei_exit = QtGui.QAction('Exit', self)
datei_exit.setStatusTip('Close the programm')
menuleiste_datei.addAction(datei_exit)
datei_exit.triggered.connect(self.close)
#Einstellungen menue
menuleiste_configurations = menue.addMenu('Configurations')
configurations_settings = QtGui.QAction('Settings', self)
configurations_settings.setStatusTip('Configurations(Settings)')
menuleiste_configurations.addAction(configurations_settings)
configurations_settings.triggered.connect(self.newwindow)
# Open the window
self.show()
def newwindow(self):
self.wid = QtGui.QWidget()
self.wid.resize(250, 150)
self.wid.setWindowTitle('NewWindow')
self.wid.show()
class FirstWidgetClass(QtGui.QWidget):
def __init__(self, parent=None):
super(FirstWidgetClass, self).__init__()
self.label_example = QtGui.QLabel('Enter Data:')
self.lineedit = QtGui.QLineEdit()
self.layout = QtGui.QGridLayout()
self.layout.addWidget(self.label_example, 0, 0)
self.layout.addWidget(self.lineedit, 0, 1)
self.setLayout(self.layout)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
start = FirstWindowClass()
sys.exit(app.exec_())
if __name__== '__main__':
main()

Resources