我遇到的問題專門與 Label 小部件有關。在鏈接的代碼中,Text 小部件和 Button 小部件的測驗都獲取繼承的值并正確顯示它。
您可以在PageTwo 類中找到有問題的標簽小部件
我試圖在標簽中繼承和顯示的變數是第一類中設定的“ num ”變數。
目標是獲取該變數,在另一個類中設定值,然后稍后在 Label 小部件中顯示新設定的值。
我試圖設定 Label 以在 f 字串中直接顯示變數作為 str 值,以及在 PageTwo 中設定區域變數以獲取 TestClass.num 的值
有問題的代碼示例是:
import tkinter as tk
class TestClass(tk.Tk):
num = None
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Game")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(PageOne)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self,
text="Make a selection",
wraplength=450, justify='center')
label.pack(padx=10, pady=10)
TestClass.num = tk.StringVar()
tk.Radiobutton(self, text="1", variable=TestClass.num, value="1", ).pack()
tk.Radiobutton(self, text="2", variable=TestClass.num, value="2", ).pack()
tk.Radiobutton(self, text="3", variable=TestClass.num, value="3", ).pack()
view_selection = tk.Button(self, text="test selection", command=lambda: print(TestClass.num.get()))
view_selection.pack()
next_page = tk.Button(self, text="Next Page",
command=lambda: controller.show_frame(PageTwo))
next_page.pack(pady=10, padx=10)
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
# label = tk.Label(self, text=TestClass.num.get()
label = tk.Label(self, text=f"The number should show up here -> {TestClass.num.get()} <- ")
label.pack()
text1 = tk.Text(self)
text1.pack()
see_num = tk.Button(self, text="View Number",
command=lambda: text1.insert('1.0', TestClass.num.get()))
see_num.pack(pady=10, padx=10)
app = TestClass()
app.mainloop()
uj5u.com熱心網友回復:
一段時間以來,我已經遇到過控制器的這種用法,但從來沒有真正知道這個想法是從哪里來的。無論如何,在這里我看到問題實際上可能出在程式流中(當您實體化時,F(container, self)您正在執行__init__該類的,因此已經設定了值。
當您選擇單選按鈕中的每個專案時,您希望標簽的值能夠適當地自行編輯。因此,我認為靜態變數更合適(訪問另一個類中的小部件),您可以使用它command來觸發回呼。
此外,您還需要通過為不等于of或給每個單選按鈕不同的初始值或使用不使用 this的方式來解決您的三態問題。RadiobuttonStringVartristatevalueRadiobuttontristatevaluettk.Radiobuttontristatevalue
所以要做的改變PageTwo是創建一些靜態變數:
class PageTwo(tk.Frame):
label = ''
text = ''
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
PageTwo.text = "The number should show up here -> {} <- " # {} so we can `format` it to be whatever text we want, later
PageTwo.label = tk.Label(self, text=self.text.format(''))
PageTwo.label.pack()
...
...
在 中PageOne,您需要為command每次Radiobutton更改選項時都會呼叫它。
class PageOne(tk.Frame):
def __init__(self, parent, controller):
...
...
TestClass.num = tk.StringVar(value=' ')
command = lambda *args: PageTwo.label.config(text=PageTwo.text.format(TestClass.num.get()))
tk.Radiobutton(self, text="1", variable=TestClass.num, value="1",command=command).pack()
tk.Radiobutton(self, text="2", variable=TestClass.num, value="2",command=command).pack()
tk.Radiobutton(self, text="3", variable=TestClass.num, value="3",command=command).pack()
...
...
我認為可能的另一種方法是創建一個函式來創建小部件,然后在應該顯示頁面時加載它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/420080.html
標籤:
上一篇:字典中的Starters串列
下一篇:python包中的物件是什么?
