调整带有固定宽高比的 Tkinter 框架

2024-10-02 00:35:58 发布

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

我正在寻找一种方法,使Tkinter帧在调整大小时有点像这样:

from Tkinter import *
w = Tk()
w.aspect(1,1,1,1)
w.mainloop()

所以我想要这里的代码:

import Tkinter as tk
from Tkconstants import *
r=tk.Tk()
content_frame=tk.Frame(r,borderwidth=5,relief=GROOVE)
content_frame.grid(row=0,column=0,sticky=(N,S,E,W))
tk.Label(content_frame,text='content').pack()
pad_frame=tk.Frame(r,borderwidth=5,relief=GROOVE)
pad_frame.grid(row=1,column=0,sticky=(N,S,E,W))
tk.Label(pad_frame,text='-pad-').pack()
r.rowconfigure(0, weight=1)
r.rowconfigure(1, weight=1)
r.columnconfigure(0, weight=1)
r.mainloop()

它基本上创建了两个帧,一个应该包含一些内容(在我的应用程序中,这个帧包含一个可以调整大小的mathplotlib图),另一个仅仅用于填充。

按照以下规则调整大小:

  • 内容框架按固定的纵横比调整大小(假设它始终需要为正方形)
  • 焊盘框架占据剩余(垂直)空间

有什么想法吗?我已经看了一段时间的手册,似乎找不到合适的东西。

-丹尼尔


Tags: fromimporttkintercontentframetkgridweight

热门问题