trying to set functionality of menu items in menu bar with wxpython - python-3.x

here's my code for setting up the menu items in menu-bar with their functionally. but I don't know why but its just not working, I mean when I set open's functionally it worked but when I'm setting others it didn't and nor it is taking the history menu and help menu. presently I have commented the history and help code but when I remove the commas it gives error. (I'm not too good with python I have just started so need some help, in getting where I'm wrong.)
import wx
class MyApp(wx.App):
def OnInit(self):
self.frame = MenuFrame(None, title="Menus and MenuBars")
self.SetTopWindow(self.frame)
self.frame.Show()
return True
ID_READ_ONLY = wx.NewId()
class MenuFrame(wx.Frame):
def __init__(self, *args, **kwargs):
super(MenuFrame, self).__init__(*args, **kwargs)
# Attributes
self.panel = wx.Panel(self)
self.txtctrl = wx.TextCtrl(self.panel,
style=wx.TE_MULTILINE)
# Layout
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.txtctrl, 1, wx.EXPAND)
self.panel.SetSizer(sizer)
self.CreateStatusBar() # For output display
# Setup the Menu
menub = wx.MenuBar()
# File Menu
filem = wx.Menu()
filem.Append(wx.ID_NEW, "New\tCtrl+N")
filem.Append(wx.ID_OPEN, "Open\tCtrl+O")
filem.Append(wx.ID_SAVE, "Save\tCtrl+S")
filem.Append(wx.ID_SAVEAS, "Save_As\tCtrl+Shift+S")
menub.Append(filem, "&File")
# Edit Menu
editm = wx.Menu()
editm.Append(wx.ID_UNDO, "Undo\tCtrl+Z")
editm.Append(wx.ID_REDO, "Redo\tCtrl+Shift+Z")
editm.Append(wx.ID_COPY, "Copy\tCtrl+C")
editm.Append(wx.ID_CUT, "Cut\tCtrl+X")
editm.Append(wx.ID_PASTE, "Paste\tCtrl+V")
editm.Append(wx.ID_SELECTALL, "SelectAll\tCtrl+A")
editm.AppendSeparator()
editm.Append(ID_READ_ONLY, "Read Only",
kind=wx.ITEM_CHECK)
menub.Append(editm, "E&dit")
"""# History Menu
historym = wx.Menu()
historym.Append(wx.ID_RECENT, "Recent\tCtrl+N")
menub.Append(historym, "&History")
# Help Menu
helpm = wx.Menu()
helpm.Append(wx.ID_HINT, "Hint")
helpm.Append(wx.ID_ABOUT, "About")
menub.Append(helpm, "&Help")"""
self.SetMenuBar(menub)
# Event Handlers
self.Bind(wx.EVT_MENU, self.OnMenu)
def OnMenu(self, event):
"""Handle menu clicks"""
evt_id = event.GetId()
actions = { wx.ID_COPY : self.txtctrl.Copy,
wx.ID_CUT : self.txtctrl.Cut,
wx.ID_PASTE : self.txtctrl.Paste }
action = actions.get(evt_id, None)
if action:
action()
elif evt_id == ID_READ_ONLY:
# Toggle enabled state
self.txtctrl.Enable(not self.txtctrl.Enabled)
elif evt_id == wx.ID_OPEN:
dlg = wx.FileDialog(self, "Open File", style=wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
fname = dlg.GetPath()
handle = open(fname, 'r')
self.txtctrl.SetValue(handle.read())
handle.close()
else:
event.Skip()
if __name__ == "__main__":
app = MyApp(False)
app.MainLoop()

The problem with the history and help menus, is that the Id's that you are using do not exist i.e. ID_RECENT and ID_HINT, so use something else or assign your own Id's.
The selectall function was not assigned.
Note, that the cut and copy functions require that you have selected something.
You haven't written any code for the Save or Saveas functions.
The undo and redo functions do not seem to function in a standard wx.TextCtrl, so you could use a StyledTextCtrl instead.
Hopefully, the code below will move you on a bit.
(It's for wxpython Phoenix and python3 on Linux)
import wx
import wx.stc
class MyApp(wx.App):
def OnInit(self):
self.frame = MenuFrame(None, title="Menus and MenuBars")
self.SetTopWindow(self.frame)
self.frame.Show()
return True
ID_READ_ONLY = wx.NewId()
class MenuFrame(wx.Frame):
def __init__(self, *args, **kwargs):
super(MenuFrame, self).__init__(*args, **kwargs)
# Attributes
self.panel = wx.Panel(self)
self.txtctrl = wx.stc.StyledTextCtrl(self.panel,
style=wx.TE_MULTILINE)
# Layout
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.txtctrl, 1, wx.EXPAND)
self.panel.SetSizer(sizer)
self.CreateStatusBar() # For output display
# Setup the Menu
menub = wx.MenuBar()
# File Menu
filem = wx.Menu()
filem.Append(wx.ID_NEW, "New\tCtrl+N")
filem.Append(wx.ID_OPEN, "Open\tCtrl+O")
filem.Append(wx.ID_SAVE, "Save\tCtrl+S")
filem.Append(wx.ID_SAVEAS, "Save_As\tCtrl+Shift+S")
menub.Append(filem, "&File")
# Edit Menu
editm = wx.Menu()
editm.Append(wx.ID_UNDO, "Undo\tCtrl+Z")
editm.Append(wx.ID_REDO, "Redo\tCtrl+Shift+Z")
editm.Append(wx.ID_COPY, "Copy\tCtrl+C")
editm.Append(wx.ID_CUT, "Cut\tCtrl+X")
editm.Append(wx.ID_PASTE, "Paste\tCtrl+V")
editm.Append(wx.ID_SELECTALL, "SelectAll\tCtrl+A")
editm.AppendSeparator()
editm.Append(ID_READ_ONLY, "Read Only",
kind=wx.ITEM_CHECK)
menub.Append(editm, "E&dit")
# History Menu
historym = wx.Menu()
historym.Append(wx.ID_PREVIEW, "Recent\tCtrl+N")
menub.Append(historym, "&History")
# Help Menu
helpm = wx.Menu()
helpm.Append(wx.ID_HELP_INDEX, "Hint")
helpm.Append(wx.ID_ABOUT, "About")
menub.Append(helpm, "&Help")
self.SetMenuBar(menub)
# Event Handlers
self.Bind(wx.EVT_MENU, self.OnMenu)
def OnMenu(self, event):
"""Handle menu clicks"""
evt_id = event.GetId()
actions = { wx.ID_COPY : self.txtctrl.Copy,
wx.ID_CUT : self.txtctrl.Cut,
wx.ID_PASTE : self.txtctrl.Paste,
wx.ID_UNDO : self.txtctrl.Undo,
wx.ID_REDO : self.txtctrl.Redo,
wx.ID_SELECTALL : self.txtctrl.SelectAll}
action = actions.get(evt_id, None)
if action:
action()
elif evt_id == ID_READ_ONLY:
# Toggle enabled state
self.txtctrl.Enable(not self.txtctrl.Enabled)
elif evt_id == wx.ID_OPEN:
dlg = wx.FileDialog(self, "Open File", style=wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
fname = dlg.GetPath()
handle = open(fname, 'r')
self.txtctrl.SetValue(handle.read())
handle.close()
else:
event.Skip()
if __name__ == "__main__":
app = MyApp(False)
app.MainLoop()

Related

How can i load class in notebook when tab is clicked in wxpython?

Here i have a doubt that when notebook tab is clicked at that time only it should load class in that tab. But in wxpython all class loads by default in tab so how can i put conditions to load class when tab is clicked.
Here is a small example.
import wx
class tabclass(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "This is the help tab", (20,20))
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="notebook")
mainPanel = wx.Panel(self,size=(1365, 700), pos=(0, 71), style=wx.DOUBLE_BORDER)
self.nb = wx.Notebook(mainPanel,size=(1365, 700))
tab0 = tabclass(self.nb)
self.nb.AddPage(tab0, "Tab One")
if __name__ == "__main__":
app = wx.App()
MainFrame().Show()
app.MainLoop()
Here I want to display static text when the tab is clicked otherwise class should not load.
You might want a different method of selecting what is and what is not displayed in the notebook.
A menu, for example, seems an appropriate selection tool.
i.e.
import wx
class tabclass(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "This is the help tab", (20,20))
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="notebook")
mainPanel = wx.Panel(self,size=(1365, 700), pos=(0, 71), style=wx.DOUBLE_BORDER)
self.nb = wx.Notebook(mainPanel,size=(1365, 700))
tabmenu = wx.Menu()
t1 = wx.MenuItem(tabmenu, id = -1, text="Help", kind=wx.ITEM_CHECK)
tabmenu.Append(t1)
t2 = wx.MenuItem(tabmenu, id = -1, text="Other tab", kind=wx.ITEM_CHECK)
tabmenu.Append(t2)
t3 = wx.MenuItem(tabmenu, id = -1, text="Another tab", kind=wx.ITEM_CHECK)
tabmenu.Append(t3)
tabmenu.Append(wx.ID_EXIT, '&Quit')
# Creating the menubar.
menuBar = wx.MenuBar()
# Add menus
menuBar.Append(tabmenu, "&Tabs")
# Adding the MenuBar to the Frame content.
self.SetMenuBar(menuBar)
# Bind menu item to functions
self.Bind(wx.EVT_MENU, self.helptab, t1)
self.Bind(wx.EVT_MENU, self.othertab, t2)
self.Bind(wx.EVT_MENU, self.anothertab, t3)
self.Bind(wx.EVT_MENU, self.OnQuit, id=wx.ID_EXIT)
# Obviously, here you would differentiate your tabs
# I have used the same one for brevity
def helptab(self, event):
if event.IsChecked():
self.tab0 = tabclass(self.nb)
self.nb.AddPage(self.tab0, "Help Tab")
else:
#Delete the "Help Tab"
pass
def othertab(self, event):
if event.IsChecked():
self.tab1 = tabclass(self.nb)
self.nb.AddPage(self.tab1, "Other Tab")
else:
#Delete the "Other Tab"
pass
def anothertab(self, event):
if event.IsChecked():
self.tab2 = tabclass(self.nb)
self.nb.AddPage(self.tab2, "Another Tab")
else:
#Delete the "Another Tab"
pass
def OnQuit(self, event):
self.Destroy()
if __name__ == "__main__":
app = wx.App()
MainFrame().Show()
app.MainLoop()

how entrer value in new window and passing values of wx.newindow to wx.Frame wxpython

i have a panel with button Dynamic and when i click in button i have a new window opened the problem that i need zone to enter values to edit parameter of dynamic image like that :
that my code :
import wx
class MainFrame(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent,title="Myfirst",size=(800,580))
self.top = wx.Panel(self, style = wx.SUNKEN_BORDER)
self.bottom = wx.Panel(self ,style = wx.SUNKEN_BORDER)
self.left = wx.Panel(self ,style = wx.SUNKEN_BORDER, size = (250,-1))
st1 = wx.StaticText(self.bottom, -1, "show info ")
self.bottom.SetBackgroundColour('white')
dynamic=wx.Button(self.left,-1,"Dynamique",size=(110,30),pos=(50,100))
self.Bind(wx.EVT_BUTTON, self.newwindow, dynamic)
sizer1 = wx.BoxSizer(wx.VERTICAL)
sizer1.Add(self.top,1,wx.EXPAND,5)
sizer1.Add(self.bottom,1,wx.EXPAND,5)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add(self.left,0,wx.EXPAND,5)
sizer2.Add(sizer1,1,wx.EXPAND,5)
self.SetSizer(sizer2)
def newwindow(self, event):
secondWindow = window2(parent=self.left)
secondWindow.Show()
class window2(wx.Frame):
title = "new Window"
def __init__(self,parent):
wx.Frame.__init__(self,parent, -1,'Dynamic of image', size=(300,100))
panel=wx.Panel(self, -1)
self.SetBackgroundColour(wx.Colour(100,100,100))
self.Centre()
self.Show()
app = wx.App()
frame=MainFrame(None).Show()
app.MainLoop()
how can i add the zone to edit parametre like picture ?
i not sure if the newwindow what i need or dialog !!
thanks for help
I guess you will be fine with a normal new window. You can get the zone to write the parameters with a wx.TextCtrl widgets. You will need a way to export the values typed into the wx.TextCtrl so I added the style wx.TE_PROCESS_ENTER. With this style when you finish typing and press Enter you can process the typed values.
Also, there is no need to use Show() two times (secondWindow.Show() and self.Show()). One of them is enough.
Code with comments:
import wx
class MainFrame(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent,title="Myfirst",size=(800,580))
self.top = wx.Panel(self, style = wx.SUNKEN_BORDER)
self.bottom = wx.Panel(self ,style = wx.SUNKEN_BORDER)
self.left = wx.Panel(self ,style = wx.SUNKEN_BORDER, size = (250,-1))
st1 = wx.StaticText(self.bottom, -1, "show info ")
self.bottom.SetBackgroundColour('white')
dynamic=wx.Button(self.left,-1,"Dynamique",size=(110,30),pos=(50,100))
self.Bind(wx.EVT_BUTTON, self.newwindow, dynamic)
sizer1 = wx.BoxSizer(wx.VERTICAL)
sizer1.Add(self.top,1,wx.EXPAND,5)
sizer1.Add(self.bottom,1,wx.EXPAND,5)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add(self.left,0,wx.EXPAND,5)
sizer2.Add(sizer1,1,wx.EXPAND,5)
self.SetSizer(sizer2)
def newwindow(self, event):
secondWindow = window2(parent=self.left)
secondWindow.Show()
class window2(wx.Frame):
title = "new Window"
def __init__(self,parent):
"""
This is similar to the class MainFrame. You define a parent wx.Panel
and all other widgets are his childs.
"""
wx.Frame.__init__(self,parent, -1,'Dynamic of image', size=(300,100))
self.panel=wx.Panel(self, -1, style=wx.SUNKEN_BORDER)
self.st = wx.StaticText(self.panel, label='modifier bornes de la dynamique', style=wx.ALIGN_CENTER)
#### Notice the wx.TE_PROCESS_ENTER style to trigger processing the input when
#### Enter is pressed. Another alternative is to put a button somewhere.
self.text = wx.TextCtrl(self.panel, size=(200, 20), style=wx.SUNKEN_BORDER|wx.TE_PROCESS_ENTER)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.st, 0, wx.EXPAND|wx.ALL, 5)
self.sizer.Add(self.text, 0, wx.ALIGN_CENTER|wx.ALL, 5)
self.panel.SetSizer(self.sizer)
self.sizer.Fit(self.panel)
#self.SetBackgroundColour(wx.Colour(100,100,100))
self.Centre()
#### No need to use Show() here since you already use it in MainFrame.newwindow()
self.Show()
#### To execute self.onEnter when Enter is pressed inside self.text
self.Bind(wx.EVT_TEXT_ENTER, self.onEnter)
def onEnter(self, event):
#### Change it to fit your needs
print(self.text.GetValue())
self.Destroy()
app = wx.App()
frame=MainFrame(None).Show()
app.MainLoop()

Pyqt5 inheritance

full code: link to file
I'm trying to build a GUI using PyQT5, and I have two classes:
In the main class "Window", I have a method to close/exit the GUI, and when I use the method within the class, everything is working
class Window(QMainWindow):
choice = QMessageBox.question(self, ' WARNING!!!!', 'Are you sure to {}'.format(message),
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
if choice == QMessageBox.Yes:
print('Quiting Application')
return sys.exit()
else:
pass
But the problem starts here, with the second class, when I try to quit using other class : Q_button.clicked.connect(Window.close_app)
class NewGrid(QWidget):
def __init__(self, parent=None):
super(NewGrid, self).__init__(parent)
grid = QGridLayout()
grid.addWidget(self.createExampleGroup(), 0, 0)
grid.addWidget(self.check_box_vBBU(), 21, 21)
grid.addWidget(self.button_test(), 0, 1)
grid.addWidget(self.quit_button(), 2, 2)
self.setLayout(grid)
def quit_button(self):
groupBox = QGroupBox("Quit_placeholder")
Q_button = QPushButton('Quit', self)
box = QHBoxLayout()
box.addWidget(Q_button)
super()
**Q_button.clicked.connect(Window.close_app)**
groupBox.setLayout(box)
return groupBox
any solution?
edit:
Here is the full code
import sys
from ctypes import windll
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
SCREEN_WIDTH = windll.user32.GetSystemMetrics(0) # 1920
SCREEN_HEIGHT = windll.user32.GetSystemMetrics(1) # 1080
class CustomDialog(QDialog):
def __init__(self, *args, **kwargs):
super(CustomDialog, self).__init__(parent=None)
Qbtn = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
self.buttonBox.accepted(self.accept)
self.buttonBox.rejected(self.reject)
self.layout = QVBoxLayout()
self.layout.addWidget(self.buttonBox)
self.setLayout(self.layout)
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(SCREEN_WIDTH / 3, SCREEN_HEIGHT / 3, SCREEN_WIDTH / 3,
SCREEN_HEIGHT / 2) # (start_point_X,start_point_Y,DIMensionX,DimensionY)
self.setWindowIcon(QIcon('dru_icon.png'))
self.setWindowTitle('----------------------DRU GUI-----------------')
self.statusBar()
self.file_menu()
self.view_menu()
self.toolbar_menu()
self.home()
def home(self):
# btn = QPushButton('quit', self)
# btn.clicked.connect(self.close_app)
# make the buttons near the bottom right: 1920-50=450, 1080-50=250
# btn.move(450, 250)
# btn.resize(50, 50)
# btn.move(SCREEN_WIDTH / 3 - 50, SCREEN_HEIGHT / 2 - 50)
label = QLabel("Holla")
label.setAlignment(Qt.AlignBottom)
self.setCentralWidget(label)
widget = NewGrid()
self.setCentralWidget(widget)
self.show()
def toolbar_menu(self):
toolbar = QToolBar("My Main Toolbar")
toolbar.setIconSize(QSize(32, 32)) # manual size
self.addToolBar(toolbar) # showing toolbar
icon1 = self.set_toolbar_icon('new_icon', icon_image='truck--plus')
icon1.triggered.connect(self.close_app)
icon2 = self.set_toolbar_icon('iconNum2')
# icon2.triggered.connect(self.notification_button)
icon3 = self.set_toolbar_icon('iconNum3', icon_image='application-monitor')
def set_toolbar_icon(self, icon_name="NONE", icon_image='animal-monkey.png', Width=32, Length=32):
toolbar = QToolBar("My Main Toolbar")
toolbar.setIconSize(QSize(Width, Length)) # manual size
self.addToolBar(toolbar) # showing toolbar
icon_name = QAction(QIcon(icon_image), icon_name, self)
# icon_name.triggered.connect(
# self.notification_button) # add different command later, for now its quitting,
self.toolBar = self.addToolBar('RUN IT')
# self.toolBar.addAction(icon1) #will be the default icon size from windows
toolbar.addAction(icon_name)
return icon_name
# ------main menu dialog --------
def file_menu(self):
# creating a toolbar with menu
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
quit_tooltip = QAction('&Quit', self)
quit_tooltip.setShortcut('Ctrl+Q')
quit_tooltip.setToolTip('close the app')
# quit_tooltip.triggered.connect(self.close_app)
quit_tooltip.triggered.connect(self.close_app)
fileMenu.addAction(quit_tooltip)
def view_menu(self):
mainMenu = self.menuBar()
viewMenu = mainMenu.addMenu('View')
# ------main menu dialog --------
def notification_button(self):
return self.areYouSure_toolbar(message='NO ACTION DEFINED')
def close_app(self): # defined our own method of closing
# choice = QMessageBox.question(self, ' WARNING!!!!', 'Are you sure to quit?',
# QMessageBox.Yes | QMessageBox.No,
# QMessageBox.No) # the last QMessageBox.No is to highliht the option implicitly
choice = self.areYouSure_toolbar(message='Quit')
if choice == QMessageBox.Yes:
print('Quiting Application')
return sys.exit()
else:
pass
def areYouSure_toolbar(self, message='____'):
choice = QMessageBox.question(self, ' WARNING!!!!', 'Are you sure to {}'.format(message),
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No) # the last QMessageBox.No is to highliht the option implicitly
return choice
class NewGrid(QWidget):
def __init__(self, parent=None):
super(NewGrid, self).__init__(parent)
grid = QGridLayout()
grid.addWidget(self.createExampleGroup(), 0, 0)
grid.addWidget(self.check_box_vBBU(), 0, 1)
grid.addWidget(self.button_test(), 1, 0)
grid.addWidget(self.check_box_LPR(), 1, 1)
grid.addWidget(self.quit_button(), 3, 3)
# grid.addWidget(self.createExampleGroup(), 1, 2)
self.setLayout(grid)
# temp = Window()
# self.setWindowTitle("PyQt5 Group Box")
# self.resize(400, 300)
def check_box_vBBU(self):
groupBox = QGroupBox("Input (From vBBU) ")
checkbox1 = QCheckBox('Port {}'.format(1), self)
checkbox2 = QCheckBox('Port {}'.format(2), self)
check_box = QHBoxLayout()
check_box.addWidget(checkbox1)
check_box.addWidget(checkbox2)
check_box.addStretch(1)
groupBox.setLayout(check_box)
return groupBox
def check_box_LPR(self):
groupBox = QGroupBox("Output (from LPR) ")
layout = QHBoxLayout()
for n in range(20):
btn = QCheckBox('LPR' + str(n))
# btn.pressed.connect(self.close_app_Newgrid) #where to connect
layout.addWidget(btn)
# btn.setChecked(True)
num_of_buttons = n
# widget = QWidget()
selectRandom_btn = QPushButton('Random ports')
layout.addWidget(selectRandom_btn)
selectAll_btn = QPushButton('All')
selectAll_btn.pressed.connect(lambda : self.select_buttons(num_of_buttons, btn))
layout.addWidget(selectAll_btn)
groupBox.setLayout(layout)
return groupBox
def select_buttons(self, num_of_buttons,btn):
for x in range(1,num_of_buttons-1):
btn.setC
# btn[1].setChecked(True)
def quit_button(self):
groupBox = QGroupBox("Quit_placeholder")
Q_button = QPushButton('Quit', self)
box = QHBoxLayout()
box.addWidget(Q_button)
super()
# Q_button.clicked.connect(self.close_app_Newgrid)
# Q_button.clicked.connect(Window.close_app)
groupBox.setLayout(box)
return groupBox
def button_test(self):
groupBox = QGroupBox("test buttons")
btn1 = QPushButton("Push me", self)
# btn1.clicked.connect(self.close_app)# need to connect the button somewhere..
vbox = QHBoxLayout()
vbox.addWidget(btn1)
# vbox.addStretch(1)
groupBox.setLayout(vbox)
return groupBox
def createExampleGroup(self):
groupBox = QGroupBox("Data type")
radio1 = QRadioButton("Binary")
radio2 = QRadioButton("Decimal")
radio1.setChecked(True)
vbox = QVBoxLayout()
vbox.addWidget(radio1)
vbox.addWidget(radio2)
vbox.addStretch(1)
groupBox.setLayout(vbox)
return groupBox
def close_app_Newgrid(self): # defined our own method of closing
choice = QMessageBox.question(self, ' WARNING!!!!', 'Are you sure to quit?',
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No) # the last QMessageBox.No is to highliht the option implicitly
if choice == QMessageBox.Yes:
# print('Quiting Application')
return sys.exit()
else:
return
def run():
app = QApplication(sys.argv)
Gui = Window()
sys.exit(app.exec_())
run()
update :
I tried to simplify the solution below:
for w in QApplication.topLevelWidgets():
if isinstance(w,Window):
w = True
windows = w
The connection is between the signal of one object and the slot of another, not between classes, so the instruction is incorrect.
So in order to perform this task we must get some way to the Window object, a possible solution is to take advantage of Window is the window, so it is a top-Level for it we use the topLevelWidgets() method and we filter through isinstance():
def quit_button(self):
groupBox = QGroupBox("Quit_placeholder")
Q_button = QPushButton('Quit', self)
box = QHBoxLayout()
box.addWidget(Q_button)
#Q_button.clicked.connect(self.close_app_Newgrid)
windows = [w for w in QApplication.topLevelWidgets() if isinstance(w, Window)]
if windows:
Q_button.clicked.connect(windows[0].close_app)
groupBox.setLayout(box)
return groupBox
Note: the problem is not due to inheritance.

PYQT how to draw line between two buttons

from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.view = View(self)
self.button = QtGui.QPushButton('Clear View', self)
self.button.clicked.connect(self.handleClearView)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.view)
layout.addWidget(self.button)
def handleClearView(self):
self.view.scene().clear()
class DragButton(QtGui.QPushButton):
def mousePressEvent(self, event):
self.__mousePressPos = None
self.__mouseMovePos = None
if event.button() == QtCore.Qt.LeftButton:
self.__mousePressPos = event.globalPos()
self.__mouseMovePos = event.globalPos()
#super(DragButton, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() == QtCore.Qt.LeftButton:
# adjust offset from clicked point to origin of widget
currPos = self.mapToGlobal(self.pos())
globalPos = event.globalPos()
diff = globalPos - self.__mouseMovePos
newPos = self.mapFromGlobal(currPos + diff)
self.move(newPos)
self.__mouseMovePos = globalPos
#super(DragButton, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if self.__mousePressPos is not None:
moved = event.globalPos() - self.__mousePressPos
if moved.manhattanLength() > 3:
event.ignore()
return
#super(DragButton, self).mouseReleaseEvent(event)
class View(QtGui.QGraphicsView):
def __init__(self, parent):
QtGui.QGraphicsView.__init__(self, parent)
self.setScene(QtGui.QGraphicsScene(self))
self.setSceneRect(QtCore.QRectF(self.viewport().rect()))
btn1=DragButton('Test1', self)
btn2=DragButton('Test2', self)
def mousePressEvent(self, event):
self._start = event.pos()
def mouseReleaseEvent(self, event):
start = QtCore.QPointF(self.mapToScene(self._start))
end = QtCore.QPointF(self.mapToScene(event.pos()))
self.scene().addItem(
QtGui.QGraphicsLineItem(QtCore.QLineF(start, end)))
for point in (start, end):
text = self.scene().addSimpleText(
'(%d, %d)' % (point.x(), point.y()))
text.setBrush(QtCore.Qt.red)
text.setPos(point)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
Here is my code. There are two movable buttons on the QGraphicsView and I can draw line on the QGraphicsView with mouse dragging. But what I want to do is to draw line between two buttons. For detail, If I right click the btn1(Test1) and then right click the btn2(Test2) , the line would be created between two buttons. I'm struggling this problem for a month. Plz Help!
I am assuming that the line you need to draw between the buttons must also be movable. If not it is just simple you can just use :
lines = QtGui.QPainter()
lines.setPen(self)
lines.drawLine(x1,y1,x2,y2)
So, if the line needs to be movable along with the buttons then first you create a mini widget consisting of Two Buttons and the Line, so you can move the whole widget. This might help!, in that case.

wxpython - Menu interaction

I'm having trouble with a menu system - I have a basic example here (below) that shows a basic menu example I've followed; specifically, my issue is how may I make decisions from use menu choices, I'm not sure how to interact user choice with a menu choice?
Could someone point me in the right direction, or ideally give a brief example on this - say input data from a menu and display this?
Thanks
import wx
class myFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id,'Menu', size=(300,200))
panel = wx.Panel(self)
status = self.CreateStatusBar()
menubar = wx.MenuBar()
firstMenu = wx.Menu()
secondMenu = wx.Menu()
# create files
firstMenu.Append(wx.NewId(), 'Save Data' , 'Save data')
firstMenu.Append(wx.NewId(), 'Open Data..', 'Open a new window')
secondMenu.Append(wx.NewId(),'Configure..', 'Input Data here')
# append to menu
menubar.Append(firstMenu, 'File')
menubar.Append(secondMenu,'Options')
#
self.SetMenuBar(menubar)
if( __name__ == '__main__' ):
app = wx.PySimpleApp()
frame = myFrame(parent=None, id=-1)
frame.Show()
app.MainLoop()
You have to bind wx.EVT_MENU event. See wxPython demo for more examples. In your case that would be somethink like:
import wx
SAVE_DATA = wx.NewId()
OPEN_DATA = wx.NewId()
CONFIGURE = wx.NewId()
class myFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id,'Menu', size=(300,200))
panel = wx.Panel(self)
status = self.CreateStatusBar()
menubar = wx.MenuBar()
firstMenu = wx.Menu()
secondMenu = wx.Menu()
# create files
firstMenu.Append(SAVE_DATA, 'Save Data' , 'Save data')
firstMenu.Append(OPEN_DATA, 'Open Data..', 'Open a new window')
secondMenu.Append(CONFIGURE,'Configure..', 'Input Data here')
# append to menu
menubar.Append(firstMenu, 'File')
menubar.Append(secondMenu,'Options')
#
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU, self.SaveData, id=SAVE_DATA)
self.Bind(wx.EVT_MENU, self.OpenData, id=OPEN_DATA)
self.Bind(wx.EVT_MENU, self.Configure, id=CONFIGURE)
def SaveData(self, e):
print("Save")
def OpenData(self, e):
print("Open")
def Configure(self, e):
print("Config")
if( __name__ == '__main__' ):
app = wx.PySimpleApp()
frame = myFrame(parent=None, id=-1)
frame.Show()
app.MainLoop()

Resources