具有多个选项卡、框架和类的 CustomTkinter - 获取小部件值

问题描述 投票:0回答:1

我有一个 ctk 应用程序,其中许多小部件分布在多个选项卡和框架中。输入如此广泛,我最终希望用户能够将他们的条目保存到命名文件中并稍后检索它们。目前,我正在努力获取单击“保存”按钮时分布在所有选项卡上的所有小部件值。保存按钮位于一帧上,但我在各个类中的编码函数无法看到彼此。

下面提供了一个涵盖复杂性的最小示例:

import customtkinter as ctk
from tkinterdnd2 import TkinterDnD, DND_ALL
from tkinter import StringVar

class TabView(ctk.CTkTabview, TkinterDnD.DnDWrapper):
    def __init__(self, master):
        super().__init__(master)
        self.TkdndVersion = TkinterDnD._require(self)
        global path_string

        self.add('Tab 1')
        self.add('Tab 2')

        # Input file drag and drop.
        path_string = StringVar(value='Drop a file here')
        self.PathLabel = ctk.CTkLabel(master=self.tab('Tab 1'), text='Input File', anchor='w')
        self.PathLabel.grid(row=0, column=0, padx=10, pady=10, sticky='ew')
        self.PathEntry = ctk.CTkEntry(master=self.tab('Tab 1'), textvariable=path_string)
        self.PathEntry.grid(row=0, column=1, columnspan=3, padx=10, pady=10, sticky='ew')
        self.PathEntry.drop_target_register(DND_ALL)
        self.PathEntry.dnd_bind('<<Drop>>', self.fn_get_path)

        # Number of shuffles.
        self.ShuffleEntry = ctk.CTkEntry(master=self.tab('Tab 1'), textvariable=ctk.StringVar(value='10'))
        self.ShuffleEntry.grid(row=1, column=1, padx=10, pady=10, sticky='ew')

        # Number of units.
        self.UnitsEntry = ctk.CTkEntry(master=self.tab('Tab 2'), textvariable=ctk.StringVar(value='1000'))
        self.UnitsEntry.grid(row=1, column=1, padx=10, pady=10, sticky='ew')

        # Settings frame with save now button.
        self.SettingsFrame = SettingsFrame(master=self.tab('Tab 2'))
        self.SettingsFrame.grid(row=2, column=1, padx=20, pady=20)

    def fn_get_path(self, event):
        path_string.set(value=event.data.strip('{}'))

    def fn_save_now(self):
        App.fn_save_settings(TabView)
        return

class SettingsFrame(ctk.CTkFrame):
    def __init__(self, master):
        super().__init__(master)
        self.SaveNowButton = ctk.CTkButton(master=self, width=40, height=20, text='Save',
                                           command=lambda: TabView.fn_save_now(self))
        self.SaveNowButton.grid(row=5, column=4, columnspan=1, padx=10, pady=10, sticky='ew')

class App(ctk.CTk):
    def __init__(self):
        super().__init__()
        self.tab_view = TabView(master=self)
        self.tab_view.grid(row=0, column=0, padx=20, pady=20)

    def fn_save_settings(self):
        Setting_1 = path_string.get()
        Setting_2 = self.tab_view.ShuffleEntry.get() # Gives a no attribute error
        Setting_3 = self.tab_view.UnitsEntry.get()   # Gives a no attribute error
        print(Setting_1, Setting_2, Setting_3)
        return

app = App()
app.mainloop()

在这种情况下,用户可以将输入文件放入 PathEntry 框中并指定几个其他条目。我希望他们能够通过单击第二个选项卡上的“保存”按钮随时保存其条目。我正在设法检索 path_string,但无法检索其他两个感兴趣的设置。如何检索Setting_2和Setting_3?我尝试过使用 tab_view / master / controller 选项但没有成功,并且我尝试过移动功能。如果我可以检索所有值,那么我希望我能够编写代码来保存它们,但任何进一步的提示将不胜感激。

r function tabs frames customtkinter
1个回答
0
投票

我找到的解决方案是删除 SettingsFrame 类并将内容放入 TabView 类中:

import customtkinter as ctk
from tkinterdnd2 import TkinterDnD, DND_ALL
from tkinter import StringVar

class TabView(ctk.CTkTabview, TkinterDnD.DnDWrapper):
    def __init__(self, master):
        super().__init__(master)
        self.TkdndVersion = TkinterDnD._require(self)
        global path_string

        self.add('Tab 1')
        self.add('Tab 2')

        # Input file drag and drop.
        path_string = StringVar(value='Drop a file here')
        self.PathLabel = ctk.CTkLabel(master=self.tab('Tab 1'), text='Input File', anchor='w')
        self.PathLabel.grid(row=0, column=0, padx=10, pady=10, sticky='ew')
        self.PathEntry = ctk.CTkEntry(master=self.tab('Tab 1'), textvariable=path_string)
        self.PathEntry.grid(row=0, column=1, columnspan=3, padx=10, pady=10, sticky='ew')
        self.PathEntry.drop_target_register(DND_ALL)
        self.PathEntry.dnd_bind('<<Drop>>', self.fn_get_path)

        # Number of shuffles.
        self.ShuffleEntry = ctk.CTkEntry(master=self.tab('Tab 1'), textvariable=ctk.StringVar(value='10'))
        self.ShuffleEntry.grid(row=1, column=1, padx=10, pady=10, sticky='ew')

        # Number of units.
        self.UnitsEntry = ctk.CTkEntry(master=self.tab('Tab 2'), textvariable=ctk.StringVar(value='1000'))
        self.UnitsEntry.grid(row=1, column=1, padx=10, pady=10, sticky='ew')

        # Settings frame with save now button.
        self.settings_frame()

    def fn_get_path(self, event):
        path_string.set(value=event.data.strip('{}'))

    def settings_frame(self):
        self.SettingsFrame = ctk.CTkFrame(master=self.tab('Tab 2'))
        self.SettingsFrame.grid(row=2, column=1, padx=20, pady=20)

        self.SaveNowButton = ctk.CTkButton(self.SettingsFrame, width=40, height=20, text='Save',
                                           command=lambda: self.fn_save_now())
        self.SaveNowButton.grid(row=5, column=4, columnspan=1, padx=10, pady=10, sticky='ew')

    def fn_save_now(self, *_args):
        App.fn_save_settings(self)
        return

class App(ctk.CTk):
    def __init__(self):
        super().__init__()
        self.tab_view = TabView(master=self)
        self.tab_view.grid(row=0, column=0, padx=20, pady=20)

    def fn_save_settings(self):
        Setting_1 = path_string.get()
        Setting_2 = self.ShuffleEntry.get() # Gives a no attribute error
        Setting_3 = self.UnitsEntry.get()   # Gives a no attribute error
        print(Setting_1, Setting_2, Setting_3)
        return

app = App()
app.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.