簡體   English   中英

Python tkinter 框架在另一個框架中具有多個小部件

[英]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.Labeltk.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 = 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 單擊任何按鈕時,前者將引發異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM