What is the best way to structure a Tkinter application?

I advocate an object oriented approach. This is the template that I start out with: # Use Tkinter for python 2, tkinter for python 3 import tkinter as tk class MainApplication(tk.Frame): def __init__(self, parent, *args, **kwargs): tk.Frame.__init__(self, parent, *args, **kwargs) self.parent = parent <create the rest of your GUI here> if __name__ == “__main__”: root … Read more

Why does tkinter (or turtle) seem to be missing or broken? Shouldn’t it be part of the standard library?

WARNING: Do not use pip to try to solve the problem The Pip package manager cannot help to solve the problem. No part of the Python standard library – including tkinter, turtle etc. – can be installed from PyPI. For security reasons, PyPI now blocks packages using names that match the standard library. There are … Read more

Tkinter KeyPress and KeyRelease events

Ok some more research found this helpful post which shows this is occuring because of X’s autorepeat behaviour. You can disable this by using os.system(‘xset r off’) and then reset it using “on” at the end of your script. The problem is this is global behaviour – not just my script – which isn’t great … Read more

How can I prevent a window from being resized with tkinter?

This code makes a window with the conditions that the user cannot change the dimensions of the Tk() window, and also disables the maximise button. import tkinter as tk root = tk.Tk() root.resizable(width=False, height=False) root.mainloop() Within the program you can change the window dimensions with @Carpetsmoker’s answer, or by doing this: root.geometry(‘{}x{}’.format(<widthpixels>, <heightpixels>)) It should … Read more

How to change the foreground or background colour of a selected cell in tkinter treeview?

@BryanOkley shared that one cannot change the color of an individual cell in ttk.Treeview. So I explored using tk.Canvas() and tk.Canvas.create_text() to create the illusion of changing the color of a selected cell in a ttk.Treeview() widget. I was fortunate to come by j08lue/ttkcalendar.py which had the same objective and I adapted from it. My … Read more