如何从另一个框架获取 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 的代码被遗漏了。它现在只有一个按钮,单击时可以调用函数,但问题是我不确定如何将 keywords.get() 从 EntryFrame 获取到我的 ButtonFrame
全局字典方法仅适用于检查按钮
有人可以帮助我吗?谢谢你!
I have a .ini file which holds the default values for the Checkbuttons/Radiobuttons/Entry components of my app.
I want to use a 'SAVE' button in another frame to save the updated values by user and update the ini file.
Initially I tried to bypass the problem by having all the values in a dictionary, then make each Checkbuttons to call a method to update the dictionary, but then I realized I cannot call a command inside ttk.Entry()
...then how should I retrieve its value in my 'SAVE' button frame?
My code so far:
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)
Code for my ButtonFrame is left out. It just has a button now which is ready to call a function when clicked, but the problem is I'm not sure how to get the keyword.get() from EntryFrame to my ButtonFrame
The global dictionary method only worked for the Checkbuttons
Can someone please help me? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过花费一些时间来理解如何从TKINTER中的不同类中访问变量?
控制器本质上是包含所有其他帧/组件的主要应用程序,因此我只需要在其中声明我的TK变量并传递到每个子帧。
I was able to solve my issue by spending some time to understand the concept in How to access variables from different classes in tkinter?
Controller is essentially the main app which contains all other frames/components,so I just needed to declare my tk variables in there and pass to each sub-frames.