how i can with just with the checked entry widgets

Viewed 43

Hi,

i just wanna ask you guys, if is there any way to work with just checked chexboxes

here it my code :

    from tkinter import *

window1 =Tk()
window1.geometry("400x400")
window1.resizable(False,False)
#---------------------------------------------
def test1():
    if entry1['state'] == 'normal':
        entry1['state'] = 'disabled'
    else:
        entry1['state'] = 'normal'

entry1 =Entry()
entry1.pack()
cb1 = Checkbutton(command=test1)
cb1.select()
cb1.pack()
#---------------------------------------------
def test2():
    if entry2['state'] == 'normal':
        entry2['state'] = 'disabled'
    else:
        entry2['state'] = 'normal'

entry2 =Entry()
entry2.pack()
cb2 = Checkbutton(command=test2)
cb2.select()
cb2.pack()
#---------------------------------------------
def test3():
    if entry3['state'] == 'normal':
        entry3['state'] = 'disabled'
    else:
        entry3['state'] = 'normal'

entry3 =Entry()
entry3.pack()
cb3 = Checkbutton(command=test3)
cb3.select()
cb3.pack()
#---------------------------------------------
def test4():
    if entry4['state'] == 'normal':
        entry4['state'] = 'disabled'
    else:
        entry2['state'] = 'normal'

entry4 =Entry()
entry4.pack()
cb4 = Checkbutton(command=test4)
cb4.select()
cb4.pack()
#---------------------------------------------

def message():
    b = float (entry1.get())
    c = float ( entry2.get())
    d = float ( entry3.get())
    e = float ( entry4.get())
    
    
    label1 = Label(window1, text="1) here is what you wrote ma boy :"+ str(b))
    label1.place(x=60,y=200)
    label2 = Label(window1, text="2) here is what you wrote ma boy :"+ str(c))
    label2.place(x=60,y=230)
    label3 = Label(window1, text="3) here is what you wrote ma boy :"+ str(d))
    label3.place(x=60,y=260)
    label4 = Label(window1, text="4) here is what you wrote ma boy :"+ str(e))
    label4.place(x=60,y=290)
    
button1 = Button(window1,text="Click", command= message, width=8)
button1.place(x=20,y=200)

window1.mainloop()

here is what i want :

i want to check the checkbox 2 and 4 for example and enter values and i want the code to execute just the second and the forth message. if i check the the first,second and the forth checkboxes,and enter values, i want the code to execute just the first, the second and the forth message. and so one . i wish u unerstand what i'm trying to say.

thank you in advance, and i'm sorry for my bad English

if there is a tutorial that can help me in this , plz just share it.

1 Answers

The checkbuttons command is executed every time you click on it. The checkbutton takes additional options like variable a tkinter variable that is set to onvalue and offvalue depending on an internal state. Check your variable in the function and alter the entry as you desire.

update You could check the variable even in a different function like your message function.

import tkinter as tk

def message():
    if var.get():
        print(entry.get())
        entry.delete('0',tk.END)

def test():
    if var.get():#check variable
        entry['state'] = 'normal'
    else:
        entry['state'] = 'disabled'

root = tk.Tk()
entry = tk.Entry()
var = tk.BooleanVar(value=False)#tkinter variable
checkbutton = tk.Checkbutton(command=test,
                             onvalue=True,
                             offvalue=False,
                             variable=var)
button = tk.Button(text='Print',command=message)

checkbutton.invoke()
##checkbutton.invoke()

button.pack(side=tk.BOTTOM,fill=tk.BOTH)
entry.pack(side=tk.RIGHT)
checkbutton.pack(side=tk.LEFT)

root.mainloop()
Related