调整 tkinter 窗口和内容的大小 [英] Resizing tkinter windows and contents
问题描述
我正在使用 python tkinter 构建一个包含 matplotlib 图形和一些按钮的 ui,但在调整窗口及其内容时遇到困难.我已经查看了本网站上的一些示例和文档,据我了解,对于包含较小框架的框架,要一起调整大小,它们都需要单独配置.每个都有一个权重来定义它接收多少可用空间(这是正确的吗?).但是,当我尝试如下所示应用它时,没有任何帧调整大小.此外,如果 columnconfigure 的 wiegth 为零,而 row configure 的 wiegth 为 1,这是否意味着它只会在一个方向上调整大小?
I am using python tkinter to build a ui containing a matplotlib figure and some buttons, but am having difficulty with resizing the window and it's contents. I've looked at some of the examples on this site and the docs and as I understand it, for a frame containing smaller frames to resize together they all need to be configured individually. Each one gets a weight applied to it to define how much of the available space it receives (is this correct?). However, when I try to apply this as shown below none of the frames resize. Also if the wiegth is zero for columnconfigure and 1 for row configure does that mean it will only resize in one direction?
import Tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class Application():
def __init__(self, master):
frame2 = Tkinter.Frame(master, height=510, width=770, bg='red')
frame2.grid(row=0, column=0, sticky='nsew')
frame2.columnconfigure(0, weight=1)
frame2.rowconfigure(0, weight=1)
frame2a = Tkinter.Frame(frame2, height=80, width=770, bg='blue')
frame2a.grid(row=0, column=0, sticky='nsew')
frame2a.columnconfigure(0, weight=1)
frame2a.rowconfigure(0, weight=1)
frame2b = Tkinter.Frame(frame2, height=410, width=770, bg='green')
frame2b.grid(row=1, column= 0, sticky='nsew')
frame2b.columnconfigure(0, weight=1)
frame2b.rowconfigure(1, weight=1)
# add plot
fig = Figure(figsize=(9.5,5.2), facecolor='white')
fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig, master=frame2b)
canvas.show()
canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
if __name__ == '__main__' :
root = Tkinter.Tk()
root.geometry("770x510")
app = Application(root)
root.mainloop()
推荐答案
您使用 columnconfigure
和 rowconfigure
的方法是正确的,但您忘记了一件事:您还没有使用了 master 窗口上的方法.所以,你基本上想要这样做:
Your approach using columnconfigure
and rowconfigure
is correct, but you forgot one thing: You haven't used the methods on the master window. So, you basically want to do this:
def __init__(self, master):
master.columnconfigure(0, weight=1)
master.rowconfigure(0, weight=1)
回答您的另一个问题(另外,如果 columnconfigure 的 wiegth 为零,而 row configure 的 wiegth 是否意味着它只会在一个方向上调整大小?):是的,您说得对,小部件/窗口将只向一个方向延伸或不向任何方向延伸.
To answer your other question (Also if the wiegth is zero for columnconfigure and 1 for row configure does that mean it will only resize in one direction?): Yes, you're right, the widget/window would extend in just one or none direction then.
此外,由于您使用 grid
进行动态调整大小,height
和 width
参数已过时.
Additionally, since you are using grid
for dynamic resizing, the height
and width
parameters are obsolete.
这篇关于调整 tkinter 窗口和内容的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!