我想显示
Frame
Label
中的Frame
会延伸到窗口的整个宽度以下代码
import Tkinter as tk
root = tk.Tk()
root.geometry("100x100")
# first column of root will stretch
root.columnconfigure(0, weight=1)
# a frame in root
upper_frame = tk.Frame(root)
# first column of upper_frame will stretch
upper_frame.columnconfigure(0, weight=1)
upper_frame.grid(row=0, column=0)
# a label in upper_frame, which should stretch
mylabel = tk.Label(upper_frame)
mylabel.grid(row=0, column=0)
mylabel.configure(text="hello", background="blue")
root.mainloop()
显示器
为什么Label
伸展到窗口的整个宽度但是和文本一样宽?
答案 0 :(得分:0)
在致电grid
时调用sticky
选项(e
= east,w
= west)。否则,单元格中的窗口小部件是居中对齐的。
upper_frame.grid(row=0, column=0, sticky='ew')
..
mylabel.grid(row=0, column=0, sticky='ew')