如何从另一帧获取Checkbutton/Entry/RadioButton值



我有一个.ini文件,其中包含我的应用程序的Checkbuttons/Radiobuttons/Entry组件的默认值
我想在另一个框架中使用"保存"按钮来保存用户更新的值并更新ini文件

起初,我试图通过将所有值都放在字典中来绕过这个问题,然后让每个Checkbuttons调用一个方法来更新字典,但后来我意识到我不能在ttk.Entry()中调用命令。。。那么我应该如何在"保存"按钮框中检索它的值

到目前为止我的代码:

import tkinter as tk
from tkinter import ttk
import configparser
class iniParser(configparser.ConfigParser):
"""Return dictionary with the config information"""
def as_dict(self):
d = dict(self._sections)
for k in d:
d[k] = dict(self._defaults, **d[k])
d[k].pop('__name__', None)
return d
global d
parser = iniParser()
parser.read('Config.ini')
d = parser.as_dict()['CONFIG']
class CheckButtonFrame(ttk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.rowconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.__create_widgets()

def __create_widgets(self):
# Create control variables
self.check1 = tk.BooleanVar(value=d['check1'])
self.check2 = tk.BooleanVar(value=d['check2'])

check_frame = ttk.LabelFrame(self, text="Checks", padding=(20, 10))
check_frame.grid(row=0, column=0, padx=(20, 10), pady=(5, 5), sticky="nsew")
# Checkbuttons
check_1 = ttk.Checkbutton(check_frame, text="Checkbox1", variable=self.check1, command=lambda:self.update_val('check1',self_check1))
check_1.grid(row=0, column=0, padx=3, pady=5, sticky=tk.NSEW)
check_2 = ttk.Checkbutton(check_frame, text="Checkbox2", variable=self.check2, command=lambda:self.update_val('check2',self_check2))
check_2.grid(row=1, column=0, padx=3, pady=5, sticky=tk.NSEW)

def update_val(self, text, variable):
d[text] = variable.get()
class EntryFrame(ttk.Frame):
def __init__(self,parent):
super.__init__(parent)
self.rowconfigure(0,weight=1)
self.rowconfigure(1,weight=1)
self.__create_widgets()
def __create_widgets(self):
self.rowconfigure(0,weight=1)
self.rowconfigure(1,weight=1)
self.path = tk.StringVar(value=d['path'])

entry_frame = ttk.LabelFrame(self, text="Path", padding=(20, 10))
entry_frame.grid(row=0, column=0, padx=(20, 10), pady=(10, 10), sticky="nsew")
ttk.Label(entry_frame, text='Path:').grid(row=0, column=0, sticky=tk.W, pady=10)
keyword = ttk.Entry(small_frame, width=30, textvariable=self.path)
keyword.focus()
keyword.grid(row=1, column=0, columnspan=2, sticky=tk.W)

我的ButtonFrame的代码被遗漏了。它现在只有一个按钮,可以在单击时调用函数,但问题是我不确定如何将关键字.get((从EntryFrame获取到我的ButtonFrame
全局字典方法只适用于Checkbuttons

有人能帮帮我吗?非常感谢。

我花了一些时间来理解"如何访问tkinter中不同类的变量?"中的概念,从而解决了我的问题?
Controller本质上是包含所有其他框架/组件的主应用程序,所以我只需要在其中声明我的tk变量并传递给每个子框架

class MainApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.rowconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.rowconfigure(2, weight=1)
self.rowconfigure(3, weight=1)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(2, weight=1)
self.config_data = {
"check1": tk.BooleanVar(value=d['check1']),
"check2": tk.BooleanVar(value=d['check2'])
}
self.__create_widgets(self.config_data)
def __create_widgets(self):
first_frame = FirstFrame(self)
button_frame = ButtonFrame(self)
...

最新更新