tkinter optionmenu close event - python

I am trying to display a tooltip for optionmenu items in tkinter. I have figured out how to do that. The problem I am left with is closing the tooltip (toplevel) window. I want to do so when the user selects an option or clicks outside the optionmenu (which closes the menu of the option menu). The enter and leave events don't seem to be doing the trick. Any help would be appreciated!
Edit: I have added some code on what I have tried. The "test" print occurs whenever I hover over menu item 1 (which is what I want) but I want to fire enter and leave events whenever the user opens or closes the menu as a whole. I tried binding them to the optionmenu but it is unreliable and sometimes triggers leave events when it shouldn't.
from tkinter import Tk, Frame, BOTH, Menu, Label, SUNKEN, X, BOTTOM
import tkinter as tk
class Application(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, background = "white")
self.parent = parent
vals = ["1","2","3"]
var = tk.StringVar()
var.set("1")
option = tk.OptionMenu(root,var,*vals)
option.pack()
t = option.children["menu"]
tk.Menu
#Do I need to unbind
#t.bind("<<MenuSelect>>", self.test_func)
t.bind("<<MenuSelect>>", self.test_func)
t.bind("<Enter>",self.enter_func)
t.bind("<Leave>",self.leave_func)
def test_func(self,event = None):
if self.parent.call(event.widget,"index","active") == 0:
print("test")
def enter_func(self,event=None):
print("entered")
def leave_func(self,event=None):
print("left")
root = tk.Tk()
Application(root)
root.mainloop()
One way I have found to close the tooltip (not shown in example) is to trace the variable associated with the optionmenu and when it changes kill the topwindow. However, if the user opens the menu, hovers over options, then clicks outside the menu items, the topwindow tooltip stays because the menu item never changed.
The question is very similar to this one

Related

tkinter: Unbind not working, even workarounds don't work

I bring up here a problem, that's been there for ages, but is obviously still not solved and older workarounds don't work on my Python 3.7.2 (64-bit on Win10).
I have this code:
import tkinter as tk
import tkinter.simpledialog
# message box to enter a value where to set the scale to
class EnterValueBox(tk.simpledialog.Dialog):
def body(self, master):
self.e = tk.Entry(self, width=10)
self.e.pack(pady=5)
return self.e # initial focus
def apply(self):
print(self.e.get())
# callback to open message box
def enterValue(event):
EnterValueBox(root, title="Enter Value 0..100")
# create window with scale widget
root = tk.Tk()
scale = tk.Scale(root, orient=tk.HORIZONTAL, from_=0, to=100)
scale.pack()
# unbind any button-3 events
scale.unbind("<ButtonPress-3>")
scale.unbind("<ButtonRelease-3>")
scale.unbind("<Button-3>")
# bind button-3 press event to open message box
scale.bind("<ButtonPress-3>", enterValue)
tk.mainloop()
It creates a window with a single scale widget. I want to bind ButtonPress-3 to open a little dialog to directly enter a new value. The code only prints that value to the shell, but the example shows, that the unbind is not working, because after printing the value, the dialog box is closed (when the user clicks OK) and then the default binding is executed, which sets the slider, where the user clicked in the trough of the slider widget.
I tried the workaround from Deleting and changing a tkinter event binding with a PatchedScale widget (instead of the PatchedCanvas shown there), but that didn't make any difference.
Any help would be greatly appreciated!
The default bindings are not on the widget, they are on the widget class. Calling unbind on a widget for which there is no widget-specific binding won't have any effect.
If you don't want the default binding to run after your widget-specific binding, the normal technique is to have your bound function return the string break.
def enterValue(event):
EnterValueBox(root, title="Enter Value 0..100")
return "break"

How to sound a bell when the user clicks outside a modal window?

The situation is simple. I have a main window with a Help - About menu.
When this menu item is clicked, a modal window is opened (let's say it's an About-window).
With self.grab_set() I disabled the main-window (although the modal window does flicker when you click the main title bar).
So far, so good.
Here is the question: I really like to sound a bell when the user clicks outside the modal window on the main window.
This is what I could find about grab_set(), really not that much:
[effbot] ...a method called grab_set, which makes sure that no mouse or keyboard
events are sent to the wrong window.
[effbot] Routes all events for this application to this widget.
[kite.com] A grab directs all events to this and descendant widgets in the application.
[google books] grab_set() ensures that all of the application's events are sent to w until a corresponding call is made to grab_release ([Me:] or till the window is destroyed?)
I'm not quite sure how to understand this: does it mean you can handle an event on the main window within the modal window (like sounding my bell)?
So I tried things like:
self.bind('<Button-1>', self.bell) Exception in Tkinter callback: _tkinter.TclError: bad window path name
parent.bind('<Button-1>', self.bell) Nothing happens
So, how to sound a bell like when clicked outside the modal window on the main window, like in so many other applications?
Derived questions:
Is it still possible to cature events from the main window after using
grab_set for the modal window?
Is there a way to prevent the flickering?
I really like to understand this mysterious grab_set() method.
Stripped code:
import tkinter as tk
class About(tk.Toplevel):
def __init__(self, parent):
tk.Toplevel.__init__(self, parent)
self.geometry('200x150')
#--- OK button
btn_ok = tk.Button(self, text='OK', command=self.destroy) # destroy with OK
btn_ok.pack(side=tk.TOP)
btn_ok.focus() # destroy with spacebar
#--- Make window modal
self.grab_set()
# self.wait_window() # is this necessary in this case?
# self.bind('<Button-1>', self.bell) ??? The question
class MenuBar(tk.Menu):
def __init__(self, parent):
tk.Menu.__init__(self)
helpmenu = tk.Menu(self, tearoff=0)
helpmenu.add_command(label='About', command=lambda: About(parent))
self.add_cascade(label='Help', menu=helpmenu)
class MainApp():
def __init__(self, parent):
parent.configure(background='#000000')
parent.geometry('800x600')
menubar = MenuBar(parent)
parent.configure(menu=menubar)
if __name__ == '__main__':
root = tk.Tk()
MainApp(root)
root.mainloop()
When you set a grab, all button clicks will go to the window with the grab. You capture them the way you capture any other event. In the case of a button click you do that by binding a function to <1>.
It's important to know that a binding on a root window or a Toplevel window will apply to all widgets in that window. For example, binding to self in your code will fire even when you click on the "Ok" button. Therefore, the callback should probably only do work when the widget associated with the event is the same as the toplevel.
Example:
class About(tk.Toplevel):
def __init__(self, parent):
...
self.bind("<1>", self.capture_click)
...
def capture_click(self, event):
if event.widget == self:
<your logic here>
In the case of wanting to know if the user clicked outside the window, you can use the coordinates of the event object to compare against the window to see if the click is inside or outside.
def on_click(self, event):
if event.widget == self:
if (event.x < 0 or event.x > self.winfo_width() or
event.y < 0 or event.y > self.winfo_height()):
self.bell()
I found a second solution. Though my question was explicitly about using grab_set(), this method does the same for me: making the window as modal as possible and sound a bell.
Instead of using self.grab(), you can also disable the parent window:
parent.attributes('-disabled', True)
Of course it needs to be enabled again when the OK button is clicked (and when the windows is closed with the [X] close control. However, my original About-window has no window decoration). The command for the OK-button becomes:
btn_ok = tk.Button(self, text='OK', command=lambda: self.closeme(parent))
...which calls the closeme function:
def closeme(self, parent):
parent.attributes('-disabled', False)
self.destroy()
The bell sounds automatically when clicking a disabled window.
Method 1: Keeps you in full control of the main window but does not 'freeze' the main window: you can still move it around.
Method 2: Completely freezes the main window, but if it happens to be (partially) covered by another window (not of this application), then you can only bring back to top using Alt+Tab (windows).
I'm sure I will use both techniques in the future depending on my needs.

Python/Tkinter OptionMenu Update creates a "Shadow" of the Menu

So I'm still quite new to this (my code should make this obvious) and I am working on a GUI with tkinter.
I am trying to have an OptionMenu which shows Keys from a dict and and upon clicking on a key I would like to see the value.
I want to modify that dict and would like to be able to update said OptionMenu.
So far, so good. Now I've been able to get this to "work" - but when I update the menu (after changing it or not) I get a shadow of the menu itself.
I've conconcted a small test program:
import tkinter as tk
from tkinter import ttk
class MyApp():
def __init__(self,master):
self.master = master
self.myDict = {'Key1':1, 'Key2': 2, 'Key3':3}
self.valueVar = tk.StringVar()
self.valueVar.set("0.00")
self.RCS = tk.Label(master, textvariable=self.valueVar).grid(row=5, column=3)
updateButton = tk.Button(text= "Update List", command = self.update)
updateButton.grid(row=4,column=4)
changeButton = tk.Button(text= "Change list", command = self.changeDict)
changeButton.grid(row=5,column=4)
self.keyVar = tk.StringVar(master)
self.om = ttk.OptionMenu(self.master, self.keyVar, "Select Key ", *self.myDict, command = self.setKey )
self.om.configure(width=20)
self.om.grid(row=4, column=3)
def setKey(self,Surface):
self.valueVar.set(self.myDict[Surface])
def update(self):
menu = self.om["menu"]
menu.delete(0,"end")
menu.destroy
menu = ttk.OptionMenu(self.master, self.keyVar, "Select Key", *self.myDict, command = self.setKey )
menu.grid(row=4, column=3)
def changeDict(self):
self.myDict = {'Key4':4, 'Key5': 5, 'Key6':6}
root = tk.Tk()
app = MyApp(root)
root.mainloop()
What do I have to change? Why?
Usually I work with Matlab. I guess it shows.
Much appreciated!
As far as I understand this mini-program you which for it to display an optionmenu with the keys of a dict and then when you press "change dict" and then update it should switch the optionmenu to the other set of items? In this case, you were calling the destroy on the wrong widget. The only issue was with the update function which should be changed to:
def update(self):
#menu = self.om["menu"]
#menu.delete(0,"end")
#The above two menu items are not neede dif you are just going to destroy the widget
self.om.destroy()
self.om = ttk.OptionMenu(self.master, self.keyVar, "Select Key", *self.myDict, command = self.setKey )
self.om.configure(width=20)
self.om.grid(row=4, column=3)
This will do what I think you want it to do. Just so you know, the optionmenu widget is actually a combination of a button and a menu widget. So when you do menu = self.om["menu"], you are actually getting the menu object of the optionmenu widget and then destroying that. Then you are replacing that variable with an optionmenu and losing the original menu whilst not destroying the original optionmenu (self.om). This is why you got the shadow. Some other notes though:
There is no need for the user to have to press 2 buttons when switching lists - call the update function from the changedict function so that it automatically populates.
You can iterate over the menu items as you began to do but you need to decide on one or the other. I can't comment on the efficiency of completely destroying and enabling the widget or changing the menu items of the current widget as I am still rather experienced.
Hopefully that helps!

Tkinter, show Calendar in Toplevel window on button click

I deleted my previous question about this in order to simplify my question and communicate the question clearer. I've got a project with multiple classes inside of it and I'd like to get a Calendar to display in a new window once I click a button. I am currently using this Calendar script with a minor change inside of my overall script. I changed Frame to Toplevel in the first part of the Calendar script like this:
class Calendar(tk.Toplevel):
def __init__(self, parent, **kw):
Toplevel.__init__(self, parent, **kw)
Now this does create the Calendar in a Toplevel window along with the rest of my script but it does it as soon as the program is started. I want to get it to show when it is called later on by the user.
example:
class Application(tk.Tk): # tk.Tk creates main window
def __init__(self):
tk.Tk.__init__(self)
self.title("T")
self.geometry('550x320')#x,y
self.create_options()
self.calendar = Calendar(self)
def create_options(self):
self.widgets = tk.Frame(self)
tk.Button(self,
text = "...", command=self.show_Calendar
).place(x=525, y=130)
which would call this:
def show_Calendar(self):
'''shows calendar'''
toplevel = Toplevel()
toplevel.Calendar.place(x=0, y=0)
The button does create a window but there is nothing in it. What would be the best way to get this Calendar to only show in the window that appears when the button is clicked?
self.calendar = Calendar(self)
Putting this line within your application init will create it at the same time that the application is created. You would want to move this into your show_Calendar method.
def show_Calendar(self):
'''shows calendar'''
toplevel = Toplevel()
toplevel.Calendar.place(x=0, y=0)
toplevel = Toplevel() does not make any sense here. You are creating a blank Toplevel and making it a local variable. This Toplevel is not related to your Calendar in any way.
Within the Calendar script, you made sure that the Calendar class inherits from Toplevel, so any time you create a Calendar, it will be attached to its own Toplevel.
def show_Calendar(self):
'''shows calendar'''
self.calendar = Calendar(self)
I was looking at your previous question before you deleted it, and if you would also like to remove the calendar when the user changes focus, you should look into Events and Bindings here, specifically <FocusOut>.

Python tkinter to modify only listbox with focus

Good day,
I have a python application that produces multiple listboxes each with it's own list of data. These listboxes are created dynamically according to the length of a user generated list.
I have a button that when clicked i want to trigger some code to effect the active listbox (removing the value from the list amongst other things).
So my plan is to iterate through all the listboxes and only delve deeper if the list box has focus. But alas, after 2-3 hours of peeling through questions and tkinter documentation I cannot find any way to determine if something has focus or not.
Thanks in advance!
Widgets are capable of emitting <FocusIn> and <FocusOut> events, so you can bind callbacks in order to manually keep track of which listbox has focus. Example:
from Tkinter import *
class App(Tk):
def __init__(self, *args, **kargs):
Tk.__init__(self, *args, **kargs)
self.focused_box = None
for i in range(4):
box = Listbox(self)
box.pack()
box.insert(END, "box item #1")
box.bind("<FocusIn>", self.box_focused)
box.bind("<FocusOut>", self.box_unfocused)
button = Button(text="add item to list", command=self.add_clicked)
button.pack()
#called when a listbox gains focus
def box_focused(self, event):
self.focused_box = event.widget
#called when a listbox loses focus
def box_unfocused(self, event):
self.focused_box = None
#called when the user clicks the "add item to list" button
def add_clicked(self):
if not self.focused_box: return
self.focused_box.insert(END, "another item")
App().mainloop()
Here, clicking the button will add "another item" to whichever listbox has focus.

Categories