我想將我的一個 python tkinter 框架 ( ButtonWindow) 放置在我的另一個框架 ( MainWindow) 中,這樣當我運行應用程式時,其中的小部件ButtonWindow會MainWindow與MainWindow小部件一起出現。
在下面的代碼中,Buttons fromButtonWindow與MainWindowLabel一起存在,但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()
uj5u.com熱心網友回復:
如果我理解正確,您想要的是創建一個主框架,其中包含另一個框架,其中包括 3 個按鈕。
在這種情況下,我稍微更改了您的代碼來做到這一點。其中一個變化是更換tk.Label了tk.LabelFrame(這校正了標簽,該標簽是在缺少ButtonWindow如你所說)。
我建議的第二個更改是將 傳遞MainFrame給ButtonWindow作為父框架。為此,我創建myCoreFrame了MainWindow類內部。此外,對于所有小部件,我都設定了父框架。
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()
uj5u.com熱心網友回復:
問題是您沒有將標簽和按鈕放在框架內。您需要將框架顯式設定為按鈕和標簽的父級。如果不這樣做,小部件將成為根視窗的子項。
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)
# ^^^^
...
uj5u.com熱心網友回復:
創建視窗小部件時最好指定其父視窗,否則它們將是根視窗的子視窗。
此外,您從未在“按鈕視窗”標簽上呼叫任何布局函式,因此它不可見。
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. 單擊任何按鈕時,前者將引發例外。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/327487.html
