[英]Label widget wont move inside of frame in tkinter
我對Tkinter還是很陌生,但是現在已經練習了一段時間,但是每當我嘗試通過指定行和列在具有網格幾何形狀的框架中移動標簽時,標簽就停留在中間。
在下面的代碼中,我嘗試為名稱標簽指定行和列,該標簽位於頂部中間框架中,但始終不會移動。 它只是停留在中間。
from tkinter import*
root=Tk()
frame_topleft = Frame(root, height=150, width=50, bg = "green")
frame_topmiddle = Frame(root, height=150, width=250, bg="red")
frame_topright = Frame(root, height=150, width=250, bg="green")
frame_bottomleft = Frame(root, height=300, width=50, bg="blue")
frame_bottommiddle = Frame(root, height=300, width=250, bg="yellow")
frame_bottomright = Frame(root, height=300, width=250, bg="blue")
label_name=Label(frame_topmiddle, text="Name", font="halvetica")
label_phone=Label(frame_topmiddle, text="Phone", font="halvetica")
frame_topmiddle.grid(row=0, column=1)
frame_topleft.grid(row=0, column=0)
frame_topright.grid(row=0, column=2)
frame_bottomleft.grid(row=1, column=0)
frame_bottommiddle.grid(row=1, column=1)
frame_bottomright.grid(row=1, column=2)
label_name.grid(row=0, column=0)
root.mainloop()
因此,我只是想知道如何解決此問題。 我希望名稱標簽位於頂部中間框架的左上方。
frame_topmiddle
縮小到label_name
的大小,默認情況下顯示在(行0,第1列)的中心。
如果將網格方法的sticky
選項設置為“ nw”:
frame_topmiddle.grid(row=0, column=1, sticky='nw')
那么frame_topmiddle
將位於(第0行,第1列)的左上角,而不是中心。
如果希望frame_topmiddle
保持其初始大小,則需要執行frame_topmiddle.grid_propagate(False)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.