如何從類別存取變數資料
問題摘要
您有多個頁面的小部件在多頁面Tkinter 應用程式中收集使用者輸入。每個頁面都在主應用程式中實例化為一個類,但您在從一個頁面 (PageOne) 的另一個頁面 (PageTwo) 中的小部件獲取 StringVar 的值時遇到困難。
解決方案:利用你的控制器
利用控制器,你可以在頁面之間建立通訊。首先,在每個頁面中儲存控制器的參考:
<code class="python">class PageOne(ttk.Frame): def __init__(self, parent, controller): self.controller = controller ... class PageTwo(ttk.Frame): def __init__(self, parent, controller): self.controller = controller ...</code>
其次,在控制器中合併一個方法,根據其類別名稱檢索頁面:
<code class="python">class MyApp(Tk): ... def get_page(self, classname): '''Returns an instance of a page given it's class name as a string''' for page in self.frames.values(): if str(page.__class__.__name__) == classname: return page return None</code>
使用此方法,頁面可以透過呼叫函數並獲得對目標頁面的引用來相互存取。隨後,您可以存取該頁面的共用成員,如下所示:
<code class="python">class PageTwo(ttk.Frame): ... def print_it(self): page_one = self.controller.get_page("PageOne") value = page_one.some_entry.get() print ('The value stored in StartPage some_entry = %s' % value)</code>
替代解決方案:在控制器中儲存資料
為了避免頁面之間的緊密耦合,請考慮將共享資料儲存在控制器。此方法允許頁面獨立於彼此的內部設計:
<code class="python">class MyApp(Tk): def __init__(self): ... self.app_data = {"name": StringVar(), "address": StringVar(), ... }</code>
修改頁面在創建小部件時引用控制器:
<code class="python">class PageOne(ttk.Frame): def __init__(self, parent, controller): self.controller=controller ... self.some_entry = ttk.Entry(self, textvariable=self.controller.app_data["name"], ...) </code>
最後,從控制器存取數據,消除需要get_page:
<code class="python"> def print_it(self): value = self.controller.app_data["address"].get() ...</code>
以上是如何從一個 Tkinter 頁面到另一個頁面存取 StringVar 資料?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python不是嚴格的逐行執行,而是基於解釋器的機制進行優化和條件執行。解釋器將代碼轉換為字節碼,由PVM執行,可能會預編譯常量表達式或優化循環。理解這些機制有助於優化代碼和提高效率。

可以使用多種方法在Python中連接兩個列表:1.使用 操作符,簡單但在大列表中效率低;2.使用extend方法,效率高但會修改原列表;3.使用 =操作符,兼具效率和可讀性;4.使用itertools.chain函數,內存效率高但需額外導入;5.使用列表解析,優雅但可能過於復雜。選擇方法應根據代碼上下文和需求。

有多種方法可以合併Python列表:1.使用 操作符,簡單但對大列表不內存高效;2.使用extend方法,內存高效但會修改原列表;3.使用itertools.chain,適用於大數據集;4.使用*操作符,一行代碼合併小到中型列表;5.使用numpy.concatenate,適用於大數據集和性能要求高的場景;6.使用append方法,適用於小列表但效率低。選擇方法時需考慮列表大小和應用場景。

CompiledLanguagesOffersPeedAndSecurity,而interneterpretledlanguages provideeaseafuseanDoctability.1)commiledlanguageslikec arefasterandSecureButhOnderDevevelmendeclementCyclesclesclesclesclesclesclesclesclesclesclesclesclesclesclesclesclesclesandentency.2)cransportedeplatectentysenty

Python中,for循環用於遍歷可迭代對象,while循環用於條件滿足時重複執行操作。 1)for循環示例:遍歷列表並打印元素。 2)while循環示例:猜數字遊戲,直到猜對為止。掌握循環原理和優化技巧可提高代碼效率和可靠性。

要將列表連接成字符串,Python中使用join()方法是最佳選擇。 1)使用join()方法將列表元素連接成字符串,如''.join(my_list)。 2)對於包含數字的列表,先用map(str,numbers)轉換為字符串再連接。 3)可以使用生成器表達式進行複雜格式化,如','.join(f'({fruit})'forfruitinfruits)。 4)處理混合數據類型時,使用map(str,mixed_list)確保所有元素可轉換為字符串。 5)對於大型列表,使用''.join(large_li

pythonuseshybridapprace,ComminingCompilationTobyTecoDeAndInterpretation.1)codeiscompiledtoplatform-Indepententbybytecode.2)bytecodeisisterpretedbybythepbybythepythonvirtualmachine,增強效率和通用性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

禪工作室 13.0.1
強大的PHP整合開發環境

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。