I am making an application where I want to create a dropdown menu if anyone clicks on the Qaction.
My code
self.navtb = QToolBar("Navigation")
self.navtb.setIconSize(QSize(25, 25))
self.navtb.setMovable(False)
self.addToolBar(self.navtb)
option_btn = QAction(QIcon(os.path.join('images', 'options.png')), "Option", self)
self.navtb.addAction(option_btn)
A QAction can have an associated menu which you can simply set with setMenu(). This menu will pop up (drop down) on click+hold.
Now all you need is to set your button to directly go for the menu on click by altering its popup mode. Note that while the wording pop up is used, the menu will be a proper drop-down menu in a toolbar scenario.
In your example it would roughly translate to:
option_btn.setMenu(...)
self.navtb.widgetForAction(option_btn).setPopupMode(QToolButton.InstantPopup)
For reference, this is how I do it in my code in C++:
// initialize compute menu and let button display menu without holding mouse
actionComputeDisplay->setMenu(new QMenu(widget));
auto btn = qobject_cast<QToolButton*>(toolBar->widgetForAction(actionComputeDisplay));
btn->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup);
Related
I just started learning pyqt5 qt designer. I used to use tkinter. I have a problem, and I can't figure out how to do it. As the picture shows, there are QCheckBox and QLineEdit. Keep the LineEdit field 'disabled' when checkbox is not checked; When the CheckBox is checked, I try to get the LineEdit field to be activated in the 'normal' position.
when i use tkinter,
txtSample.configure(state=DISABLED)
txtSample = Checkbutton( text='TEST', variable=var, onvalue=1, offvalue=0,font('arial',16,'bold'))
txtSample = Entry(font=('arial',16,'bold'), bd=8, width=6, justify='left',state=DISABLED)
def checkbuttonSample():
if(var.get() == 1):
txtSample.configure(state=NORMAL)
elif var.get()== 0:
txtSample.configure(state=DISABLED)`
I would use similar code like this.But I couldn't find a solution and I am looking for a solution about this issue. Thank you for your answers in advance.
From code it's pretty simple: you need to connect the toggled signal of the checkbox to the setEnabled() slot of the line edit:
self.lineEdit.setEnabled(False)
self.checkBox.toggled.connect(self.lineEdit.setEnabled)
You can also do it directly in Designer:
select the line edit;
on the Property Editor panel, uncheck the "Enabled" option;
enter the "Edit Signals/Slots" mode (from the tool bar or the "Edit" menu);
press the left mouse button on the checkbox, move the mouse on the line edit, then release the button;
in the "Configure Connection" dialog that will be opened afterwards, select the "toggled(bool)" signal on the left, check the "Show signals and slots inherited from QWidget" option, and select "setEnabled(bool)" slot, then click "Ok";
How to add an 'about' button to the menu bar of your main window - which when clicked directly opens a dialog with some about text - using PyQT?
Or is that impossible?
Having had a look at the documentation/question and answers online having to do with the menu bar, I get the impression that the QMenuBar only supports triggering events via 'QAction's via menu drop-downs. However I dont want a drop-down for the about button but rather would like it to trigger some showAboutDialog method.
If you have any ideas/links please let me know.
You can add a QAction object directly to the menubar of your MainWindow. Use the QMenuBar.addAction() method for this:
class YourMainWindow(QMainWindow):
def __init__(self):
super().__init__()
menu = QMenuBar()
menu.addAction(show_about_dialog_action)
self.setMenuBar(menu)
Situation: When I use the mouse button to click the "down-arrow" of a ttk.Combobox it's standard behaviour is to show a dropdown list. When the down arrow is clicked on the second time, the combobox dropdown list will become hidden.
Using the keyboard. it is possible to show the combobox dropdown list by pressing the "down-arrow" once. Pressing the "down-arrow" further will scroll down the dropdown list to its end. Pressing the "up-arrow" repeatedly will scroll up the dropdown list until the highlight/selection reaches to the top of the dropdown list, but it does not finally hide the dropdown list.
Question: Without using the mouse or keyboard, that is, using computer programming, how can I hide an expose dropdown list of a ttk.Combobox. I am aware that the w.event_generate("<Down>") command can be used to program a ttk.Combobox to show it's dropdown list. But how do I achieve the opposite? That is, how can I use the same w.event_generate() command to hide the dropdown list? Or what other tkinter command should I use to achieve what I want?
I made several attempts at this question and finally found a way to hide the combobox droplist via programming. My code is shown below.
OBSERVATIONS:
Using "combobox_widget_object.event_generate('<Button-1>')" can
cause the combobox dropdown list to show. Event '<Button-1>' appears to be
inherently defined to cause this behavior.
Running 2 of this command back to back do not lead to the showing
and hiding of the combobox dropdown list. It still only SHOWS the dropdown
list as with a single command.
The "combobox_widget_object.after(delay_ms, callback=None, *args)"
method can be used to instruct the combobox to run a function
after certain time delays. That function should contain the
"combobox_widget_object.event_generate('<Button-1>')" method to cause the
hiding of the dropdown list.
CODE:
# tkinter modules
import tkinter as tk
import tkinter.ttk as ttk
"""
Aim:
Create a combobox widget and use w.event_generate(sequence, sequence,**kw) to
simulate external stimuli to cause combobox dropdown list to show and hide.
Author: Sun Bear
Date: 16/01/2017
"""
# Function to activate combobox's '<Button-1>' event
def _source_delayed_clicked():
print ('\n def __source_delayed_clicked():')
print('Delayed 2nd simulation of external stimuli')
print('HIDE combobox Dropdown list. \n'
'IT WORKED!')
source.event_generate('<Button-1>')
root = tk.Tk()
source_var=tk.StringVar()
reference=['Peter', 'Scotty', 'Walter', 'Scott', 'Mary', 'Sarah']
# Create Main Frame in root
frame0 = ttk.Frame(root, borderwidth=10, relief=tk.RAISED)
frame0.grid(row=0, column=0, sticky='nsew')
# Create Combobox
source = ttk.Combobox(frame0, textvariable=source_var, values=reference)
source.grid(row=0, column=0, sticky='nsew')
# Simulate external stimuli using w.event_generate(sequence,**kw)
print('\n', '1st simulation of external stimuli using: \n'
' source.event_generate('"<Button-1>"') \n'
' SHOW Combobox Dropdown List.')
source.event_generate('<Button-1>')
#source.event_generate('<Button-1>') # running another similar command
# back to back didn't work
delay = 1000*6 # 6 seconds delay
source.after(delay, _source_delayed_clicked)
Update:
Alternatively, to hide the combobox dropdown list, the command
source.event_generate('<Escape>') can be used in place of the source.event_generate('<Button-1>') command defined in the function def _source_delayed_clicked(). This simulates pressing the keyboard "Esc" key.
From what i understood from the internet resources, I could create a Popup menu of QActions on the Qtoolbar by using Qtoolbuttonpopup mode.
So, I created a QMenu and added a few QActions to it by using QMenu.addAction.
After that i have created a QToolButton and set the ToolButtonPopupMode to 2. Followed by setting the QMenu i have created above as the menu for it by using .setMenu(QMenu)
SettingMenu = QtGui.QMenu()
SettingMenu.addAction(Action1)
SettingMenu.addAction(Action2)
SettingButton = QtGui.QToolButton()
SettingButton.setIcon(QtGui.QIcon(QtGui.QPixmap(':/setting.png')))
SettingButton.ToolButtonPopupMode(2)
SettingButton.setMenu(SettingMenu)
from the above code, i am expecting to have a Qtoolbutton on my toolbar and when i click on it, it should pop up a menu with 2 Actions. But when i run the code, all i see is a Qtoolbutton on my toolbar but when i click the Qtoolbutton it doesn't create any pop up menu.
Am i doing this wrong? How do i create a toolbutton that create a pop up menu of actions upon user click?
ToolButtonPopupMode is an enumerating type. All the values in that enum are instances of that type. Because it inherits from int, calling it with an integer returns the same integer. However, you want to set the popupMode property, so use setPopupMode(2).
I'm trying to make a Tkinter menu that is like a taskbar checker.
So if I go to this menu and check a box, a specific button then appears on my window, and then the user can select multiple buttons based on what they want.
The program is just a bunch of buttons that after entering text in my text field, and clicking the button, a web browser launches with a search of the website that the button is linked to.
How can I make a menu like I mentioned above?
Edit:
I've just tried basic menu stuff:
buttonmenu = Menu(menubar, tearoff=0)
buttonmenu.add_command(label="button1", command=turnbuttononoff)
buttonmenu.add_command(label="button2", command=turnbuttononoff)
buttonmenu.add_command(label="button3", command=turnbuttononoff)
buttonmenu.add_command(label="button4", command=turnbuttononoff)
buttonmenu.add_command(label="button5", command=turnbuttononoff)
This just creates a basic menu. And if I could have a function that triggers a button to be turned on or off that would be great.
So essentially just a function to swap a button from being shown to not being shown
def turnbuttononoff():
#togglebutton here
ANSWER:
I made a dictionary of the data of where each button was stored, and then checked to see if the button was active, and if it was, turned it off, and if it was inactive, turn it off.
Making this a command lambda function for each button works.
def Toggle_Button(myButton):
if myButton.winfo_ismapped()==1:
myButton.grid_forget()
else:
myButton.grid(row=gridData[myButton][0],column=gridData[myButton][1])
gridData = {}
gridData[button] = [row,col]
def Toggle_Button(myButton):
if myButton.winfo_ismapped()==1:
myButton.grid_forget()
else:
myButton.grid(row=gridData[myButton][0],column=gridData[myButton][1])
If you already have buttons on a grid, use button.grid_info to find what you need, it returns a dictionary.