Python tkinter,网格管理器不工作

2024-06-26 13:38:00 发布

您现在位置:Python中文网/ 问答频道 /正文

import tkinter
from tkinter import *
root = Tk()
root.geometry("500x500")
root.title("Insert title")
root.configure(background='#CCCCFF')
label1 = Label(root, text = "Insert title", font = ("Rockwell", 12))
label2 = Label(root, text = "Name", font = ("Rockwell", 25))
label1.configure(background='#CCCCFF')
label2.configure(background = '#CCCCFF')
label1.grid(row = 8, column = 3)
root.mainloop()

每次更改label2的网格管理器设置时,标签始终保持在同一位置。我该怎么解决这个问题?在


Tags: textfromimporttitletkinterconfigurerootlabel
2条回答

如果行或列完全为空,则其大小将为零。所以即使你在第8行放了一些东西,第0行到第7行都是不存在的。立柱也是如此。在

如果你问“为什么我的'插入标题'标签总是出现在左上角,即使它有大的行和列值?”第八行中的任何一行都是空的,所以第八行中的任何一行都是空的。在

一种可能的解决方法是将占位符小部件添加到不希望折叠的每一行和每列。在

import tkinter
from tkinter import *
root = Tk()
root.geometry("500x500")
root.title("Insert title")
root.configure(background='#CCCCFF')

for i in range(10):
    Frame(root, width=20, height=20, background='#CCCCFF').grid(row=0, column=i)

for j in range(10):
    Frame(root, width=20, height=20, background='#CCCCFF').grid(column=0, row=j)

label1 = Label(root, text = "Insert title", font = ("Rockwell", 12))
label2 = Label(root, text = "Name", font = ("Rockwell", 25))
label1.configure(background='#CCCCFF')
label2.configure(background = '#CCCCFF')
label1.grid(row = 8, column = 3)
label2.grid(row = 9, column = 3)
root.mainloop()

相关问题 更多 >