I have 2 radiobuttons created (inside a QMainWindow class) like:
def dtype_radiobuttons(self):
layout = QHBoxLayout()
rb1 = QRadioButton("complex")
rb1.toggled.connect(lambda: self.update_image("dtype", rb1.text()))
self.real_dtype_rb = QRadioButton("real", self)
self.real_dtype_rb.toggled.connect(lambda: self.update_image("dtype", self.real_dtype_rb.text()))
self.btngroup.append(QButtonGroup())
self.btngroup[-1].addButton(self.real_dtype_rb)
self.btngroup[-1].addButton(rb1)
rb1.setChecked(True)
layout.addWidget(rb1)
layout.addWidget(self.real_dtype_rb)
layout.addStretch()
return layout
def library_radiobutton(self):
layout = QHBoxLayout()
self.cvnn_library_rb = QRadioButton("cvnn", self)
self.cvnn_library_rb.toggled.connect(lambda: self.update_image("library", self.cvnn_library_rb.text()))
rb2 = QRadioButton("tensorflow", self)
rb2.toggled.connect(lambda: self.update_image("library", rb2.text()))
self.btngroup.append(QButtonGroup())
self.btngroup[-1].addButton(rb2)
self.btngroup[-1].addButton(self.cvnn_library_rb)
self.cvnn_library_rb.setChecked(True)
layout.addWidget(self.cvnn_library_rb)
layout.addWidget(rb2)
layout.addStretch()
return layout
I want to make it impossible to select the complex option of the dtype radiobuttons group and tensorflow radiobutton of the library radiobuttons. Leaving 3 out of the 4 possible combinations. So if I select complex and library was tensorflow, I want to automatically change the library to cvnn. I tried to implement it like this:
def update_image(self, key, value):
if value == "complex":
if hasattr(self, 'cvnn_library_rb'): # It wont exists if I still didnt create the radiobutton.
self.cvnn_library_rb.setChecked(True) # Set library cvnn
elif value == "tensorflow":
if hasattr(self, 'real_dtype_rb'):
self.real_dtype_rb.setChecked(True) # Set real dtype
... Do the other stuff I need to do.
The weird thing is that it actually works in the sense that, for example, if I am on complex activated and select tensorflow, the radiobutton changes to real (what I want!) but tensorflow does not get selected! I need to select it again as if making self.real_dtype_rb.setChecked(True) cancels the selection of the radiobutton I clicked on. (Very weird if you ask me).
The hasattr is used because depending on the order I call the
functions, there are some radiobuttons that will be created before
the other, so it might not exist.
This
is an option I am considering but it's disabling the radiobutton
group instead of changing their state (not what I prefer).
The signal toggled is triggered whenever you change the state of your radio buttons. So, it will be triggered when you call setChecked (once for the radio button you toggle and once for the other you untoggle) and update_image is called is the wrong case.
You have to check the state of the radio button and call update_image only if the radio button is toggled:
rb2.toggled.connect(lambda state: state and self.update_image("library", rb2.text(), state))
Related
so I have a mass of buttons on a QT Designer GUI application all named LED_i where i ranges from 0-191, ie: LED_0, LED_1, ..., LED_191. I would like basically the same thing to happen when clicked except changing the input i. So LED_0 when clicked would call the function OnClick(0), LED_75 would call OnClick(75) etc etc.
I am connecting my buttons with
ui.LED_0.clicked.connect(OnClick0)
usually using a separate function for each button. However this would require 191 functions, and 191 lines like the above connecting my buttons to their functions. I'm sure I could edit it s.t. I can use the same function by passing the button name that was clicked and getting the number from it but that would still require the 191 lines of button.clicked.connect. Is there any way to do this more efficiently?
TIA
It's not impossible with PyQt5. This is a good fit for a QButtonGroup. And there are a few ways to approach it.
QButtonGroup using Designer
To set up a QButtonGroup in Designer, do the following:
select two or more buttons, radioboxes, or checkboxes
right click on one of the buttons and you'll get a context menu item, Assign to button group --> New button group
After this, you'll see buttonGroup (the default name) show up in the Object Inspector.
To run code when one of your buttons is clicked, you can use the buttonClicked signal of QButtonGroup. It will give you a reference to the button that was clicked, and from the objectName, you can figure out what to do.
ui.buttonGroup.buttonClicked.connect(self.OnClicked)
then
def OnClicked(self, button):
# button objectName follows pattern LED_<number>
button_number = int(button.objectName()[4:])
... do stuff here with button_number
QButtonGroup in code
In the original post, there were 191 buttons in Designer. That is a lot of buttons to arrange. If for some reason, you wanted to do it in code instead, you could assign each button an id as it is added to the group and then you could use the idClicked signal:
grid = QGridLayout()
buttonGroup = QButtonGroup()
buttonGroup.idClicked.connect(OnClick)
buttonList = []
for row in range(14):
rowList = []
for col in range(14):
button_number = 14*row + col
button = QPushButton(f'{button_number}', objectName=f'LED_{button_number}')
rowList.append(button)
buttonGroup.addButton(button, button_number)
grid.addWidget(button, row, col)
then
def OnClick(self, idClicked):
... do something with idClicked here
I'm making a user-interface in PYQT 5. It includes a QGroupBox containing several QRadioButton. When there is a toggled event I check the buttons and do some stuff. The problem is that there are two toggled events because one button is toggled on and one off so my code is always running twice.
I have been looking for an event from the QGroupBox instead. That should only happen once instead of twice when i toggle a radiobutton.
def __init__(self):
self.radioButton1.toggled.connect(self.update_stimulus)
self.radioButton2.toggled.connect(self.update_stimulus)
self.radioButton3.toggled.connect(self.update_stimulus)
def update_stimulus(self):
if self.radioButton1.isChecked():
print('1')
if self.radioButton2.isChecked():
print('2')
if self.radioButton3.isChecked():
print('3')
# Do stuff based on button positions
I tried using
self.groupBox.toggled.connect(self.update_stimulus)
But that only works if the groupbox itself is toggled. Is there any way to get a signal from the groupbox when one of the radiobuttons changes or do I have to find some way of doing it with the indivdual signals from the radiobuttons?
As #ekhumoro explains, you can add the checked parameter to the function and do your processing only if it's True:
def update_stimulus(self, checked):
if not checked:
return
if self.radioButton1.isChecked():
print('1')
if self.radioButton2.isChecked():
print('2')
if self.radioButton3.isChecked():
print('3')
Keep in mind, though, that if you want to have different radio button "groups" within the same groupbox, none of this will work properly as they will all be considered as part of the same single group: all Qt buttons (widgets that inherit QAbstractButton: QPushButton, QToolButton, QCheckBox and QRadioButton) have an autoExclusive property which is off by default except from radio buttons. This property makes all button that belong to the same parent widget automatically exclusive.
If you need different groups within the same parent, the solution is to use a QButtonGroup, which extends the exclusive functionality by limiting the membership of each button to a specific group.
def __init__(self):
# ...
self.buttonGroupA = QtWidgets.QButtonGroup()
self.buttonGroupA.addButton(self.radioButton1)
self.buttonGroupA.addButton(self.radioButton2)
self.buttonGroupA.addButton(self.radioButton3)
self.buttonGroupA.buttonToggled[QtWidgets.QAbstractButton, bool].connect(self.update_stimulusA)
self.buttonGroupB = QtWidgets.QButtonGroup()
self.buttonGroupB.addButton(self.radioButton4)
self.buttonGroupB.addButton(self.radioButton5)
self.buttonGroupB.addButton(self.radioButton6)
self.buttonGroupB.buttonToggled[QtWidgets.QAbstractButton, bool].connect(self.update_stimulusB)
def update_stimulusA(self, btn, checked):
if not checked:
return
# do something with group A
def update_stimulusB(self, btn, checked):
if not checked:
return
# do something with group B
Creation of a button group is also possible from Designer: just select at least two buttons that will be members of the same group, right click on one of them, go to the "Assign to button group" sub menu and select "New button group". To add a button to an existing group, just use the same context menu and choose the group you want to add that button to.
I want to delete a menu checkbutton when i right click on it.
Its usually done with bind("Mouse3", deletefunction) , BUT i need an actual instance of a checkbutton to bind it with, and the only way to add a checkbutton to a menu i know is a add_checkbutton() method (and i have no access to instance this way). Is there any way i could do this?
import tkinter as tk
root = tk.Tk()
menubar = tk.Menu(root)
view_menu = tk.Menu(menubar, tearoff=0)
view_menu.add_checkbutton(label="Right click on me to delete", onvalue=1, offvalue=False)
# I want to do something like this:
# c = Checkbutton(label="Right click on me to delete")
# c.bind("Mouse3", my_delete_function())
# view_menu.add(c)
menubar.add_cascade(label='View', menu=view_menu)
root.config(menu=menubar)
root.mainloop()
To the best of my understanding, there's essentially two parts to your question:
Can the menu bar item be assigned for manipulation after?
Can the referenced item be then bound to an event?
The first answer is, sort of. While you can't exactly assign the object, you can reference it by index like this:
view_menu.delete(0)
Since you added the checkbutton first, it'll have an index of 0. You can either keep track of the indices, or refer to the item by its label. See this related answer from Bryan Oakley. e.g.:
view_menu.delete(view_menu.index("Right click on me to delete"))
The .index() method will locate the index by the menu entry's label, which can be handy unless you have the same label more than once...
The second answer, as far as I'm aware, there doesn't seem to be any effective binding for typical events like mouse clicks. However after some search I did come across a rather hidden <<MenuSelect>> binding that at least triggers an event. That by itself is not useful to your quest, but you can combine the event state with the checkbutton's command argument as well as a boolean flag to trigger an event on click:
# Add a BooleanVar for flagging:
delete_checkbutton = tk.BooleanVar()
# Add a binding to your view_menu
view_menu.bind('<<MenuSelect>>', event_state)
# Define the callback function:
def event_state(e):
if bool(e.state & 0x0400): # watch for the Mouse Right click state
# you might opt to use 0x0004 or 0x0001 instead
# i.e. Ctrl+click or Shift+Click
delete_checkbutton.set(True)
else: # If the state is not right click, reset the flag
delete_checkbutton.set(False)
# Define a self_delete command for the checkbutton
def self_delete():
if delete_checkbutton.get():
view_menu.delete(view_menu.index("Right click on me to delete"))
# Add the command to your checkbutton
view_menu.add_checkbutton(label="Right click on me to delete", onvalue=lambda:print('hey'), offvalue=False, command=self_delete)
Note: You will actually have to hold right click and then left click on the checkbutton to delete it. Obviously the drawback is you have now triggered the on/off value, and you might need to have some additional handling on those.
If right + left click is too awkward, Ctrl/Shift is another mod state you might consider.
Another side note: I'm a proponent of OOP when it comes to tkinter, it makes accessible variables and flags much easier without needing to worry the global and nonlocal namespaces. Here since delete_checkbutton is set in the global namespace I avoied using the global keyword and accessed it via the tk.BooleanVar() object. However if you were to use a python boolean (e.g. flag = True) then it won't be as effective unless you indicate global flag in both functions. If however you took an OOP approach you can reference the flags directly via self.flag without ambiguity.
Finally, here are the comprehensive changes implemented into your code for sampling:
import tkinter as tk
def event_state(e):
if bool(e.state & 0x0400):
# you might opt to use 0x0004 or 0x0001 instead
# i.e. Ctrl+click or Shift+Click
delete_checkbutton.set(True)
else:
delete_checkbutton.set(False)
def self_delete():
if delete_checkbutton.get():
view_menu.delete(view_menu.index("Right click on me to delete"))
root = tk.Tk()
menubar = tk.Menu(root)
delete_checkbutton = tk.BooleanVar()
view_menu = tk.Menu(menubar, tearoff=0)
view_menu.add_command(label='dude', command=lambda: print('dude'))
view_menu.add_checkbutton(label="Right click on me to delete", onvalue=lambda:print('hey'), offvalue=False, command=self_delete)
menubar.add_cascade(label='View', menu=view_menu)
root.config(menu=menubar)
view_menu.bind('<<MenuSelect>>', event_state)
root.mainloop()
All that said, I am of the opinion that this is not a very smooth User Experience and is somewhat confusing. Just the permanent deletion of the menu item alone is questionable at best, combined with the method you are trying to call upon the deletion feels even more contrived. I'd suggest revisiting your UX flow to consider how to streamline this.
I'm working on a GUI that will eventually run one of several data analyses depending on which the user selects. In this part of the GUI, I have four radio buttons with options, and then a display button. I would like one of four imported functions to run when the user hits display.
It boils down to something like this
import myFunction
class myUi(QtGui.QWidget):
def retranslateUi(self, myUi):
self.option1.clicked.connect(self.option1task)
self.option2.clicked.connect(self.option2task)
self.display.clicked.connect(self.displaytask)
def option1task(self):
#do something
def option2task(self):
#do something
def displaytask(self):
#if option 1 was clicked, run myFunction.option1()
#if option 2 was clicked, run myFunction.option2()
I'm just having trouble making it work. Is there some way to solve it just by passing variables or will I need to use the signal/slot method?
First of all you do not want to react immediately when a radio button is clicked, so you do not need to connect to their clicked signal.
Instead the radio buttons (which are automatically exclusive within the same parent) can be selected by the user and at the moment the display button is clicked, you just read out which of the radio buttons is selected (can be at most one) and do something according to which one is selected.
For four radio buttons you can do that by a if else clause on isChecked() of the buttons.
For larger numbers of button I recommend additionally using a QButtonGroup (documentation) which allowes to assign an integer to each button within the addButton() method and then easily retrieve the integer of the selected button with checkedId(). If no button is selected the return value is -1.
My example (using PySide which is very similar to PyQt):
from PySide import QtGui
def do_something():
id = g.checkedId()
if id == -1:
print('no option selected')
else:
print('selected option {}, read for display'.format(id))
app = QtGui.QApplication([])
w = QtGui.QWidget()
l = QtGui.QVBoxLayout(w)
# three radio buttons
b1 = QtGui.QRadioButton('Option 1')
l.addWidget(b1)
b2 = QtGui.QRadioButton('Option 2')
l.addWidget(b2)
b3 = QtGui.QRadioButton('Option 3')
l.addWidget(b3)
# a button group (mapping from buttons to integers)
g = QtGui.QButtonGroup(w)
g.addButton(b1, 1)
g.addButton(b2, 2)
g.addButton(b3, 3)
# display button
b = QtGui.QPushButton('Display')
b.clicked.connect(do_something)
l.addWidget(b)
w.show()
app.exec_()
And it looks like:
I know in Tkinter that the "<Configure>" event handles size changes in a window. However, I need to distinguish between when a user hits the maximize button, the restore button and when the user is resizing the window (instead of all three at once). Any ideas on how to do this? Is there a standard way? For instance, when a user hits maximize, I want to execute my code to maximize. When the user hits restore, I want to execute different code to restore. When the user drags to resize (or uses the keyboard shortcut to do so) I want it to execute different code altogether.
I can't think of a built-in way to track these events, but you could use the state() method on your root window to track changes. You can check the returned values of state(), specifically normal and zoomed (looks like Windows and OSX only), and call your own methods to handle the resize type based off those values. Here's a example to clarify:
class App(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
# initialize the new_state
self.new_state = 'normal'
self.parent.bind('<Configure>', self._resize_handler)
def _resize_handler(self, event):
self.old_state = self.new_state # assign the old state value
self.new_state = self.parent.state() # get the new state value
if self.new_state == 'zoomed':
print('maximize event')
elif self.new_state == 'normal' and self.old_state == 'zoomed':
print('restore event')
else:
print('dragged resize event')
root = Tk()
App(root).pack()
root.mainloop()
If you want to distinguish between dragging the window and dragging to resize, you'll have to add some extra checks, maybe storing the size before <Configure> and the size after, with winfo_height() and winfo_width(), and if no change occurs, you know the window was only repositioned.
Hope that helps.