我有一個主程序可以做一些很酷的事情,我目前正在設置一個“設置編輯器”來讓用戶更改一些 GUI 相關的東西和默認值。它從文本文件中讀取值,該文件被正確讀取并將它們保存到字典 self.propertiesDict 中。有些選項是開/關開關,所以我為它們使用了復選按鈕。令我困惑的是以下行為:當我直接執行 settingsEditor.py(創建設置窗口的腳本)時,代碼工作得很好。所有的檢查按鈕都設置為活動/真實。但是,當我在我的主程序中包含我的 settingsEditor 并調用它時,它創建正常但所有的復選按鈕顯示錯誤的值:False。我在這里閱讀了很多主題以找到答案,但我認為我避免了最常見的錯誤:我使用 tk 變量tk 變量在按鈕之前創建和設置變量不僅在本地范圍內(前綴為 self.)如您所見,我嘗試使用 IntVar 和 BooleanVar,但都無法正常工作。我使用 Visual Studio 進行調試,除了錯誤的顯示結果外,我在逐行調試時看不出任何區別。我很高興提出任何建議。很抱歉沒有提供完整的 MWE,如果這里沒有人可以幫助我,我會提供。settingsEditor.pyimport tkinter as tkfrom tkinter import ttk...class mySettingsEditor:? ? def __init__(self):? ? ? ? ...? ? def createGUI(self):? ? ? ? # Show main options on startup on/off? ? ? ? self.showOptionsVar = tk.IntVar()? ? ? ? self.showOptionsVar.set(str2int(self.propertiesDict['showMainOptionsExpanded']))? ? ? ? print(self.showOptionsVar.get())? ? ? ? self.checkBtn1 = tk.Checkbutton(Frame, text='Main Options Section', variable=self.showOptionsVar)? ? ? ? self.checkBtn1.grid(column=0,row=2)? ? ? ? # Show main STL section on startup on/off? ? ? ? self.showMainSTLVar = tk.BooleanVar()? ? ? ? self.showMainSTLVar.set(str2bool(self.propertiesDict['showMainSTLSectionExpanded']))? ? ? ? print(self.showMainSTLVar.get())? ? ? ? self.checkBtn2 = tk.Checkbutton(Frame, text='Main STL Section', variable=self.showMainSTLVar)? ? ? ? self.checkBtn2.grid(column=0,row=3)main.pyfrom settingsEditor import mySettingsEditor...settEditor = mySettingsEditor()這是它在單獨執行時在 GUI 中的樣子(左側有打印輸出的終端):這就是我在 main.py 中添加它時的結果。這些框未選中,但 .get() 告訴我這些值已正確分配給 tk 變量。
當從其他文件導入類時,tkinter checkbutton 不顯示正確的值
慕婉清6462132
2023-06-20 13:57:47