我有一个类中有两个单选按钮的 GUI,使用 tkinter 的第一个单选按钮旨在将变量 save_to_excel 设置为 True,而第二个单选按钮应为 false,这是为了让用户确定是否要保存信息作为 Excel 工作表或文本文档。我有一个旧版本的代码,可以工作并正确更新变量,但是新版本必须更改某些内容,而新版本无法正确更新它。
当前代码(不起作用):
class DICOMExtractorApp:
def __init__(self, root):
try:
self.root = root
self.root.title("DICOM Info Extractor")
self.root.configure(bg="white")
# Initializing variables
self.folder_path = ""
self.output_path = ""
self.save_to_excel = tk.BooleanVar(value=True) # Default to saving to Excel
self.dicom_tags = {}
self.default_tags = {
"Filename": "filename",
"Acquisition Date": (0x0008, 0x0022),
"Manufacturer": (0x0008, 0x0070),
"Study Description": (0x0008, 0x1030),
"Instance Number": (0x0020, 0x0013),
"Series Description": (0x0008, 0x103E),
"Acquisition Time": (0x0008, 0x0032),
"Modality": (0x0008, 0x0060),
"Station Name": (0x0008, 0x1010)
}
self.selected_tags = {tag: tk.BooleanVar(value=True) for tag in self.default_tags}
self.tags_per_page = 10
self.current_page = 0
self.create_widgets()
except Exception as e:
self.log_error(e)
...
tk.Radiobutton(self.root, text="Save to Excel", variable=self.save_to_excel, value=True, bg="white", command=self.print_save_option).place(x=350, y=20)
tk.Radiobutton(self.root, text="Save to Text", variable=self.save_to_excel, value=False, bg="white", command=self.print_save_option).place(x=460, y=20)
旧代码:|| |当我选择“保存到文本”单选按钮时,我希望输出打印 False,当我选择“保存到 Excel”单选按钮时,输出打印 True...
class DICOMExtractorApp:
def __init__(self, root):
self.root = root
self.root.title("DICOM Info Extractor")
self.root.configure(bg="white")
self.folder_path = ""
self.output_path = ""
self.save_to_excel = tk.BooleanVar(value=True)
self.dicom_tags = self.load_dicom_tags()
self.default_tags = {
"Filename": "filename",
"Acquisition Date": (0x0008, 0x0022),
"Manufacturer": (0x0008, 0x0070),
"Study Description": (0x0008, 0x1030),
"Instance Number": (0x0020, 0x0013)
}
self.selected_tags = {tag: tk.BooleanVar() for tag in self.default_tags}
self.create_widgets()
...
tk.Radiobutton(self.root, text="Save to Excel", variable=self.save_to_excel, value=True, bg="white",command=self.print_save_option).place(x=350, y=20)
tk.Radiobutton(self.root, text="Save to Text", variable=self.save_to_excel, value=False, bg="white",command=self.print_save_option).place(x=460, y=20)
I expect the output to print False when I select 'save to text' radio button and True when I select 'save to excel' radiobutton...
问题出在如何初始化
selected_tags
字典。在当前代码中,将
selected_tags
初始化为包含
default_tags
中每个标签的布尔变量的字典。但是,在初始化这些布尔变量时没有将它们与
self.save_to_excel
变量相关联。
以下是修复代码的方法:
class DICOMExtractorApp:
def __init__(self, root):
try:
self.root = root
self.root.title("DICOM Info Extractor")
self.root.configure(bg="white")
# Initializing variables
self.folder_path = ""
self.output_path = ""
self.save_to_excel = tk.BooleanVar(value=True) # Default to saving to Excel
self.dicom_tags = {}
self.default_tags = {
"Filename": "filename",
"Acquisition Date": (0x0008, 0x0022),
"Manufacturer": (0x0008, 0x0070),
"Study Description": (0x0008, 0x1030),
"Instance Number": (0x0020, 0x0013),
"Series Description": (0x0008, 0x103E),
"Acquisition Time": (0x0008, 0x0032),
"Modality": (0x0008, 0x0060),
"Station Name": (0x0008, 0x1010)
}
# 正确初始化 selected_tags
self.selected_tags = {tag: tk.BooleanVar(value=self.save_to_excel.get()) for tag in self.default_tags}
self.tags_per_page = 10
self.current_page = 0
self.create_widgets()
except Exception as e:
self.log_error(e)
...
tk.Radiobutton(self.root, text="Save to Excel", variable=self.save_to_excel, value=True, bg="white", command=self.print_save_option).place(x=350, y=20)
tk.Radiobutton(self.root, text="Save to Text", variable=self.save_to_excel, value=False, bg="white", command=self.print_save_option).place(x=460, y=20)
通过在
selected_tags
初始化期间使用
self.save_to_excel.get()
,可以确保每个标签的布尔变量都设置为与
self.save_to_excel
变量的当前值相同。这样,当更改
self.save_to_excel
时,
selected_tags
中的相应布尔变量也会更新。