简体   繁体   English

用相同大小的小部件重复替换小部件 - 它是如何完成的?

[英]Repeated replacement of a widget with a widget of equal size - how its done?

I'm currently working on a tkinter based python gui and in my program you are able to press a button and then the first widget is getting invisible and the other one ist displayed one the same position of the first one.我目前正在开发基于 tkinter 的 python gui,在我的程序中,您可以按下一个按钮,然后第一个小部件变得不可见,而另一个小部件显示在与第一个相同的位置。 After that you are able to switch back and the second widget disappears and the first one will be at the same position visible and so on...之后,您可以切换回来,第二个小部件消失,第一个小部件将在同一位置可见,依此类推...

My goal is that the two widgets get the same size if you are switching the wdigets and the user will not see any bigger/smaller widgets, but I'm not able to say how I can get there - may you can help me?我的目标是,如果您正在切换 wdigets 并且用户不会看到任何更大/更小的小部件,则这两个小部件的大小相同,但我无法说明如何到达那里 - 你能帮我吗?

My tkinter window:我的 tkinter 窗口:

self.window = tkinter.Tk()
self.window.resizable(True, True)
self.window.grid_rowconfigure(2, weight=1)
self.window.grid_columnconfigure(0, weight=2)
self.window.grid_columnconfigure(1, weight=1)

My frame for the two widgets:我的两个小部件的框架:

contentFrame = tkinter.Frame(master=self.window, bg="#FFF", highlightthickness=0, borderwidth=0)
contentFrame.grid_rowconfigure(0, weight=1)
contentFrame.grid_columnconfigure(0, weight=1)
contentFrame.grid(column=0, row=2, padx=5, pady=5, sticky="nsew")

My first widget (red one):我的第一个小部件(红色小部件):

canvas = tkinter.Canvas(master=contentFrame, bg="#F00", highlightthickness=0, borderwidth=0)
        canvas.pack(fill="both", expand=True)

My second widget (blue one)我的第二个小部件(蓝色的)

xmlEditor = tkinter.Text(master=contentFrame, bg="#00F", highlightthickness=0, borderwidth=0)

And whats happening when change from red to blue one:当从红色变为蓝色时会发生什么:

self.window.children["!frame2"].children["!canvas"].pack_forget()
self.window.children["!frame2"].children["!text"].pack(fill="both", expand=True)

And whats happening when change from blue to red one:当从蓝色变为红色时会发生什么:

self.window.children["!frame2"].children["!text"].pack_forget()
self.window.children["!frame2"].children["!canvas"].pack(fill="both", expand=True)

Look at the video here to see the problem in action :)观看此处的视频以了解实际问题:)

What I would do is use an intermediate frame which controls the size.我会做的是使用一个控制大小的中间框架。 Then, you can pack either the red or blue frame inside this frame.然后,您可以将红色或蓝色框架装入此框架内。

Start by creating a subframe with the size you want.首先创建一个您想要的大小的子框架。 Lets say you want the area to be 500x500:假设您希望该区域为 500x500:

subframe = tk.Frame(contentFrame, width=500, height=500)
subframe.pack(fill="both", expand=True)

Next, turn geometry propagation off so that the frame won't grow or shrink to fit its children:接下来,关闭几何传播,以便框架不会增长或收缩以适应其子项:

subframe.pack_propagate(False)

Finally, put your red and blue frames inside this subframe:最后,把你的红色和蓝色框架放在这个子框架内:

canvas = tkinter.Canvas(master=subframe, bg="#F00", highlightthickness=0, borderwidth=0)
xmlEditor = tkinter.Text(master=subframe, bg="#00F", highlightthickness=0, borderwidth=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM