dimanche 23 octobre 2022

Checkbutton does not work on a nested tkinter window

I am trying to create a Tkinter window with a button which when clicked will provide with a new window. The new window has a checkbox and I want some actions to be done based on the checkbox value.

from tkinter import *
from tkinter import messagebox

def my_command():

    def cb_command():
        firstname = fname_entry.get()
        messagebox.showinfo("First Name", firstname)
        if cbVar.get() == 1:
            messagebox.showinfo(cbVar.get())
        else: messagebox.showinfo("Not found!")

    root = Tk()
    root.geometry("200x200")
    fname = Label(root, text="First Name")
    fname.grid(row= 0, column = 0, sticky = "news", padx=5, pady=5)
    fname_entry = Entry(root, width = 10)
    fname_entry.grid(row =0, column = 1, sticky = "news", padx=5, pady=5)
    cbVar = IntVar()
    cb1 = Checkbutton(root, text="Please check this", variable=cbVar, onvalue=1, offvalue=0, command=cb_command)
    cb1.grid(row = 1, column = 0)
    root.mainloop()

window = Tk()
window.geometry("200x200")
button1 = Button(window, text = "Run", command = my_command)
button1.pack()
window.mainloop()

I wrote this simple code which works fine with all other entry widgets. However, the checkbutton in the new window does not work. Can someone suggest any alternative?




Aucun commentaire:

Enregistrer un commentaire