Tkinter escape binding not destroying - python

I am trying to learn tkinter and the idea that I have requires it to be in fullscreen. Before making it fullscreen, however I wanted to make sure I could close the window using escape. So through other questions similar to this one on Stack Overflow I have been trying to get it to destroy the tkinter window when I hit escape. To me this seems like it should work but I am getting an exception when I hit escape:
`Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Jake\AppData\Local\Programs\Python\Python36-
32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
TypeError: destroy() takes 1 positional argument but 2 were given`
This is confusing for me because I don't think I am calling any arguments at all let alone 2. I have added a quit button which calls the close method I made and that works but using escape doesn't seem to. I have supplied my code for clarity. I know this is similar to a lot of questions on here but I have tried everything and nothing seems to be working for me. Thanks in advance!
import tkinter
from tkinter import *
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
master.bind('<Escape>', master.destroy)
self.init_window()
def init_window(self):
self.pack(fill=BOTH, expand=1)
quitButton = Button(self, text="quit", command=self.close)
quitButton.place(x=0, y=0)
def close(self):
self.master.destroy()
def main():
root = Tk()
root.geometry('500x500')
app = Window(root)
root.mainloop()
main()

When you bind a function to an event, tkinter will always pass an event object to that function. The destroy function takes no arguments, which means you can't bind directly to it. You need to bind to a function that will accept the event object as an argument.
Since you already have a function, you can give it an optional named argument so that you can continue to call your function without the argument, but it can also be used in a binding:
class Window(Frame):
def __init__(self, master = None):
...
master.bind('<Escape>', self.close)
def close(self, event=None):
self.master.destroy()
You could also use an anonymous function:
master.bind('<Escape>', lambda event: master.destroy())

The following code works.
I used it in a class for creating a full screen app in a 800x480 touch screen for pi:
class FullScreenApp(object):
def __init__(self, master, **kwargs):
self.master=master
pad=3
self._geom='200x200+0+0'
master.geometry('{}x{}'.format(800,480))
master.bind('<Escape>', self.close)
def close(self, event=None):
self.master.destroy()

Related

How to define Tkinter controller?

Code Background: trying to have a popup window call a function in another class.
I am having trouble defining controller and configuring my functions correctly. I get an error about controller not being defined before it passes to the popupWindow class which makes sense because I have not defined controller somewhere else but I don't know where to do that:
NameError: name 'controller' is not defined
I have studied these past answers for help but am still stuck:
Calling Tkinter frame controller from function rather then button command
Calling functions from a Tkinter Frame to another
Here is my simplified code (Note: I define the various variables in the values function in other functions within the class BoundingBox but I have not included those functions for clarity and to shorten the code):
I also don't understand how to make the get_page function work which is part of my problem with figuring out how and where to define controller.
import tkinter as tk
from tkinter import *
class BoundingBox(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
def get_page(self, page_class):
return self.frames[page_class]
def popup(self):
self.w=popupWindow(self.master,controller)
def values(self):
print(self.start_x, self.start_y, self.end_x, self.end_y, self.totwidth, self.totheight, self.value1, self.value2)
self.allcord.append([self.start_x, self.start_y, self.end_x, self.end_y, self.totwidth, self.totheight, self.value1, self.value2])
self.allrect.append(self.rect)
print (len(self.allcord))
class popupWindow(tk.Toplevel):
def __init__(self, master, controller):
super().__init__(master)
self.controller = controller
self.l1=Label(self,text="Breed")
self.l1.grid(row=0, column=0)
self.e1=Entry(self)
self.e1.grid(row=0, column=1)
self.l2=Label(self,text="Color")
self.l2.grid(row=1, column=0)
self.e2=Entry(self)
self.e2.grid(row=1, column=1)
self.b=Button(self,text='Save',command=self.cleanup)
self.b.grid(row=2, column=1)
def cleanup(self):
self.value1=self.e1.get()
self.value2=self.e2.get()
self.controller.values()
self.top.destroy()
if __name__ == "__main__":
draw = BoundingBox()
draw.mainloop()
In this specific case, controller simply refers to the main window. So, you simply need to pass self as the controller argument:
def popup(self):
self.w=popupWindow(self.master, self)
Notice how the cleanup method calls self.controller.values(). values is defined in BoundingBox, so it's clear that popupwindow was designed to have BoundingBox as the controller.

Can't Show Tkinter Root Window Again After Using withdraw()

In my program, I am creating a window from my root tkinter window, and hiding the root using the .withdraw() function. When I try to show the root window again by calling the root class, it does not show and my program exits. Here's a rough outline of my code describing the problem:
class MainGUI:
def __init__(self, master):
self.master = master
#....Create and .grid() all GUI Widgets....
# Button for switching to other window
button = Button(text="CLICKME", command=lambda: self.other_window())
# Call and define show function at the end of __init__
self.show()
def show(self):
self.master.update()
self.master.deiconify()
# Create other window and withdraw self on button click
def other_window(self):
OtherGUI(self.master)
self.master.withdraw()
class OtherGUI:
def __init__(self, master):
# Function for returning to main window, calls MainGUI class
# to create window and withdraws self.
def main_window():
MainGUI(self.master)
self.master.withdraw()
master = self.master = Toplevel(master)
#....Create and .grid() all GUI Widgets....
# Button for switching back to main window
button = Button(text="CLICKME", command=lambda: self.main_window())
Using print functions in the MainGUI, I was able to see that when trying to switch back to the main window, show() is actually called, and the entire class does appear to be entered.
This puzzles me as I've only really learn how to do this from other forum posts, and using root.update() and .deiconify() seemed to be the solution for most people, however I have no idea why this isn't working.
Does anyone have an idea as to where I'm going wrong here?
The example you presented will not work for several reason.
#really you should build your gui as an inherited class as it makes things much easier to manage in tkinter.
class MainGUI:
def __init__(self, master):
self.master = master
button = Button(text="CLICKME", command=lambda: self.other_window())
# no need for lambda expressions here.
# missing geometry layout... grid(), pack() or place()
self.show()
# self.show does nothing here because your show method is improperly indented.
# your other_window method is also not properly indented.
def show(self):
self.master.update()
self.master.deiconify()
def other_window(self):
OtherGUI(self.master)
self.master.withdraw()
class OtherGUI:
def __init__(self, master):
# this function should be its own method.
def main_window():
MainGUI(self.master)
self.master.withdraw()
master = self.master = Toplevel(master)
# this is not how you should be defining master.
button = Button(text="CLICKME", command=lambda: self.main_window())
# missing geometry layout... grid(), pack() or place()
# your button command is using a lambda to call a class method but your define it as a function instead.
Here is a simpler version of what you are attempting that will work:
import tkinter as tk
class MainGUI(tk.Tk):
def __init__(self):
super().__init__()
tk.Button(self, text="Open Toplevel", command=self.open_toplevel_window).pack()
def open_toplevel_window(self):
OtherGUI(self)
self.withdraw()
class OtherGUI(tk.Toplevel):
def __init__(self, master):
super().__init__()
tk.Button(self, text="Close top and deiconify main", command=self.main_window).pack()
def main_window(self):
self.master.deiconify()
self.destroy()
MainGUI().mainloop()
As you can see here when you inherit from the tkinter classes that control the main window and toplevel windows it becomes easier to manage them and less code to perform a task.

Python - How would I use a single function to get information about the frame in Tkinter

Python - Events, Frames and Tkinter
Here I have my GameApplication class which creates and runs the tkinter application. I would like to have it so that I can call the same function from every frame and using that function find out what frame was clicked. I'm only just diving into python now so excuse me if this seems simple.
Thanks for the help in advance.
# Import needed classes.
from tkinter import *
# Create the GameApplication class to run the GUI
class GameApplication(Frame):
def __init__(self, master=None):
super().__init__(master)
self.grid()
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.position_1 = Frame(width=100, height=100, bg="#CC0000")
# This is the Frame I want to get information about.
self.position_1.bind("<Button-1>", self.callback)
self.position_1.grid(column=0, row=0)
self.position_2 = Frame(width=100, height=100, bg="#00CC00")
# And this one too.
self.position_2.bind("<Button-1>", self.callback)
self.position_2.grid(column=1, row=0)
def callback(self, event):
print("?")
if __name__ == "__main__":
root = Tk()
app = GameApplication(master=root)
root.mainloop()
Take a look at this . It's a small project I wrote in python . I have a function that checks the status of each call . I call that function after each function has been executed.
class Messengers:
def __init__(self):
self.box = tkMessageBox
def successer(self):
self.box.showinfo("ImageR Success", "Done YO! Go run a test :)")
def failure(self):
self.box.showerror('ImageR Failure', 'Yo you broke me!')
The code is here https://github.com/jaytarang92/imager . I use subprocess.check_ouput to make sure the call was done correctly.

Close main Tkinter window binding a key

I do not understand why this code does not work:
import tkinter
class Application ():
def__init__(self):
self.master = tkinter.Tk()
self.master.bind("<Enter>", self.quit)
self.master.mainloop()
def quit (self):
self.master.destroy()
my_app = Application()
I keep receiving the error: "quit() takes 1 positional argument but 2 were given". Is there a way to close a main Tkinter window binding a key?
Thanks
Simply add another variable to the quit method ("i","n",etc.), when you bind an event to a method, the method must be able to handle said event as a parameter.
import tkinter
class Application ():
def __ init __ (self):
self.master = tkinter.Tk()
self.master.bind("<Enter>", self.quit)
self.master.mainloop()
def quit (self,n):
self.master.destroy()
#notice that the n variable doesnt really do anything other than "handling" of the event, so when
#it gets 2 arguments it can handle 2 parameters without giving an exception
#the (old) method only had space for 1 argument (self), but the moment you "bind" a button or event
#the method MUST be able to handle such information
my_app = Application()

Passing function into tkinter object

I am creating a virtual keyboard using tkinter in python 3.4.
class Keyboard(Frame):
def __init__(self, root, callback):
Frame.__init__(self, root, callback)
self.pack()
callback is going to be a function invoked when user presses a key.
So I will run it like:
def press(key):
print(key)
root = Tk()
keyb = Keyboard(root, press)
keyb.grid()
root.mainloop()
However, this must not be the right way to pass the function press into Keyboard because I get an error "AttributeError: 'function' object has no attribute 'items'"
So how do I pass this function into Keyboard?
First of all, don't mix pack and grid. See http://effbot.org/tkinterbook/pack.htm
Second, the Frame class does not take a callback parameter, but in your code you called it with one:
Frame.__init__(self, root, callback)
See http://effbot.org/tkinterbook/frame.htm#reference for correct usage.
What you're looking for would be like this, where callback was removed completely from Keyboard:
def press(key):
print(key)
root = Tk()
keyb = Keyboard(root)
keyb.bind("<Key>", key)

Categories