[英]Python tkinter Frame with multiple widgets within another Frame
我想将我的一个 python ButtonWindow
框架( ButtonWindow
)放置在我的另一个框架( MainWindow
)中,这样当我运行应用程序时, ButtonWindow
中的小部件与MainWindow
小部件一起出现在MainWindow
。
在下面的代码中, ButtonWindow
中的按钮与MainWindow
Label 一起存在,但缺少ButtonWindow
Label。
我查看了Frame inside another frame In python Tkinter中的答案,并尝试将背景设置为紫色以了解ButtonWindow
的边框实际上在哪里,但我看不到任何紫色?
谢谢你的帮助!
import tkinter as tk
class ButtonWindow(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.bd = 5
self.bg = "purple"
self.label = tk.Label(text="Button window", font=12)
for i in range(3):
self.button = ttk.Button(text="button", command= lambda: button_fun())
self.button.pack(side = tk.LEFT)
def button_fun(self):
pass
class MainWindow(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.label = tk.Label(text="Main Window", font=12)
self.label.pack(pady=10,padx=10)
self.button_window = ButtonWindow()
self.button_window.pack()
app = MainWindow()
app.mainloop()
如果我理解正确,您想要的是创建一个主框架,其中包含另一个框架,其中包括 3 个按钮。
在这种情况下,我稍微更改了您的代码来做到这一点。 其中一项更改是替换tk.Label
的tk.LabelFrame
(这更正了ButtonWindow
中缺少的标签,如您所述)。
我建议的第二个更改是将MainFrame
作为父框架传递给ButtonWindow
。 为此,我在MainWindow
类中创建了myCoreFrame
。 此外,对于所有小部件,我都设置了父框架。
import tkinter as tk
class ButtonWindow():
def __init__(self, Frame, *args, **kwargs):
self.label = tk.LabelFrame(Frame, text="Button window", font=12, bg = "purple")
self.label.pack()
for i in range(3):
self.button = tk.Button(self.label, text="button", command= lambda: button_fun())
self.button.pack(side = tk.LEFT)
def button_fun(self):
pass
class MainWindow():
def __init__(self, window, *args, **kwargs):
myCoreFrame = tk.Frame(window)
myCoreFrame.pack()
self.label = tk.LabelFrame(myCoreFrame, text="Main Window", font=12, bg = "red")
self.label.pack(pady=10,padx=10)
self.button_window = ButtonWindow(self.label)
root = tk.Tk()
app = MainWindow(root)
root.mainloop()
问题是您没有将标签和按钮放在框架内。 您需要将框架显式设置为按钮和标签的父级。 如果不这样做,小部件将成为根窗口的子项。
class ButtonWindow(tk.Frame):
def __init__(self, *args, **kwargs):
...
self.label = tk.Label(self, text="Button window", font=12)
# ^^^^^^
for i in range(3):
self.button = ttk.Button(self, text="button", command= lambda: button_fun())
# ^^^^^^
...
class MainWindow(tk.Frame):
def __init__(self, *args, **kwargs):
...
self.label = tk.Label(self, text="Main Window", font=12)
# ^^^^^
self.label.pack(pady=10,padx=10)
self.button_window = ButtonWindow(self)
# ^^^^
...
创建窗口小部件时最好指定其父窗口,否则它们将是根窗口的子窗口。
此外,您从未在“按钮窗口”标签上调用任何布局函数,因此它不可见。
self.bd = 5
和self.bg = "purple"
不会改变边框宽度和背景颜色。 使用self.config(bd=5, bg="purple")
代替。
import tkinter as tk
from tkinter import ttk
class ButtonWindow(tk.Frame):
def __init__(self, master=None, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.config(bd=5, bg="purple") # replace self.bd = 5 and self.bg = "purple"
self.label = tk.Label(self, text="Button window", font=12, fg='white', bg='purple') # specify parent
self.label.pack() # pack the label, otherwise it is not visible
for i in range(3):
self.button = ttk.Button(self, text="button", command=self.button_fun) # specify parent
self.button.pack(side=tk.LEFT)
def button_fun(self):
pass
class MainWindow(tk.Frame):
def __init__(self, master=None, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.label = tk.Label(self, text="Main Window", font=12) # specify parent
self.label.pack(pady=10, padx=10)
self.button_window = ButtonWindow(self) # specify parent
self.button_window.pack()
root = tk.Tk() # create root window explicitly
MainWindow(root).pack()
root.mainloop()
此外,我已将command=lambda: button_fun()
更改为command=self.button_fun
。 单击任何按钮时,前者将引发异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.