What is the best way to change the state of a tkinter menu or its children after the mainloop?
Thanks
You configure it the way you configure all widgets, with the configure method:
widget.configure(state="disabled")
If you want to disable one of the items on the menu, use entryconfig:
widget.entryconfig(0, state="disabled")
Related
There are multiple entry widgets in my tkinter gui. So I want to use a single function to enter data in the entry widget currently in focus with the help of a keypad of buttons. How can I do it?
You can use root.focus_get() to get the current focused widget, derive StringVar/IntVar and update them properly, or just use entry.insert('end','0') for example.
I have a checkbutton inside of a menu widget in python with tkinter. (Using python 3.5.2). I know that with normal checkbuttons you can select or deselect the checkbuttons using checkbutton.select() and checkbutton.deselect(). I need to know how to do this with the checkbuttons that I have in the menu object.
I have tried the menu.entrybutton.configure(id, coption) method but there is no coption for selecting and deselecting checkbuttons within the menu.
Any help would be appreciated.
You should assign an IntVar (or possibly StringVar) to the checkbutton when you create it, via its variable= configuration option. You call .get() on this var to check the button's state, and .set() to change its state.
I have multiple buttons in my tkinter 8.5 GUI (on Windows 7). I want whatever button is focused on (tabbed over) to be selected when the user hits Enter. I know I have to bind '<Return>', but I need the rest of the gaps filled in.
Thanks in advance!
Assuming you want this to be universal to all applications in the root window you could do something similar to this.
def clickButton():
widget = root.focus_get()
if widget != root:
widget.invoke()
root = Tkinter.Tk()
root.bind("<Return>", clickButton)
root.mainloop()
That will run any command associated with the currently tabbed selection. If you want to limit it to certain buttons you can do type-checking inside of the method. Widget will be whatever widget is currently in focus via the tabbed selection. Also beware of a user hitting enter on certain widgets that may not support the invoke method.
I am designing a GUI using Python and Tkinter. All the buttons and entries required to register the user input commands are placed inside a main frame and are their child widgets.
I want to know if it is possible to disable all the input functionality from these widgets by propagating some "disable" flag from the main frame to all the input widgets.
In this way, I was hoping to be able to toggle their state in a single line of code.
I believe that should be possible. Does anyone know how to do this?
Tk widgets have a state configuration option that can be either normal or disabled. So you can set all children of a frame to disabled using the winfo_children method on the frame to iterate over them. For instance:
for w in app.winfo_children():
w.configure(state="disabled")
Ttk widgets have state method which might require alternative handling. You may also want to set the takefocus option to False as well although I think that disabled widgets are automatically skipped when moving the focus (eg: by hitting the Tab key).
Edit
You can use the winfo_children and winfo_parent methods to walk the widget tree in both directions if necessary to access widgets contained in child frames for instance. For example, a simple function to visit each child of a root widget:
def visit_widgets(root, visitor):
visitor(root)
for child in root.winfo_children():
visit_widgets(child, visitor)
from __future__ import print_function
visit_widgets(app, lambda w: print(str(w)))
I know how to create and set menus for tkinter Toplevel windows, but I'm struggling to find any information on how to get a window's menu bar. What I'd like to do is dynamically add options to the menu, so I need to do something like:
menubar = self.getMenu()
menubar.add_cascade(...)
Where self is a Toplevel window. Thanks!
You want to use the cget method, which can be used to get any of the configured options:
menu = self.cget("menu")