Unable to unbind a function using tkinter - python

I am working with Tkinter in Python 3.5 and I encounter a weird problem.
I used the tkinterbook about events and bindings to write this simple example:
from tkinter import *
root = Tk()
frame = Frame(root, width=100, height=100)
def callback(event):
print("clicked at", event.x, event.y)
# frame.unbind("<Button-1>", callback)
frame.bind("<Button-1>", callback)
frame.pack()
root.mainloop()
It works fine, but if I try to unbind the callback (just uncomment the line), it fails with the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Delgan\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Users\Delgan\Desktop\Test\test.py", line 9, in callback
frame.unbind("<Button-1>", callback)
File "C:\Users\Delgan\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1105, in unbind
self.deletecommand(funcid)
File "C:\Users\Delgan\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 441, in deletecommand
self.tk.deletecommand(name)
TypeError: deletecommand() argument must be str, not function
This is not clear, I am not sure if it is a bug in tkinter or if I am doing something wrong.
frame.unbind("<Button-1>") works fine but I would like to remove this exact callback instead of a global remove.

The second argument to unbind is a 'funcid', not a function. help(root.unbind) returns
unbind(sequence, funcid=None) method of tkinter.Tk instance.
Unbind for this widget for event SEQUENCE the function identified with FUNCID.
Many tk functions return tk object ids that can be arguments for other funtions, and bind is one of them.
>>> i = root.bind('<Button-1>', int)
>>> i
'1733092354312int'
>>> root.unbind('<Button-1>', i) # Specific binding removed.
Buried in the output from help(root.bind) is this: "Bind will return an identifier to allow deletion of the bound function with unbind without memory leak."

Related

Tkinter button bind throws extra argument error

I am trying to write tkinter code that calls a function either when a button is clicked or when enter is clicked when the button is in focus. I have the following code:
import tkinter as tk
m = tk.Tk(className="My window")
def callback():
print("Ice cream weather")
butt = tk.Button(m, text="My button", width=25, command=callback)
butt.grid()
butt.focus_set()
butt.bind('<Return>', callback)
m.mainloop()
But when I run it and press Enter, I get the following error:
Traceback (most recent call last):
File "C:\Users\chiller\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
TypeError: callback() takes 0 positional arguments but 1 was given
I found a solution, which I posted below, but I am wondering if there is a simpler or neater fix.
A way to solve this is to set the function up to take in a dummy parameter, and set a default value for when it isn't passed (i.e. for button click, which passes 0 arguments). This is just changing the line
def callback():
To
def callback(pointless=None):
This gives the extra parameter from pressing Enter a place to go, while not requiring it for a button push.

_tkinter.TclError: bad window path name

I am making an application that involves tkinter, and will eventually involve sockets. My code currently consists of this:
import tkinter as tk # imports tkinter module
from tkinter import * # imports tkinter module
a=tk.Tk()
a.title('Custodian Alert Tool')
List=['','','']
List.clear()
def Clear():
for widget in a.winfo_children(): # clears the window for the next Page
widget.destroy()
def Home():
Clear()
Label(a,text='Welcome to the Custodian Alert Tool.').pack()
Label(a,text='').pack()
SubmitButton=tk.Button(a,text='Submit A Ticket',command=Submit)
SubmitButton.pack()
ExitButton=tk.Button(a,text='Exit',command=Exit)
ExitButton.pack()
a.mainloop()
def Submit():
Clear()
def Append1(): # the button calls this function when pressed
List.append(x.get())
Gender()
Label(a,text='Which bathroom are you reporting for?').pack()
f=tk.Frame(a)
f.pack()
x=tk.StringVar()
E=tk.Entry(f,textvariable=x)
E.grid(row=1,column=1)
b1=tk.Button(f,text='Submit',command=Append1) # the error occurs after I click this button
b1.grid(row=1,column=2)
def Gender():
Clear()
def Append2(y):
List.append(y)
Issue()
Label(a,text='Boys or Girls?').pack()
f=tk.Frame(a)
f.pack()
b1=tk.Button(f,text='Boys',command=Append2('Boys'))
b1.grid(row=1,column=1)
b2=tk.Button(f,text='Girls',command=Append2('Girls'))
b2.grid(row=1,column=2)
def Issue():
Clear()
def Exit():
a.destroy()
Home()
When I click the b1 button under the Submit function, however, I get this:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__
return self.func(*args)
File "/Users/skor8427/Desktop/AlertClient.py", line 27, in Append1
Gender()
File "/Users/skor8427/Desktop/AlertClient.py", line 45, in Gender
b1=tk.Button(f,text='Boys',command=Append2('Boys'))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 2209, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 2139, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: bad window path name ".4610182280"
This is the biggest error I have ever gotten and I have no clue where to begin. Anyone know what I can do?
When you do this:
b1=tk.Button(f,text='Boys',command=Append2('Boys'))
It behaves exactly the same as this:
result = Append2('Boys'))
b1=tk.Button(f,text='Boys',command=result)
When Append2 is called, it calls Issue which calls Clear which destroys all of the children in a. f is in a so f gets destroyed. That means that you're trying to create b1 as a child of a widget that has been destroyed. And that is why you get the error "bad window path name" -- that cryptic string is the name of the widget that has been destroyed.
You need to modify the construction of b1 to be something like this:
b1 = Button(f, text='Boys', command=lambda: Append2('Boys'))`
That will defer the calling of Append2 until the button is clicked.

Tkinter -tkinter.TclError

I have the following code:
from Tkinter import *
from urllib import urlretrieve
import webbrowser
import ttk
def get_latest_launcher():
webbrowser.open("johndoe.com/get_latest")
global percent
percent = 0
def report(count, blockSize, totalSize):
percent += int(count*blockSize*100/totalSize)
homepage = "http://Johndoe.com"
root = Tk()
root.title("Future of Wars launcher")
Button(text="get latest version", command=get_latest_launcher).pack()
global mpb
mpb = ttk.Progressbar(root, orient="horizontal", variable = percent,length="100",
mode="determinate")
mpb.pack()
root.mainloop()
urlretrieve("https://~url~to~my~file.com",
"Smyprogram.exe",reporthook=report)
however, if I run this script, it won't display the progressbar, and it will only display the button. It wont even download the file, and the cursor will just blink. However, if I close the gui window, I get the following code:
Traceback(most recent call last):
File "C:\Users\user\Desktop\downloader.py", line 28 in <module>
mpb = ttk.Progressbar(root, orient="horizontal", variable =
percent,length="100",mode="determinate")
File "C:\Users/user\Desktop\pyttk-0.3\ttk.py" line 1047, in __init__
Widget.__init__(self, master, "ttk::progressbar", kw)
File "C:\Users/user\Desktop\pyttk-0.3\ttk.py", line 574, in __init__
Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Python26\lib\lib-tk\Tkinter.py", line 1930, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: this isn't a Tk applicationNULL main window
What's wrong?
You have at least two problems in your code, though neither of them cause the error you say you're getting.
First, you use a normal python variable as the value of the variable attribute of the progress bar. While this will work, it won't work as you expect. You need to create an instance of a tkinter StringVar or IntVar. Also, you'll need to call the set method of that instance in order for the progressbar to see the change.
Second, you should never have code after the call to mainloop. Tkinter is designed to terminate once mainloop exits (which typically only happens after you destroy the window). You are going to need to move the call to urlretrieve somewhere else.
variable = percent is wrong. You must use Tkinter Variables which are objects. Such as IntVar.

Creating a GUI with executable button

Hi I am using Python27 on Window7 OS, i am trying to create a Tk GUI with button, when the button is press a file directory will appear. But the following code won't do anything. Did i miss out something?
import webbrowser
import Tkinter as Tk
def action(self):
webbrowser.open ('C:\AgmPlots')
win = Tk.Toplevel()
frame = Tk.Frame(master=win).grid(row=1, column=1)
button = Tk.Button(master=frame, text='press', command= lambda: action())
You've got three big problems.
First, you never start the GUI. You need something like win.mainloop() at the end to actually do anything.
Second, your button isn't actually laid out within the frame, so you won't see it. You need something like button.pack().
Finally, your command is a function that calls action(), with no arguments. But you've defined it to require a parameter. So, all that will happen when you click it is that Tk will log a traceback that looks like this:
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
return self.func(*args)
File "tkt.py", line 8, in <lambda>
button = Tk.Button(master=frame, text='press', command= lambda: action())
TypeError: action() takes exactly 1 argument (0 given)
To fix that, either don't add the unnecessary self parameter to action (this is a function, not a method), or explicitly pass some dummy to match it in your lambda.
While we're at it, lambda: action() does exactly the same thing as action itself, except more verbose, harder to read, and slower. You should never use unescaped backslashes in non-raw string literals. And we might as well remove the stray spaces and PEP8-ify everything to make it consistent.
So, putting it all together:
import webbrowser
import Tkinter as Tk
def action():
webbrowser.open(r'C:\AgmPlots')
win = Tk.Toplevel()
frame = Tk.Frame(master=win).grid(row=1, column=1)
button = Tk.Button(master=frame, text='press', command=action)
button.pack()
win.mainloop()

How to properly bind an event to my button? My button seems to exe the function before click

I'm looking at the example codes online, seems like I'm doing exactly like them. But the event seems to load as soon as the ui loads. What am I doing wrong?
From the code below, the click function doesn't load right when ui is loaded. But when I click the button, it throws:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
TypeError: clicky() takes no arguments (1 given)
class LogIn:
def __init__(self):
self.root = Tk();
self.root.title("480 - Chat Project Login");
self.root.geometry("275x125");
self.username = Label(self.root, text="Username: ");
self.username.pack(side=LEFT);
self.username.place(x=40, y=20);
self.u_entry = Entry(self.root, width=20);
self.u_entry.pack(side=RIGHT, ipady=4, ipadx=4);
self.u_entry.place(x=110, y=20);
self.password= Label(self.root, text="Password: ");
self.password.pack(side=LEFT);
self.password.place(x=40, y=50);
self.p_entry = Entry(self.root, width=20);
self.p_entry.pack(side=RIGHT, ipady=4, ipadx=4);
self.p_entry.place(x=110, y=50);
self.button = Button(text="Send", width=8);
self.button.pack(side=RIGHT, ipady=4, ipadx=4);
self.button.place(x=168, y=80);
self.button.bind("<Button-1>", clicky);
self.root.mainloop();
def clicky():
print "hello";
if __name__ == "__main__":
LogIn();
# Client();
You need self.button = Button(text="Send",width=8,command=clicky).
There's a difference between callbacks registered via command and callbacks registered via bind. With command, the callback doesn't get passed any additional arguments. With bind, the callback gets passed an event object.
Also, in case it wasn't clear, note that command is specific to Button objects whereas bind can be applied to any Tkinter widget.

Categories

Resources