简体   繁体   English

tkinter python 网格 function 不能正常工作

[英]tkinter python grid function doesn't work properly

This is my code:这是我的代码:

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()

the top right corner thing is just next to the top left corner object, why?右上角的东西就在左上角object旁边,为什么? I am really new to tkinter我真的是 tkinter 的新手

it's because the columns between the widgets don't have anything in them so they don't take up space.这是因为小部件之间的列中没有任何内容,因此它们不占用空间。 you can fix it by using grid_columnconfigure() to set the weight of any column in between the two widgets to 1 so the widgets are pushed to the edges of the screen您可以通过使用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()

The rows and column in grid no not take any space if they are empty.网格中的行和列如果为空则不占用任何空间。 Easiest thing to do is add a label(same color as background) inbetween them (column=1) with the required width (eg. width=10) to push the top_right_corner to the right.最简单的做法是在它们之间添加一个标签(与背景颜色相同)(列 = 1)并具有所需的宽度(例如宽度 = 10)以将 top_right_corner 推到右边。

The proper way to do it though is to use column configure,正确的方法是使用列配置,

root.grid_columnconfigure(X, weight=1)

which sets a blank weight to column X.它将空白权重设置为 X 列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM