python tk button 尺寸_python – Tkinter框架调整大小

本文档介绍了一个使用Tkinter创建的应用程序,该应用包含多个页面(StartPage, PageOne, PageTwo)。每个页面都有一个用于导航的按钮。作者遇到的问题是,当切换页面时,窗口大小不会根据当前页面的大小动态调整,导致空间利用不均。作者寻求解决方案,以实现窗口尺寸随页面内容变化而动态调整。" 41344277,265876,GDI+实现摄像头BMP到JPEG转换,"['图像处理', 'GDI+', '摄像头开发', '文件格式转换']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我已经被困了几天试图弄清楚如何使用这种方法动态调整TKIn​​ter中的帧.

class SampleApp(tk.Tk):

def __init__(self, *args, **kwargs):

tk.Tk.__init__(self, *args, **kwargs)

# the container is where we'll stack a bunch of frames

# on top of each other, then the one we want visible

# will be raised above the others

container = tk.Frame(self)

container.pack(side="top", fill="both", expand=True)

container.grid_rowconfigure(0, weight=1)

container.grid_columnconfigure(0, weight=1)

self.frames = {}

for F in (StartPage, PageOne, PageTwo):

page_name = F.__name__

frame = F(container, self)

self.frames[page_name] = frame

# put all of the pages in the same location;

# the one on the top of the stacking order

# will be the one that is visible.

frame.grid(row=0, column=0, sticky="nsew")

self.show_frame("StartPage")

def show_frame(self, page_name):

'''Show a frame for the given page name'''

frame = self.frames[page_name]

frame.tkraise()

class StartPage(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)

self.controller = controller

label = tk.Label(self, text="This is the start page", font=TITLE_FONT)

label.pack(side="top", fill="x", pady=10)

button1 = tk.Button(self, text="Go to Page One",

command=lambda: controller.show_frame("PageOne"))

button2 = tk.Button(self, text="Go to Page Two",

command=lambda: controller.show_frame("PageTwo"))

button1.pack()

button2.pack()

class PageOne(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)

self.controller = controller

label = tk.Label(self, text="This is page 1", font=TITLE_FONT)

label.pack(side="top", fill="x", pady=10)

button = tk.Button(self, text="Go to the start page",

command=lambda: controller.show_frame("StartPage"))

button.pack()

class PageTwo(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)

self.controller = controller

label = tk.Label(self, text="This is page 2", font=TITLE_FONT)

label.pack(side="top", fill="x", pady=10)

button = tk.Button(self, text="Go to the start page",

command=lambda: controller.show_frame("StartPage"))

button.pack()

if __name__ == "__main__":

app = SampleApp()

app.mainloop()

我面临的问题是,使用这种方法,所有框架都堆叠在一个容器中,这个容器的大小是其最大框架的大小.从一个帧移动到另一个帧不会动态调整相应窗口的大小,并在小帧中导致巨大的可用空间.我尝试了不同的技术,使容器中的所有帧动态调整大小,但没有太大的成功.有人可以建议我能做什么吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值