如何在 python 的 customtkinter 中将复选框数据传递到主窗口函数?

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

我正在使用“customtkinter”创建一个 python 应用程序。当用户单击按钮时,我需要打开另一个窗口,并且在该窗口上有 2 个选项可供选择和提交。单击提交按钮后需要关闭该窗口并将所选值传递到主窗口并将它们显示在日志文本框中。

这是我试过的;

import customtkinter

customtkinter.set_appearance_mode("System")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"


class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        # configure window
        self.title("User activity checker")
        self.geometry(f"{1100}x{580}")
        self.minsize(600, 400)

        # configure grid layout (4x4)
        self.grid_columnconfigure(1, weight=1)
        self.grid_columnconfigure((2, 3), weight=0)
        self.grid_rowconfigure((0, 1, 2), weight=1)

        # create sidebar frame with widgets
        self.sidebar_frame = customtkinter.CTkFrame(self, width=140, corner_radius=0)
        self.sidebar_frame.grid(row=0, column=0, rowspan=5, sticky="nsew")
        self.sidebar_frame.grid_rowconfigure(5, weight=1)

        self.logo_label = customtkinter.CTkLabel(self.sidebar_frame, text="User Activity",
                                                 font=customtkinter.CTkFont(size=20, weight="bold"))
        self.logo_label.grid(row=0, column=0, padx=20, pady=(20, 10))

        self.sidebar_button_1 = customtkinter.CTkButton(self.sidebar_frame, command=self.click_more_buttons,
                                                        text="Click More Buttons")
        self.sidebar_button_1.grid(row=1, column=0, padx=20, pady=10)

        self.sidebar_button_2 = customtkinter.CTkButton(self.sidebar_frame, command=self.take_user_input,
                                                        text="Take User input")
        self.sidebar_button_2.grid(row=2, column=0, padx=20, pady=10)

        self.sidebar_button_3 = customtkinter.CTkButton(self.sidebar_frame, command=self.check_user_input,
                                                        text="Check user input")
        self.sidebar_button_3.grid(row=3, column=0, padx=20, pady=10)

        self.sidebar_button_4 = customtkinter.CTkButton(self.sidebar_frame, command=self.exit,
                                                        text="Exit")
        self.sidebar_button_4.grid(row=4, column=0, padx=20, pady=10)

        self.appearance_mode_label = customtkinter.CTkLabel(self.sidebar_frame, text="Appearance Mode:", anchor="w")
        self.appearance_mode_label.grid(row=6, column=0, padx=20, pady=(10, 0))
        self.appearance_mode_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame,
                                                                       values=["Light", "Dark", "System"],
                                                                       command=self.change_appearance_mode_event)
        self.appearance_mode_optionemenu.grid(row=7, column=0, padx=20, pady=(10, 10))

        # set textbox
        self.log_label = customtkinter.CTkLabel(self, text="Logs", font=customtkinter.CTkFont(size=16, weight="bold"))
        self.log_label.grid(row=0, column=1, columnspan=2, sticky="nsew")

        self.textbox = customtkinter.CTkTextbox(self, height=400)
        self.textbox.grid(row=1, column=1, rowspan=1, columnspan=2, padx=20, pady=(20, 0), sticky="nsew")

        self.combobox = customtkinter.CTkComboBox(master=self, values=["Sample text 1", "Text 2"])
        self.combobox.grid(row=2, column=1, padx=20, pady=20, sticky="ew")
        self.button = customtkinter.CTkButton(master=self, command=self.button_callback, text="Insert Text")
        self.button.grid(row=2, column=2, padx=20, pady=20, sticky="ew")

        # set default values
        self.appearance_mode_optionemenu.set("Dark")

    def change_appearance_mode_event(self, new_appearance_mode: str):
        self.textbox.insert("insert", "change the appearance mode \n")
        customtkinter.set_appearance_mode(new_appearance_mode)

    def button_callback(self):
        self.textbox.insert("insert", self.combobox.get() + "\n")

    def click_more_buttons(self):
        self.textbox.insert("insert", "Click button 1 \n")
        w = customtkinter.CTk()
        w.geometry("300x300")
        w.minsize(300, 300)
        w.title('Select Option')

        l1 = customtkinter.CTkLabel(master=w, text="Select option to process", font=('Century Gothic', 20))
        l1.pack(padx=20, pady=20)
        checkbox_1 = customtkinter.CTkCheckBox(master=w, text="Select option 1")
        checkbox_1.pack(padx=20, pady=20)
        checkbox_2 = customtkinter.CTkCheckBox(master=w, text="Select option 2")
        checkbox_2.pack(padx=20, pady=20)
        button3 = customtkinter.CTkButton(master=w, text="Submit")
        button3.pack(padx=20, pady=20)

        w.mainloop()

    def take_user_input(self):
        self.textbox.insert("insert", "Click button 2 \n")

    def check_user_input(self):
        self.textbox.insert("insert", "Click button 3 \n")

    def exit(self):
        self.textbox.insert("insert", "Click button 4 \n")


if __name__ == "__main__":
    app = App()
    app.mainloop()

现在我需要传递选定的值并使用传递的值处理更多详细信息。如果我在开发 GUI 方面有误,请纠正我。我是 python 的新手,需要解决这个问题。

python customtkinter
1个回答
0
投票
Create a class ClickMoreButton which takes the CTkTextBox widget from your main class App. I modified your code and it looks like below:

import customtkinter
import tkinter

customtkinter.set_appearance_mode("System")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"

class ClickMoreButton(customtkinter.CTkToplevel):
    def __init__ (self, text_box=None, **kw):
      self.text_box = text_box
      self.text = ''
      super().__init__(**kw)
      self.geometry("300x300")
      self.minsize(300, 300)
      self.title('Select Option')

      self.l1 = customtkinter.CTkLabel(self, text="Select option to process", font=('Century Gothic', 20))
      self.l1.pack(padx=20, pady=20)
      self.var1 = tkinter.IntVar()
      self.checkbox_1 = customtkinter.CTkCheckBox(self, text="Select option 1", variable=self.var1,
                                                  onvalue=1,
                                                  offvalue=0,command=self.setText)
      self.checkbox_1.pack(padx=20, pady=20)
      self.var2 = tkinter.IntVar()
      self.checkbox_2 = customtkinter.CTkCheckBox(self, text="Select option 2", variable=self.var2,
                                                  onvalue=1,
                                                  offvalue=0,command=self.setText)
      self.checkbox_2.pack(padx=20, pady=20)
      self.button3 = customtkinter.CTkButton(self, text="Submit", command=self.submit)
      self.button3.pack(padx=20, pady=20)

    def setText(self):
      if self.var1.get() == 1:
        self.text = self.checkbox_1.cget('text')
      if self.var2.get() == 1:
        self.text = self.checkbox_2.cget('text')
    
    def submit(self):
      self.text_box.delete('0.0', customtkinter.END)
      self.text_box.insert('0.0', self.text)
      self.destroy()

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        # configure window
        self.title("User activity checker")
        self.geometry(f"{1100}x{580}")
        self.minsize(600, 400)

        # configure grid layout (4x4)
        self.grid_columnconfigure(1, weight=1)
        self.grid_columnconfigure((2, 3), weight=0)
        self.grid_rowconfigure((0, 1, 2), weight=1)

        # create sidebar frame with widgets
        self.sidebar_frame = customtkinter.CTkFrame(self, width=140, corner_radius=0)
        self.sidebar_frame.grid(row=0, column=0, rowspan=5, sticky="nsew")
        self.sidebar_frame.grid_rowconfigure(5, weight=1)

        self.logo_label = customtkinter.CTkLabel(self.sidebar_frame, text="User Activity",
                                                 font=customtkinter.CTkFont(size=20, weight="bold"))
        self.logo_label.grid(row=0, column=0, padx=20, pady=(20, 10))

        self.sidebar_button_1 = customtkinter.CTkButton(self.sidebar_frame, command=self.click_more_buttons,
                                                        text="Click More Buttons")
        self.sidebar_button_1.grid(row=1, column=0, padx=20, pady=10)

        self.sidebar_button_2 = customtkinter.CTkButton(self.sidebar_frame, command=self.take_user_input,
                                                        text="Take User input")
        self.sidebar_button_2.grid(row=2, column=0, padx=20, pady=10)

        self.sidebar_button_3 = customtkinter.CTkButton(self.sidebar_frame, command=self.check_user_input,
                                                        text="Check user input")
        self.sidebar_button_3.grid(row=3, column=0, padx=20, pady=10)

        self.sidebar_button_4 = customtkinter.CTkButton(self.sidebar_frame, command=self.exit,
                                                        text="Exit")
        self.sidebar_button_4.grid(row=4, column=0, padx=20, pady=10)

        self.appearance_mode_label = customtkinter.CTkLabel(self.sidebar_frame, text="Appearance Mode:", anchor="w")
        self.appearance_mode_label.grid(row=6, column=0, padx=20, pady=(10, 0))
        self.appearance_mode_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame,
                                                                       values=["Light", "Dark", "System"],
                                                                       command=self.change_appearance_mode_event)
        self.appearance_mode_optionemenu.grid(row=7, column=0, padx=20, pady=(10, 10))

        # set textbox
        self.log_label = customtkinter.CTkLabel(self, text="Logs", font=customtkinter.CTkFont(size=16, weight="bold"))
        self.log_label.grid(row=0, column=1, columnspan=2, sticky="nsew")

        self.textbox = customtkinter.CTkTextbox(self, height=400)
        self.textbox.grid(row=1, column=1, rowspan=1, columnspan=2, padx=20, pady=(20, 0), sticky="nsew")

        self.combobox = customtkinter.CTkComboBox(master=self, values=["Sample text 1", "Text 2"])
        self.combobox.grid(row=2, column=1, padx=20, pady=20, sticky="ew")
        self.button = customtkinter.CTkButton(master=self, command=self.button_callback, text="Insert Text")
        self.button.grid(row=2, column=2, padx=20, pady=20, sticky="ew")

        # set default values
        self.appearance_mode_optionemenu.set("Dark")

    def change_appearance_mode_event(self, new_appearance_mode: str):
        self.textbox.insert("insert", "change the appearance mode \n")
        customtkinter.set_appearance_mode(new_appearance_mode)

    def button_callback(self):
        self.textbox.insert("insert", self.combobox.get() + "\n")

    def click_more_buttons(self):
      self.textbox.insert("insert", "Click button 1 \n")
      my_text = ClickMoreButton(self.textbox)
      my_text.focus()
      my_text.mainloop()

    def take_user_input(self):
        self.textbox.insert("insert", "Click button 2 \n")

    def check_user_input(self):
        self.textbox.insert("insert", "Click button 3 \n")

    def exit(self):
        self.textbox.insert("insert", "Click button 4 \n")


if __name__ == "__main__":
    app = App()
    app.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.