简体   繁体   中英

Python Tkinter / ttk differences in coding styles? Or more?

Using Python (2.7) on Windows7, I have been learning Tkinter and ttk for some weeks now, and note some differences in the way some things are written.

For example, when I use the grid manager,

sometimes I see (on a single line):

f = Frame(root, ...).grid(...)

other times I see (on two lines):

f = Frame(root, ...)
f.grid(...)

Is there a difference, or is it simply a personal coding preference?

I assume pack() is similar?


Also, when using the sticky(), I have seen:

grid(sticky(NESW))                   # without quotes, upper case
grid(sticky(N, E, S, W))             # without quotes separate arguments
grid(sticky('nsew'))                 # with quotes single string, lower case
grid(sticky('N' + 'S' + 'E' + 'W'))  # plus sign makes it a single string

and some others including a different order for N, E, S, and W.

I have tried different ways and haven't had Tkinter complain but am still not sure it is working correctly.

Again, does Tkinter care or is there a difference?

Thanks, Mark

Label(...).grid(...)

When you do f = Frame(root, ...).grid(...) , f will have the value of None because grid(...) returns None . Thus, this format of creating the widget and laying the widget out on the screen is highly discouraged.

If you need to keep a reference to a widget, you must create them and lay them out in two separate statements. And since you're doing it that way for some widgets, you should do it for all to keep the code consistent.

Besides making it impossible to keep a reference to the widget, my own personal experience is that it makes code much harder to understand and maintain over time. I advocate creating all your widgets in one block of code, and laying them out in another.

The same holds true for all of the geometry managers: pack , place and grid

Sticky values

The code that implements the actual widgets uses the literal strings "n" (north) "s" (south), "e" (east) and "w" (west) for the sticky attributes. Tkinter defines constants of the same name, though I've never understood why.

Personally I discourage the use of the constants because they don't provide any value over the hard-coded strings. Also, combining them is arguably easier with strings rather than the constants (eg: "nsew" vs N+S+E+W )

Note: the order doesn't matter. You can place those values in any order.

The value of these constants will almost certainly never change 1 for the life of tkinter, and if they do, the names of the constants will likely change, too. For example, if the value changed from "n" to "t" (top), it's unlikely that the constant would remain "N" since it is mnemonically confusing.


1 why? Because tkinter is built upon tcl/tk, and the core tcl maintainers are sticklers for backwards compatibility. These constants have been the same for decades .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM