0

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.

msw
  • 42,753
  • 9
  • 87
  • 112
user3000724
  • 651
  • 3
  • 11
  • 22
  • 1
    You are just missing a comma. Like this: `from tkinter import N, S, E, W` – Rick Mar 08 '16 at 20:09
  • 1
    This'll come down to opinion, but tk.N isn't that many more characters than N and N won't be an intuitive variable to someone looking at your code later, IMO. I would assume N is the dimension of the image of something, or a loop limit, not the word 'north'. Also note you could use a string there, tk accepts that. Although I guess sticky=N couldn't mean too many things, just seems like asking for trouble – en_Knight Mar 08 '16 at 20:24
  • You seem to be asking "how do I do an `from tkinter import *` without doing that. You can't have it both ways. You can, however, do `import tkinter as tk; from tkinter import N, E, S, W` which brings most into the `tk` namespace (`tk.Button`) with while N is in the global namespace. That said, members with names of one character are far more readable as `tk.N` than `N`. – msw Mar 08 '16 at 20:26
  • @en_Knight: The Tkinter names N,S,E,W _are_ actually directions, used for widget alignment. See http://effbot.org/tkinterbook/grid.htm – PM 2Ring Mar 08 '16 at 20:41
  • @PM2Ring I know, I'm just saying it seems like an easy way to confuse future readers. It's a fine identifier when prepended with the namespace, and probably isn't amiguous here (maybe it's just a gut reaction to seeing a one letter variable name). I agree with bryan's suggestion: sticky='n' seems preferable to me than sticky=N. – en_Knight Mar 08 '16 at 20:53
  • @en_Knight: Fair enough. I was _almost_ going to suggest just using string literals instead, but then I figured that `tk.N` is only one char more than `'n'`. :) FWIW, I always use the `tk.N` form in my Tkinter code, just to keep things explicit. – PM 2Ring Mar 08 '16 at 20:57

1 Answers1

3

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685