问题
I would like to display
- a window
- with a single
Frame
- and a
Label
in theFrame
which would stretch to the whole width of the window
The following code
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()
displays
Why isn't the Label
stretching to the whole width of the window but is just as wide as the text?
回答1:
Specifying sticky
option when you call grid (e
= east, w
= west). Otherwise the widget in the cell is center-aligned.
upper_frame.grid(row=0, column=0, sticky='ew')
..
mylabel.grid(row=0, column=0, sticky='ew')
来源:https://stackoverflow.com/questions/24596354/a-label-in-a-frame-in-a-window-wont-stretch-why