英文:
Why doesn't lift change the stack order in tkinter?
问题
I have this code as an example:
import tkinter as tk
class CustomWidget(tk.Canvas):
def __init__(self, master, bg_color, width=100, height=100, *args, **kwargs):
super().__init__(master, width=width, height=height, *args, **kwargs)
self.configure(bg=bg_color)
self.create_rectangle(0, 0, width, height, fill=bg_color, width=0)
class CustomWidgetFrame(tk.Frame):
def __init__(self, master, bg_color, title, *args, **kwargs):
super().__init__(master, *args, **kwargs)
# Create and add your custom widget as a child of the frame
self.custom_widget = CustomWidget(self, bg_color)
self.custom_widget.pack()
# Add other widgets to the frame as needed
self.label = tk.Label(self, text=title)
self.label.pack()
# Create the root window and add the custom widget frames to it
root = tk.Tk()
custom_widget_frame1 = CustomWidgetFrame(root, "red", "Widget1")
custom_widget_frame1.pack()
custom_widget_frame2 = CustomWidgetFrame(root, "blue", "Widget2")
custom_widget_frame2.pack()
# Bring the blue widget frame to the front of the red widget frame
custom_widget_frame2.lift(custom_widget_frame1)
# Start the main event loop
root.mainloop()
CustomWidget
是一个具有特定颜色和尺寸的矩形框。
CustomWidgetFrame
是包含上述矩形框和标签的容器。
代码的其余部分是标准的 tkinter 和 Python 代码:
- 创建根元素 -
root = tk.Tk()
- 创建
CustomWidgetFrame
,设置颜色和标签名称,然后使用pack()
方法打包它。 - 重复步骤 2。
- 尝试使用
lift()
方法将蓝色框移到红色框之上。
为什么 lift()
方法没有实现我的目标(将蓝色框移到红色框之上),以及当然有什么实际方法可以实现这个目标?
我假设将自定义部件垂直堆叠(通过 pack()
方法),最佳方法是将它存储在一个容器中(该容器包含我的自定义部件和标签等),然后只需使用 lift()
方法移动它。
英文:
I have this code as an example:
import tkinter as tk
class CustomWidget(tk.Canvas):
def __init__(self, master, bg_color, width=100, height=100, *args, **kwargs):
super().__init__(master, width=width, height=height, *args, **kwargs)
self.configure(bg=bg_color)
self.create_rectangle(0, 0, width, height, fill=bg_color, width=0)
class CustomWidgetFrame(tk.Frame):
def __init__(self, master, bg_color, title, *args, **kwargs):
super().__init__(master, *args, **kwargs)
# Create and add your custom widget as a child of the frame
self.custom_widget = CustomWidget(self, bg_color)
self.custom_widget.pack()
# Add other widgets to the frame as needed
self.label = tk.Label(self, text=title)
self.label.pack()
# Create the root window and add the custom widget frames to it
root = tk.Tk()
custom_widget_frame1 = CustomWidgetFrame(root, "red", "Widget1")
custom_widget_frame1.pack()
custom_widget_frame2 = CustomWidgetFrame(root, "blue", "Widget2")
custom_widget_frame2.pack()
# Bring the blue widget frame to the front of the red widget frame
custom_widget_frame2.lift(custom_widget_frame1)
# Start the main event loop
root.mainloop()
CustomWidget
is just a box of a certain color and dimesions.
CustomWidgetFrame
is the container of said box with a label.
The reset of the code is straight forward tkinter and python:
- Make a root element -
root = tk.Tk()
- Create the
CustomWidgetFrame
, setting the color and label name, then pack() it. - Do step 2 again.
- Try to raise the blue box over the red box.
Why is lift
not accomplishing my goal here (moving the blue box over the red box), and of course what is a practical way to accomplish this goal?
I assumed that the best way to make my custom widget stack vertically (by pack()
ing it) was to store it in a container (that had my custom widget and labels and this that and what not), then just pack()
the container and move it with lift()
.
答案1
得分: 0
lift
在 z 轴上起作用,所以只有当小部件重叠时,你才会看到效果。在你的代码中,lift
的功能是按设计工作的,但小部件不重叠,所以你看不到效果。
如果你真正想沿着 y 轴移动小部件,你可以使用 pack
命令的 before
或 after
选项来指定顺序。
custom_widget_frame2.pack(before=custom_widget_frame1)
英文:
lift
works in the z-axis, so you'll only see the effect if the widgets overlap. In your code, lift
is working as designed but the widgets don't overlap so you can't see the effect.
If what you really want to to move the widgets along the y axis you can specify the order by using the before
or after
options to the pack
command.
custom_widget_frame2.pack(before=custom_widget_frame1)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论