Search code examples
pythontkinterreadability

How to put 'tk.' in front of every Tkinter object the right way?


I threw together a very quick proof of concept python application (~1000 lines) and it now has potential to be taken more seriously. It says for best programming practices, don't use

from <module> import *

(I did this for the proof of concept) Because I use Tkinter and ttk (plus a few more), I figured I'd just go back and quickly add 'tk.' in front of my buttons/labels/etc. The problem is, I have a lot of sticky commands and putting tk.N tk.S tk.E tk.W for every Tkinter keyword would take a while and doesn't seem like that's how it should be (readability would be gross for the longer commands).

Is there some trick? Should I do something like the following?

from Tkinter import N S E W

Thanks for any guidance.


Solution

  • My advice is to not use N, S, E and W (eg: x.grid(..., sticky="nsew")). Just use the literal strings. The constants aren't going to change -- they've been the same since 1996 and the tcl/tk community takes backwards compatibility very seriously.