首页 > 解决方案 > 如何使用 tkinter 设置 3 个或更多帧?

问题描述

我正在尝试这个,所以我可以在 medFrame 中放置按钮,但它们出现在 topFrame 中。使用 topFrame 时,按钮靠在屏幕顶部,看起来很糟糕,所以我想这可以通过使用第三个框架来解决。

from tkinter import *


root = Tk()
root.title('BulletHead')
root.attributes('-fullscreen', True)
root.resizable(width = NO, height=NO)  

topFrame=Frame(root)
topFrame.pack(side=TOP)

medFrame=Frame(root)
medFrame.pack()

botFrame = Frame(root)
botFrame.pack(side=BOTTOM)

botonJugar = Button(medFrame, text = 'Jugar')
botonJugar.bind("<Button-1>",jugar)
botonJugar.pack()

botonTabla = Button(medFrame, text = 'Tabla de puntajes')
botonTabla.bind("<Button-1>",tabla)
botonTabla.pack()

root.mainloop()

标签: pythontkintertkinter-layout

解决方案


元素已成功添加到中间帧,但它们看起来好像在顶部帧中,因为topFramebotFrame没有尺寸,因此它们不会出现(除非您可以感知一个像素)。要为中间框架获得一些间距,您需要为其他框架提供一些尺寸。试试这个给中间框架一些间距:

topFrame=Frame(root, height=200, width=200)
botFrame = Frame(root, height=200, width=200)

对此的替代方法是检查一些其他选项,以使用仅一帧的包几何管理器来获得所需的结果。特别是该expand=选项有助于在窗口中居中对象。

from tkinter import *

root = Tk()
root.title('BulletHead')
root.attributes('-fullscreen', True)
root.resizable(width = NO, height=NO)  

medFrame=Frame(root)
medFrame.pack(expand=True)

botonJugar = Button(medFrame, text = 'Jugar')
botonJugar.bind("<Button-1>",jugar)
botonJugar.pack()

botonTabla = Button(medFrame, text = 'Tabla de puntajes')
botonTabla.bind("<Button-1>",tabla)
botonTabla.pack()

root.mainloop()

推荐阅读