[英]tkinter python grid function doesn't work properly
這是我的代碼:
from tkinter import *
# row = Zeile
# Column = Spalte
root = Tk()
root.title("z")
root.geometry("1080x700")
top_left_corner = Label(root, text="Links oben", font =("arial", 12))
top_left_corner.grid(row=0, column=0)
top_right_corner = Label(root, text="rechts oben", font =("arial", 12))
top_right_corner.grid(row=0, column=10)
root.mainloop()
右上角的東西就在左上角object旁邊,為什么? 我真的是 tkinter 的新手
這是因為小部件之間的列中沒有任何內容,因此它們不占用空間。 您可以通過使用grid_columnconfigure()
將兩個小部件之間的任何列的權重設置為 1 來修復它,以便將小部件推到屏幕的邊緣
from tkinter import *
root = Tk()
root.title("z")
root.geometry("1080x700")
top_left_corner = Label(root, text="Links oben", font =("arial", 12))
top_left_corner.grid(row=0, column=0)
top_right_corner = Label(root, text="rechts oben", font =("arial", 12))
top_right_corner.grid(row=0, column=10)
root.grid_columnconfigure(1, weight=1)# add this line
root.mainloop()
網格中的行和列如果為空則不占用任何空間。 最簡單的做法是在它們之間添加一個標簽(與背景顏色相同)(列 = 1)並具有所需的寬度(例如寬度 = 10)以將 top_right_corner 推到右邊。
正確的方法是使用列配置,
root.grid_columnconfigure(X, weight=1)
它將空白權重設置為 X 列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.