【问题标题】:Python tkinter Frame with multiple widgets within another FramePython tkinter Frame,在另一个 Frame 中有多个小部件
【发布时间】:2021-10-20 12:37:50
【问题描述】:

我想将我的一个 python tkinter 框架 (ButtonWindow) 放置在我的另一个框架 (MainWindow) 中,这样当我运行应用程序时,ButtonWindow 中的小部件就会出现在 MainWindow 中使用 MainWindow 小部件。

在下面的代码中,来自ButtonWindow 的按钮与MainWindow 标签一起出现,但缺少ButtonWindow 标签。

我查看了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()

【问题讨论】:

  • 您实际上并没有在任何一个框架内放置任何东西。所有widget构造函数的第一个参数是父widget,默认是根窗口。
  • 除上述问题外,您从未在ButtonWindow 内部的self.label(“按钮窗口”)上调用任何布局函数。同样self.bd = 5self.bg = "purple" 不改变ButtonWindow 的边框宽度和背景颜色。请改用self.config(bd=5, bg="purple")

标签: python tkinter


【解决方案1】:

创建小部件时最好指定其父级,否则它们将成为根窗口的子级。

另外,您从未在 “按钮窗口” 标签上调用过任何布局函数,因此它是不可见的。

self.bd = 5self.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。单击任何按钮时,前者都会引发异常。

【讨论】:

    【解决方案2】:

    如果我理解正确,您想要的是创建一个主框架,其中包含另一个框架,其中包括 3 个按钮。

    在这种情况下,我稍微更改了您的代码来做到这一点。其中一项更改是将tk.Label 替换为tk.LabelFrame(如您所述,这更正了ButtonWindow 中缺少的标签)。

    我建议的第二个更改是将MainFrame 作为父框架传递给ButtonWindow。为此,我在MainWindowclass 中创建了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()
    

    【讨论】:

      【解决方案3】:

      问题在于您没有将标签和按钮放在框架内。您需要将框架显式设置为按钮和标签的父级。如果不这样做,小部件将成为根窗口的子窗口。

      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)
              #                                 ^^^^
              ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-21
        • 2016-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-14
        相关资源
        最近更新 更多