![](/img/trans.png)
[英]How can I terminate Python Script with particular 'if' conditions that does not affect Tkinter GUI?
[英]How does the sticky command affect the python code in Tkinter?
from tkinter import *
from tkinter import ttk
root = Tk()
root.rowconfigure(0,weight = 1)
root.columnconfigure(0,weight = 1)
frame = ttk.Frame(root)
frame.grid(row = 0,column =0)
ttk.Label(frame,text = 'Label_1').grid(row=0,column=0,sticky='nsew')
ttk.Button(frame,text = 'Button').grid(row=0,column=1,sticky='nwse')
ttk.Label(frame,text = 'Label_2').grid(row=0,column=2,sticky='nwes')
root.mainloop()
如果我除去粘滞物或将其留在原处,则看不到输出的明显差异。 那么粘性如何影响我的代码?
要查看差异,您需要给tkinter
一个理由,使其不完全适合您的小部件周围的单元格,如果可能的话,默认情况下始终会这样做。 尝试:
from tkinter import *
from tkinter import ttk
root = Tk()
root.rowconfigure(0,weight = 1)
root.columnconfigure(0,weight = 1)
frame = ttk.Frame(root)
frame.grid(row = 0,column =0)
ttk.Label(frame,text = 'Label_1',background='red').grid(row=0,column=0,sticky='nwes')
ttk.Button(frame,text = 'Button').grid(row=0,column=1,sticky='nwse')
ttk.Label(frame,text = 'Label``_2').grid(row=0,column=2,sticky='nwes')
ttk.Label(frame,text = 'LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG').grid(row=1,column=0)
root.mainloop()
然后从Label_1
删除,sticky='news'
。 请注意,文本居中和窗口小部件居中是两件事-这就是为什么我要赋予背景色以使其显而易见。
另外,虽然我会from tkinter import Button,Frame,Tk
明确列出它们from tkinter import Button,Frame,Tk
或者使用import tkinter as tk
并使用tk.Label
,但from tkinter import *
并不需要ttk
,您已经from tkinter import *
获取了Button
和Frame
tk.Label
。
sticky
选项告诉tkinter如果小部件的空间比所需的多,该怎么办。 它告诉tkinter小部件需要“粘贴”到空白区域的哪一边。
在您的情况下,窗口正好适合窗口小部件,因此您不会看到任何区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.