0

我从 Python 中的 GUI 开始,但遇到了问题。我已经在我的框架中添加了小部件,但它们总是在左侧。我尝试了一些来自互联网的示例,但我没有管理它。我试过.place了,但它对我不起作用。有人能告诉我如何将小部件放在中间吗?

代码:

import tkinter as tk

def site_open(frame):
    frame.tkraise()

window = tk.Tk()

window.title('Test')
window.geometry('500x300')

StartPage = tk.Frame(window)
FirstPage = tk.Frame(window)

for frame in (StartPage, FirstPage):
    frame.grid(row=0, column=0, sticky='news')

lab = tk.Label(StartPage, text='Welcome to the Assistant').pack()
lab1 = tk.Label(StartPage, text='\n We show you helpful information about you').pack()
lab2 = tk.Label(StartPage, text='\n \n Name:').pack()
ent = tk.Entry(StartPage).pack()
but = tk.Button(StartPage, text='Press', command=lambda:site_open(FirstPage)).pack()

lab1 = tk.Label(FirstPage, text='1Page').pack()
but1 = tk.Button(FirstPage, text='Press', command=lambda:site_open(StartPage)).pack()

site_open(StartPage)
window.mainloop()
4

2 回答 2

2

创建完成后window,添加:

window.columnconfigure(0, weight=1)

更多在网格几何管理器

于 2018-06-14T22:04:09.853 回答
0

您正在混合两个不同的布局管理器。我建议您使用The Grid Geometry Manager The Pack Geometry Manager

一旦你决定了你想使用哪一个,它会更容易帮助你:)

例如,您可以使用具有两行和两列的网格几何管理器,并像这样放置小部件:

label1 = Label(start_page, text='Welcome to the Assistant')
# we place the label in the page as the fist element on the very left 
# and allow it to span over two columns
label1.grid(row=0, column=0, sticky='w', columnspan=2) 

button1 = Button(start_page, text='Button1', command=self.button_clicked)
button1.grid(row=1, column=0)

button2 = Button(start_page, text='Button2', command=self.button_clicked)
button2.grid(row=1, column=1)

这将导致标签位于第一行和两个按钮下方并排。

于 2018-06-14T22:18:09.997 回答