How to use a <ComboboxSelected> virtual event with tkinter - python

I am using a tkk.Combobox themed widget in Python 3.5.2. I want an action to happen when a value is selected.
In the Python docs, it says:
The combobox widgets generates a <<ComboboxSelected>> virtual event when the user selects an element from the list of values.
Here on the Stack, there are a number of answers (1, 2, etc) that show how to bind the event:
cbox.bind("<<ComboboxSelected>>", function)
However, I can't make it work. Here's a very simple example demonstrating my non-functioning attempt:
import tkinter as tk
from tkinter import ttk
tkwindow = tk.Tk()
cbox = ttk.Combobox(tkwindow, values=[1,2,3], state='readonly')
cbox.grid(column=0, row=0)
cbox.bind("<<ComboboxSelected>>", print("Selected!"))
tkwindow.mainloop()
I get one instance of "Selected!" immediately when I run this code, even without clicking anything. But nothing happens when I actually select something in the combobox.
I'm using IDLE in Windows 7, in case it makes a difference.
What am I missing?

The problem is not with the event <<ComboboxSelected>>, but the fact that bind function requires a callback as second argument.
When you do:
cbox.bind("<<ComboboxSelected>>", print("Selected!"))
you're basically assigning the result of the call to print("Selected!") as callback.
To solve your problem, you can either simply assign a function object to call whenever the event occurs (option 1, which is the advisable one) or use lambda functions (option 2).
Here's the option 1:
import tkinter as tk
from tkinter import ttk
tkwindow = tk.Tk()
cbox = ttk.Combobox(tkwindow, values=[1,2,3], state='readonly')
cbox.grid(column=0, row=0)
def callback(eventObject):
print(eventObject)
cbox.bind("<<ComboboxSelected>>", callback)
tkwindow.mainloop()
Note the absence of () after callback in: cbox.bind("<<ComboboxSelected>>", callback).
Here's option 2:
import tkinter as tk
from tkinter import ttk
tkwindow = tk.Tk()
cbox = ttk.Combobox(tkwindow, values=[1,2,3], state='readonly')
cbox.grid(column=0, row=0)
cbox.bind("<<ComboboxSelected>>", lambda _ : print("Selected!"))
tkwindow.mainloop()
Check what are lambda functions and how to use them!
Check this article to know more about events and bindings:
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

Thanks you for the posts. I tried *args and it workes with bind and button as well:
import tkinter as tk
from tkinter import ttk
tkwindow = tk.Tk()
cbox = ttk.Combobox(tkwindow, values=[1,2,3], state='readonly')
def callback(*args):
print(eventObject)
cbox.bind("<<ComboboxSelected>>", callback)
btn = ttk.Button(tkwindow, text="Call Callback", command=callback);
tkwindow.mainloop()

Related

Create popups continuously in a loop without blocking the code

My program streams data and I want to create a popup displaying some text whenever a condition is met. I tried to create a simple tkinter window and ctypes window, but both seem to block my code, preventing it from continuing until the window has been closed. How can I create simple popup window functionality in for example a loop?
What I have so far is something of this structure.
import tkinter as tk
for i in range(11):
if i%5 == 0: # Any condition
popup = tk.Tk()
label = ttk.Label(popup, text='hi', font=('Verdana', 12))
label.pack(side='top', padx=10, pady=10)
popup.mainloop()
and
import ctypes
for i in range(11):
if i%5 == 0: # Any condition
popup = ctypes.windll.user32.MessageBoxW
popup(None, 'hi', 'title', 0)
However, in both cases the loop will not proceed until I close the popup.
Case 1 - Tkinter:
You are using mainloop() which is no different than a true while loop. You can make it run continuously by removing it.
import tkinter as tk
from tkinter import ttk
for i in range(11):
if i%5 == 0: # Any condition
popup = tk.Tk()
label = ttk.Label(popup, text='hi', font=('Verdana', 12))
label.pack(side='top', padx=10, pady=10)
Case 2 - Ctypes:
To make it run continuously, you will have to use threading.
import ctypes, threading
for i in range(11):
if i%5 == 0: # Any condition
popup = ctypes.windll.user32.MessageBoxW
threading.Thread(target = lambda :popup(None, 'hi', 'title', 0)).start()
Not too familiar with ctypes, but for tkinter the UI will always be blocking your main code during the mainloop.
You can somewhat bypass it if you just instantiate your Tk() without invoking mainloop and use the after function:
import tkinter as tk
from tkinter.messagebox import showinfo
root = tk.Tk()
root.withdraw()
for i in range(10):
if i in (5, 8):
root.after(ms=1, func=lambda x=i: showinfo('Message!', f'Stopped at {x}'))
# ... do something here
The root.after queues the message box to be displayed after 1 millisecond (ms=1).
A better way might be to create a modeless message/dialog box in ctypes but as mentioned, I'm not too familiar and a quick search didn't yield any simple solution.

Using Python, how do you call a tkinter GUI from another GUI?

I created a couple of GUIs using tkinter. But now I am interested in combining them into one caller GUI. So the caller GUI would have buttons that, when clicked, would open the other GUIs. However, I cannot get it to work. I've done the imports correctly (I think), edited the main functions in the subGUIs, and added the command=GUI.main in my buttons. I get it to load but I get errors about missing files...but when I run a GUI by itself it works fine.
In my research, I read that there can only be one mainloop in a Tkinter program. Basically, I cannot use a Tkinter GUI to call another Tkinter GUI. Do you know what I can do different, for instance, can I create the caller GUI using wxPython and have it call all other GUIs that use Tkinter?
Thank you!
You can't "call" another GUI. If this other GUI creates its own root window and calls mainloop(), your only reasonable option is to spawn a new process. That's a simple solution that requires little work. The two GUIs will be completely independent of each other.
If you have control over the code in both GUIs and you want them to work together, you can make the base class of your GUI a frame rather than a root window, and then you can create as many windows as you want with as many GUIs as you want.
For example, let's start with a simple GUI. Copy the following and put it in a file named GUI1.py:
import tkinter as tk
class GUI(tk.Frame):
def __init__(self, window):
tk.Frame.__init__(self)
label = tk.Label(self, text="Hello from %s" % __file__)
label.pack(padx=20, pady=20)
if __name__ == "__main__":
root = tk.Tk()
gui = GUI(root)
gui.pack(fill="both", expand=True)
tk.mainloop()
You can run that GUI normally with something like python GUI1.py.
Now, make an exact copy of that file and name it GUI2.py. You can also run it in the same manner: python GUI2.py
If you want to make a single program that has both, you can create a third file that looks like this:
import tkinter as tk
import GUI1
import GUI2
# the first gui owns the root window
win1 = tk.Tk()
gui1 = GUI1.GUI(win1)
gui1.pack(fill="both", expand=True)
# the second GUI is in a Toplevel
win2 = tk.Toplevel(win1)
gui2 = GUI2.GUI(win2)
gui2.pack(fill="both", expand=True)
tk.mainloop()
Depending on your OS and window manager, one window might be right on top of the other, so you might need to move it to see both.
Thank you for the ideas. At first, your code wouldn't print the text on the toplevel window. So I edited it a little and it worked! Thank you. GUI1 and GUI2 look like:
import tkinter as tk
def GUI1(Frame):
label = tk.Label(Frame, text="Hello from %s" % __file__)
label.pack(padx=20, pady=20)
return
if __name__ == "__main__":
root = tk.Tk()
GUI1(root)
root.mainloop()
And then the caller looks like this:
from tkinter import *
import GUI1
import GUI2
def call_GUI1():
win1 = Toplevel(root)
GUI1.GUI1(win1)
return
def call_GUI2():
win2 = Toplevel(root)
GUI2.GUI2(win2)
return
# the first gui owns the root window
if __name__ == "__main__":
root = Tk()
root.title('Caller GUI')
root.minsize(720, 600)
button_1 = Button(root, text='Call GUI1', width='20', height='20', command=call_GUI1)
button_1.pack()
button_2 = Button(root, text='Call GUI2', width='20', height='20', command=call_GUI2)
button_2.pack()
root.mainloop()

cmd + a not working in tkinter entry

I've being building a basic UI using Tkinter, and I noticed that cmd + a (or Select all command) is not enabled.
How do I enable all the shortcuts in tkinter especially for entry text field.
This is my code :
entry1 = ttk.Entry(root, width = 60)
entry1.pack()
If tkinter doesn't define the shorcuts you want you can define your own by binding keyboard events.
import tkinter as tk
import tkinter.ttk as ttk
def callback(ev):
ev.widget.select_range(0, 'end')
root = tk.Tk()
entry = ttk.Entry(root)
entry.pack()
entry.bind('<Command-a>', callback)
root.mainloop()
I think Command is the correct prefix for the cmd key but I don't have a mac to test. In windows it binds to the control key.
#Goyo already answered your question. I want to share my contribution as I do not see interest in selecting the text of the Entry widget's text and not doing anything else with it. So I am going to provide you a dirty MCVE to show how you are going to use the selected text: a) either you will delete it or b) you will copy it.
For a), the following function will do the job:
def select_text_or_select_and_copy_text(e):
e.widget.select_range(0, 'end')
It will work under the condition you bind the corresponding events described by the function's name to the entry widget:
entry.bind('<Control-a>', select_text_or_select_and_copy_text)
entry.bind('<Control-c>', select_text_or_select_and_copy_text)
For b), you can use this function:
def delete_text(e):
e.widget.delete('0', 'end')
And bind the Delete event to the entry widget:
entry.bind('<Delete>', delete_text)
I tried this MCVE on Ubuntu and it works:
import tkinter as tk
import tkinter.ttk as ttk
def select_text_or_select_and_copy_text(e):
e.widget.select_range(0, 'end')
def delete_text(e):
e.widget.delete('0', 'end')
root = tk.Tk()
entry = ttk.Entry(root)
entry.pack()
entry.bind('<Control-a>', select_text_or_select_and_copy_text)
entry.bind('<Control-c>', select_text_or_select_and_copy_text)
entry.bind('<Delete>', delete_text)
root.mainloop()

Python 2.7 Tkinter open webbrowser on click

from Tkinter import *
import webbrowser
root = Tk()
frame = Frame(root)
frame.pack()
url = 'http://www.sampleurl.com'
def OpenUrl(url):
webbrowser.open_new(url)
button = Button(frame, text="CLICK", command=OpenUrl(url))
button.pack()
root.mainloop()
My goal is to open a URL when I click the button in the GUI widget. However, I am not sure how to do this.
Python opens two new windows when I run the script without clicking
anything. Additionally, nothing happens when I click the button.
You should use
button = Button(root, text="CLCK", command=lambda aurl=url:OpenUrl(aurl))
this is the correct way of sending a callback when arguments are required.
From here:
A common beginner’s mistake is to call the callback function when
constructing the widget. That is, instead of giving just the
function’s name (e.g. “callback”), the programmer adds parentheses and
argument values to the function:
If you do this, Python will call the callback function before creating
the widget, and pass the function’s return value to Tkinter. Tkinter
then attempts to convert the return value to a string, and tells Tk to
call a function with that name when the button is activated. This is
probably not what you wanted.
For simple cases like this, you can use a lambda expression as a link
between Tkinter and the callback function:
Alternatively, you don't have to pass the URL as an argument of the command. Obviously your OpenUrl method would be stuck opening that one URL in this case, but it would work.
from Tkinter import *
import webbrowser
url = 'http://www.sampleurl.com'
root = Tk()
frame = Frame(root)
frame.pack()
def OpenUrl():
webbrowser.open_new(url)
button = Button(frame, text="CLICK", command=OpenUrl)
button.pack()
root.mainloop()

Tkinter bind problem

I have something like this:
from Tkinter import *
root = Tk()
root.title("Test")
def _quit():
root.destroy()
m = Menu(root)
root.config(menu=m)
fm = Menu(m, tearoff=0)
m.add_cascade(label="File", menu=fm)
fm.add_command(label="Quit", command=_quit, accelerator='Ctrl+Q')
root.bind('<Control-Q>', _quit())
root.bind('<Control-q>', _quit())
root.mainloop()
My question is:
"Why _quit() always is being called?"
When you are binding with Tkinter you typically do not call the function you wish to bind.
You're supposed to use the line
root.bind('<Control-Q>', _quit)
instead of
root.bind('<Control-Q>', _quit())
Take note of the lack of parentheses behind _quit.
This code below should work.
from Tkinter import *
root = Tk()
root.title("Test")
def _quit(event):
root.destroy()
m = Menu(root)
root.config(menu=m)
fm = Menu(m, tearoff=0)
m.add_cascade(label="File", menu=fm)
fm.add_command(label="Quit", command=lambda: _quit(None), accelerator='Ctrl+Q')
root.bind('<Control-Q>', _quit)
root.bind('<Control-q>', _quit)
root.mainloop()
EDIT:
Oops sorry, I only ran the code testing the keyword command for quit in the menu bar. Not the bound key commands. When doing bindings for Tkinter and I'm pretty sure most GUI toolkits, the binding inserts and event argument when the function is called. However the Tkinter command keyword argument does not typicaly insert an event. So you have to compromise by having the command keyword argument "artificially" insert an event argument of None (lambda: _quit(None)). This allows you to use one function in both scenarios.
Because you're calling it. Don't call it:
root.bind('<Control-Q>', _quit)

Categories

Resources