1

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 run():
        pass

    def cb_command():
        f1 = fname1.get()
        messagebox.showinfo("First Name", f1)
        if cbVar.get() == 1:
            messagebox.showinfo(cbVar.get())
            my_button['state'] = 'active'
        else: 
            messagebox.showinfo("Not found!")
            my_button['state'] = 'disabled'

    root = Tk()
    root.geometry("200x200")
    fname = Label(root, text="First Name")
    fname.grid(row= 0, column = 0, sticky = "news", padx=5, pady=5)
    fname1 = Entry(root, width = 10)
    fname1.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)
    my_button = Button(root, text = "Run", bg = '#333333', fg='#ffffff', font = 'Helvetica', command = run, state='disable')
    my_button.grid(row = 2, 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?

Update: Sorry, that I didn't clarify what actions to be done. I want the checkbox when clicked impact the state of the "Run" button in the toplevel window. The actual actions are based on the "Run" button.

Thank you Thingamabobs for suggesting a very simple solution. Just replaced one instance of Tk with Toplevel and it works.

  • 1
    Do not use two instances of `Tk`, use `Toplevel` instead. – Thingamabobs Oct 23 '22 at 15:12
  • What is your goal with the Checkbutton? The info of the messagebox seems to be some kind of login? If so you could just use an Entry and a button... – A J Oct 23 '22 at 16:14

1 Answers1

0
from tkinter import *

def new_window():
    second_window = Toplevel()
    def checkbutton_checked():
        # If you just want to take some action, once the checkbutton has been checked, you could do this here
        # Alternatively you could also add a button to the toplevel and on click check the value of
        # the checkbutton and perform actions based on that...
        cb1.configure(text="Checkbutton checked")
    cb1 = Checkbutton(second_window, text="Check here", command=checkbutton_checked)
    cb1.pack()

window = Tk()
b1 = Button(window, text="Open new window", command=new_window)
b1.pack()
window.mainloop()

I hope this provides some help and you can solve your problem, if not let me know please. Further details about the purpose of the checkbutton would also help me.

A J
  • 96
  • 5