vendredi 27 mai 2022

How do you control the state of a radio button using a checkbox in Tkinter & Python

How do you only allow a radio button to be enabled if another checkbox is on. I included some code that I've been trying:

    if variable.get()==1:
        radiobutton.configure(state = NORMAL)
    elif variable.get()==0:
        radiobutton.configure(state = DISABLED)

Here is my code. I know that I could have simplified it, but try to ignore thatHere is my code. I know that I could have simplified it, but try to ignore thatHere is my code. I know that I could have simplified it, but try to ignore that (sorry for the repetition, it said too much code:

from tkinter import *
import os
from tkinter import ttk
import sqlite3
from tkinter import _setit
myfont=("Verdana")
window=Tk()
window.title("Endo Phone Assist")
#window.tk.call('wm', 'iconphoto', window._w, tk.PhotoImage(file='/path/to/ico/endoassistlogo.ico'))
window.geometry("760x660")
window.resizable(False, False)
myCanvas = Canvas(window, width = 800, height = 700, bg='#4d92e8')
myCanvas.pack()
######            Database Connection            ######



conn = sqlite3.connect("Practice Information Database.db")
c = conn.cursor()
######           Screen 1            ######
s12v = IntVar()
s13v=IntVar()
s14v=IntVar()
s15v=IntVar()
s16v=IntVar()
s17v = IntVar()
s18v=IntVar()
s19v=IntVar()
s110v=IntVar()
s111v=IntVar()
s112v=IntVar()
s113v=IntVar()
s114v=IntVar()
s115v=IntVar()
s116v=IntVar()
s12 = Checkbutton( text = "Root Canal", variable=s12v, bg='#4d92e8', font=(myfont, '12', 'italic'))
s13 = Checkbutton( text = "Retreatment", variable=s13v,bg='#4d92e8',font=(myfont, '12', 'italic'))
s14 = Checkbutton( text = "Endodontic Surgery", variable=s14v,bg='#4d92e8', font=(myfont, '12', 'italic'))
s15 = Checkbutton( text = "Y", variable=s15v,bg='#4d92e8', font=(myfont, '12', 'italic'))
s16 = Checkbutton( text = "Z", variable=s16v,bg='#4d92e8', font=(myfont, '12', 'italic'))
s17 = Radiobutton( text = "Yes", variable=s17v,value=1,bg='#4d92e8', font=(myfont, '12', 'italic'))
#s17.configure(state = DISABLED)
s18 = Radiobutton( text = "Yes", variable=s18v,value=1,bg='#4d92e8',font=(myfont, '12', 'italic'))
#s18.configure(state = DISABLED)
s19 = Radiobutton( text = "Yes", variable=s19v,value=1,bg='#4d92e8', font=(myfont, '12', 'italic'))
#s19.configure(state = DISABLED)
s110 = Radiobutton( text = "Yes", variable=s110v,value=1,bg='#4d92e8', font=(myfont, '12', 'italic'))
#s110.configure(state = DISABLED)
s111 = Radiobutton( text = "Yes", variable=s111v,value=1,bg='#4d92e8', font=(myfont, '12', 'italic'))
#s111.configure(state = DISABLED)
s112 = Radiobutton( text = "No", variable=s17v,value=2,bg='#4d92e8', font=(myfont, '12', 'italic'))
#s112.configure(state = DISABLED)
s113 = Radiobutton( text = "No", variable=s18v,value=2,bg='#4d92e8',font=(myfont, '12', 'italic'))
#s113.configure(state = DISABLED)
s114 = Radiobutton( text = "No", variable=s19v,value=2,bg='#4d92e8', font=(myfont, '12', 'italic'))
#s114.configure(state = DISABLED)
s115 = Radiobutton( text = "No", variable=s110v,value=2,bg='#4d92e8', font=(myfont, '12', 'italic'))
#s115.configure(state = DISABLED)
s116 = Radiobutton( text = "No", variable=s111v,value=2,bg='#4d92e8', font=(myfont, '12', 'italic'))
#s116.configure(state = DISABLED)
#if s12v.get()==0:
    #s17.configure(state = NORMAL)
#elif s12v.get()==1:
     #s17.configure(state = DISABLED)
def screen1():
    myCanvas.create_text(300, 50, text="Welcome to Endo Phone Assist! First, Tell use a little about your practice:", fill="Black", font=(myfont, '13','bold'))
    myCanvas.create_text(137, 125, text="What services do you offer?", fill="Black", font=(myfont, '11','bold'))
    myCanvas.create_text(325, 125, text="Does it require a consult?", fill="Black", font=(myfont, '11','bold'))
    s12.place(x=130,y=175)
    myCanvas.create_text(280, 237, text="(Previously Treated Tooth)", fill="Black", font=(myfont, '7','bold'))
    s13.place(x=130,y=225)
    s14.place(x=130,y=275)
    s15.place(x=130,y=325)
    s16.place(x=130,y=375)
    s17.place(x=360,y=175)
    s18.place(x=360,y=225)
    s19.place(x=360,y=275)
    s110.place(x=360,y=325)
    s111.place(x=360,y=375)
    s112.place(x=430,y=175)
    s113.place(x=430,y=225)
    s114.place(x=430,y=275)
    s115.place(x=430,y=325)
    s116.place(x=430,y=375)
    backbtn1.place(x=-100, y=-100)
def goback1():
    myCanvas.create_text(300, 50, text="Welcome to Endo Phone Assist! First, Tell use a little about your practice:", fill="Black", font=(myfont, '13','bold'))
    myCanvas.create_text(137, 125, text="What services do you offer?", fill="Black", font=(myfont, '11','bold'))
    myCanvas.create_text(325, 125, text="Does it require a consult?", fill="Black", font=(myfont, '11','bold'))
    s12.place(x=130,y=175)
    myCanvas.create_text(280, 237, text="(Previously Treated Tooth)", fill="Black", font=(myfont, '7','bold'))
    s13.place(x=130,y=225)
    s14.place(x=130,y=275)
    s15.place(x=130,y=325)
    s16.place(x=130,y=375)
    s17.place(x=360,y=175)
    s18.place(x=360,y=225)
    s19.place(x=360,y=275)
    s110.place(x=360,y=325)
    s111.place(x=360,y=375)
    s112.place(x=430,y=175)
    s113.place(x=430,y=225)
    s114.place(x=430,y=275)
    s115.place(x=430,y=325)
    s116.place(x=430,y=375)
    backbtn1.place(x=1000,y=5500)
    button1to2.place(x=600,y=550)
######           Screen 2            ######
def screen2():
    myCanvas.delete('all')
    s12.place(x=-100,y=-100)
    s13.place(x=-100,y=-100)
    s14.place(x=-100,y=-100)
    s15.place(x=-100,y=-100)
    s16.place(x=-100,y=-100)
    s17.place(x=-100,y=-100)
    s18.place(x=-100,y=-100)
    s19.place(x=-100,y=-100)
    s110.place(x=-100,y=-100)
    s111.place(x=-100,y=-100)
    s112.place(x=-100,y=-100)
    s113.place(x=-100,y=-100)
    s114.place(x=-100,y=-100)
    s115.place(x=-100,y=-100)
    s116.place(x=-100,y=-100)
    button1to2.place(x=-100,y=-100)
    backbtn1.place(x=100, y=550)
def updateradiobtn():
    if s12v.get()==0:
        s17.configure(state = DISABLED)
    elif s12v.get()==1:
        s17.configure(state = ENABLED)
button1to2=Button(text='Next', command=screen2, bd=0,highlightbackground='#4d92e8')
button1to2.place(x=600,y=550)
backbtn1=Button(text='Go Back', command=goback1, bd=0,highlightbackground='#4d92e8')
updateradiobtn()
screen1()
window.mainloop()



Aucun commentaire:

Enregistrer un commentaire