[Python] using checkbutton, var.get==1 doesn't work why? [duplicate]

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    update: My application is big, there is many user interfaces,I need help in "Select" button where if select one of the chechbutton, it will display on previous Gui "window" the name of products in textbox1 and total price of selection in entry_price. It doesn't disply that, why?

    class App:
    def __init__(self,main_window):
    self.main_window = main_window
    self.main_window.geometry('760x500')
    tk.Button(self.main_window, text="Enter",width=25,command=self.create_GUI).place(x=50,y=50)
    def create_GUI(self):
    self.main_window.withdraw()
    first_window = tk.Tk()
    APP_GUI(first_window, self.main_window)

    class APP_GUI():
    def __init__(self, first_window, main_window):
    self.first_window = first_window
    self.first_window.geometry("900x650")
    self.main_window = main_window
    tk.Button(self.first_window,text='Category',command=self.create_prodect_GUI).place(x=80,y=80)

    def create_prodect_GUI(self):
    def open_second_gui():
    second_window = Toplevel(window)
    second_window.title("Select Items")
    items = [
    {"name": "one", "price": 340, "var": tk.BooleanVar()},
    {"name": "two", "price": 740, "var": tk.BooleanVar()},
    {"name": "three", "price": 579, "var": tk.BooleanVar()},
    {"name": "four", "price": 340, "var": tk.BooleanVar()}, ]
    for item in items:
    tk.Checkbutton(
    second_window,text=f"{item['name']} ({item['price']})",variable=item["var"]).pack(anchor='w')

    def finalize_selection():
    total_price = 0
    selected_items = []
    for item in items:
    if item["var"].get():
    total_price += item["price"]
    selected_items.append(item["name"])

    textbox1.delete("1.0", tk.END)
    textbox1.insert(tk.END, "\n".join(selected_items))
    entry_price.delete(0, tk.END)
    entry_price.insert(0, str(total_price))
    second_window.destroy()
    tk.Button(second_window, text="OK", command=finalize_selection).pack(pady=10)


    window= tk.Tk()
    textbox1 = tk.Text(window, height=10, width=40)
    textbox1.grid(row=0, column=0, padx=10, pady=10)
    tk.Button(window, text="Select", command=open_second_gui).grid(row=0, column=1,padx=10,pady=10)
    entry_price = tk.Entry(window, width=20)
    entry_price.grid(row=1, column=0, padx=10, pady=10)

    if __name__ == "__main__":
    main_window = tk.Tk()
    app = App(main_window)
    main_window.mainloop()`

    Continue reading...

Compartilhe esta Página