Function to close the window in Tkinter

def quit(self): self.root.destroy() Add parentheses after destroy to call the method. When you use command=self.root.destroy you pass the method to Tkinter.Button without the parentheses because you want Tkinter.Button to store the method for future calling, not to call it immediately when the button is created. But when you define the quit method, you need to … Read more

Image on a button

The only reference to the image object is a local variable. When __init__ exits, the local variable is garbage collected so the image is destroyed. In the second example, because the image is created at the global level it never goes out of scope and is therefore never garbage collected. To work around this, save … Read more

Disable / Enable Button in TKinter

A Tkinter Button has three states : active, normal, disabled. You set the state option to disabled to gray out the button and make it unresponsive. It has the value active when the mouse is over it and the default is normal. Using this you can check for the state of the button and take … Read more

Tkinter example code for multiple windows, why won’t buttons load correctly?

I rewrote your code in a more organized, better-practiced way: import tkinter as tk class Demo1: def __init__(self, master): self.master = master self.frame = tk.Frame(self.master) self.button1 = tk.Button(self.frame, text=”New Window”, width = 25, command = self.new_window) self.button1.pack() self.frame.pack() def new_window(self): self.newWindow = tk.Toplevel(self.master) = Demo2(self.newWindow) class Demo2: def __init__(self, master): self.master = master self.frame … Read more

How do I remove the light grey border around my Canvas widget?

Section 6.8 Why doesn’t the canvas seem to start at 0,0? of the Tk Usage FAQ describes the phenomenon. I was able to eliminate the border artefact with slight changes to the posted source… Change this: w = Canvas(master, width=150, height=40, bd=0, relief=”ridge”) w.pack() to: w = Canvas(master, width=150, height=40, bd=0, highlightthickness=0, relief=”ridge”) w.pack() and … Read more

How to clear the Entry widget after a button is pressed in Tkinter?

After poking around a bit through the Introduction to Tkinter, I came up with the code below, which doesn’t do anything except display a text field and clear it when the “Clear text” button is pushed: import tkinter as tk class App(tk.Frame): def __init__(self, master): tk.Frame.__init__(self, master, height=42, width=42) self.entry = tk.Entry(self) self.entry.focus() self.entry.pack() self.clear_button … Read more

Adding padding to a tkinter widget only on one side

The padding options padx and pady of the grid and pack methods can take a 2-tuple that represent the left/right and top/bottom padding. Here’s an example: import tkinter as tk class MyApp(): def __init__(self): self.root = tk.Tk() l1 = tk.Label(self.root, text=”Hello”) l2 = tk.Label(self.root, text=”World”) l1.grid(row=0, column=0, padx=(100, 10)) l2.grid(row=1, column=0, padx=(10, 100)) app = … Read more