Oct-07-2021, 08:28 AM
I have two rows of combo boxes representing two different groups. the user needs to select either of the group and then choose different values from these drop down boxes. I have used radio buttons to choose the group. however Its not working out . One group remains permanently disabled. I am unable to figure out where I am making a mistake. Can someone please help.Here is my code.
from tkinter import * import tkinter as tk import datetime as dt from tkinter import ttk from tkcalendar import DateEntry import mysql.connector from tkinter import messagebox #connect clinicmaster database labname,workrequire,materials, tables to combobox mydb= mysql.connector.connect(host="localhost",user="root", password="mudit",database="Clinicmaster",auth_plugin="mysql_native_password") cursor=mydb.cursor() options=[] sql="select labname,lemail from clinicmaster" cursor.execute(sql) labname=cursor.fetchall() for i in labname: options.append(str(i[0])) workr=[] sql="select work from workrequired" cursor.execute(sql) workreq=cursor.fetchall() for i in workreq: workr.append(str(i[0])) mater=[] sql="select material from materials" cursor.execute(sql) material=cursor.fetchall() for i in material: mater.append(str(i[0])) #print(labname) root = Tk() root.title("Lab Order Form") #photo=PhotoImage(file="C:\\Users\\INDIAN\\Desktop\\python exercises\\order form\\crown2.png") #label = Label(root,image = photo,bg="light blue") #label.image = photo # keep a reference! #label.grid(row=7,column=3,columnspan=20,sticky=tk.E,rowspan=30) #order date as current date d = f'{dt.datetime.now():%a, %b %d %Y}' #delivery date calender picker sel=tk.StringVar() cal = DateEntry(root,selectmode = 'day',date_pattern="dd-mm-y", background='darkblue', foreground='white', borderwidth=2) def my_upd(*args): l1.config(text=cal.get_date()) #l1.config(text=sel.get()) sel.trace('w',root) #,cal.pack(padx=10, pady=10) def getvals(): print("Submitting form") print(f"{labvalue.get(),patientnamevalue.get(),patientagevalue.get(),patientsexvalue.get(), workvalue.get(), reqdatevalue.get(),toothnumbervalue.get(), materialvalue.get(), incisalvalue.get(),middlevalue.get(),cervicalvalue.get(), labservicevalue.get(),toohreductionvalue.get(),reductioncopingvalue.get(),sendbackvalue.get()} ",d) with open("records.txt", "a") as f: f.write(f"{labvalue.get(),patientnamevalue.get(),patientagevalue.get(),patientsexvalue.get(), workvalue.get(),reqdatevalue.get(), toothnumbervalue.get(), materialvalue.get(), incisalvalue.get(),middlevalue.get(),cervicalvalue.get(), labservicevalue.get(),toohreductionvalue.get(),reductioncopingvalue.get(),sendbackvalue.get()}\n") # function to write to orderform table in database def order(): orderdaate=d deliverydate=cal.get_date() labname=labvalue.get() patient=patientnamevalue.get() ptage=patientagevalue.get() ptsex=patientsexvalue.get() workrequired=workvalue.get() tooth=toothnumbervalue.get() material=materialvalue.get() incisal=incisalvalue.get() middle=middlevalue.get() cervical=cervicalvalue.get() value=valuevalue.get() chroma=chromavalue.get() hue=huevalue.get() metalcoping=labservicevalue.get() toohreduction=toohreductionvalue.get() reductioncoping=reductioncopingvalue.get() sback=sendbackvalue.get() sql="INSERT INTO orderform(orderdaate,deliverydate,labname,patient,ptage,ptsex,workrequired,tooth,material,incisal,middle,cervical,value,chroma,hue,metalcoping,toohreduction,reductioncoping,sback)" "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" cursor.execute(sql,(orderdaate,deliverydate,labname,patient,ptage,ptsex,workrequired,tooth,material,incisal,middle,cervical,value,chroma,hue,metalcoping,toohreduction,reductioncoping,sback)) mydb.commit() print("DONE") return True root.geometry("1200x700") root.configure(background='light blue') Label(root, text="Arora Dental Care\n Lab Work Order\n Crown ", font="comicsansms 13 bold",bg="light blue", pady=15).grid(row=0, column=2) def openNewWindow(): global newWindow try: if newWindow.state()=="normal":newWindow.focus() except: newWindow = Toplevel(root) newWindow.title("Add Work") newWindow.attributes('-topmost','true') # sets the geometry of toplevel newWindow.geometry("300x250") newWindow.configure(background='light blue') newWindow.resizable(False,False) frm1=Frame(newWindow,bg="light blue") def addwork(): srno=ent3.get() work=ent4.get() sql=("INSERT INTO workrequired(srno,work)" "VALUES(%s,%s)" ) cursor.execute(sql,(srno,work)) mydb.commit() messagebox.showinfo("Confirmation","Work Added") print("DONE") return True cursor.execute('select count(*) from workrequired') next_num = cursor.fetchall()[0][0] frm1.pack(side=tk.LEFT,padx=20) var3=StringVar() srno=StringVar() var4=StringVar() workrequired=StringVar() label3=Label(frm1,textvariable=var3,bg="light blue") var3.set(" ") label3.grid(row=2,column=1,padx=10,pady=10) ent3=Entry(frm1,textvariable=srno,width=10) srno.set(next_num+1) ent3.grid(row=2,column=2,sticky=tk.W,padx=10,pady=10) label4=Label(frm1,textvariable=var4,bg="light blue") var4.set("Work Required") label4.grid(row=3,column=1,padx=10,pady=10) ent4=Entry(frm1,textvariable=workrequired) workrequired.set(" ") ent4.grid(row=3,column=2,padx=10,pady=10) def clear_text(): workrequired.set(' ') cursor.execute('select count(*) from workrequired') next_num = cursor.fetchall()[0][0] srno.set(next_num+1) btn=Button(frm1, text="ADD",command= lambda:[addwork(), clear_text()]) btn.grid(row=5,column=2,padx=10,pady=10) # A Label widget to show in toplevel Label(newWindow, text ="Add Work",bg="light blue").pack(padx=5, pady=20, side=tk.LEFT) label = Label(root, text ="This is the main window") btn = Button(root, text ="Add work",bg="light blue", height = 1, width = 7, command = openNewWindow).grid(row=2, column=2,sticky=tk.W) def openNew(): global new try: if new.state()=="normal":new.focus() except: new = Toplevel(root) new.title("Add Material") new.attributes('-topmost','true') # sets the geometry of toplevel new.geometry("300x250") new.configure(background='light blue') new.resizable(False,False) frm1=Frame(new,bg="light blue") def addmaterial(): srno=ent5.get() material=ent6.get() sql=("INSERT INTO materials(srno,material)" "VALUES(%s,%s)" ) cursor.execute(sql,(srno,material)) mydb.commit() messagebox.showinfo("Confirmation","Material Added") print("DONE") return True cursor.execute('select count(*) from materials') next_num = cursor.fetchall()[0][0] frm1.pack(side=tk.LEFT,padx=20) var5=StringVar() srno=StringVar() var6=StringVar() material=StringVar() label5=Label(frm1,textvariable=var5,bg="light blue") var5.set(" ") label5.grid(row=2,column=1,padx=10,pady=10) ent5=Entry(frm1,textvariable=srno,width=10) srno.set(next_num+1) ent5.grid(row=2,column=2,sticky=tk.W,padx=10,pady=10) label6=Label(frm1,textvariable=var6,bg="light blue") var6.set("material") label6.grid(row=3,column=1,padx=10,pady=10) ent6=Entry(frm1,textvariable=material) material.set(" ") ent6.grid(row=3,column=2,padx=10,pady=10) def clear_text(): material.set(' ') cursor.execute('select count(*) from materials') next_num = cursor.fetchall()[0][0] srno.set(next_num+1) btn=Button(frm1, text="ADD",command= lambda:[addmaterial(), clear_text()]) btn.grid(row=5,column=2,padx=10,pady=10) btn = Button(root, text ="Add Material",bg="light blue", height = 1, width = 9, command = openNew).grid(row=4, column=2,sticky=tk.W) #Text for our form lab = Label(root, text=" Lab. Name",bg="light blue") patientname=Label(root, text=" Patient.Name",bg="light blue") patientage=Label(root, text="Patient Age",bg="light blue") patientsex=Label(root,text="Sex",bg="light blue") orderdate=Label(root, text="Order Date",bg="light blue") date=Label(root,text=d,fg="black",bg="light blue",font=("Helvetica", 11)) reqdate=Label(root,text="Deliver By",bg="light blue") work = Label(root, text="Work Required",bg="light blue") toothnumber = Label(root, text="Tooth Number",bg="light blue") material = Label(root, text="Material",bg="light blue") shade = Label(root, text="Shade",bg="light blue") incisal=Label(root,text="Incisal/occlusal 1/3rd",bg="light blue") middle=Label(root,text="Middle 1/3rd",bg="light blue") cervical=Label(root,text="Cervical 1/3rd",bg="light blue") value=Label(root,text="Value",bg="light blue") chroma=Label(root,text="Chroma",bg="light blue") hue=Label(root,text="Hue",bg="light blue") lessspace=Label(root,text=" In Case of inadequate occlussal clearance",font='Helvetica 18 bold', bg="light blue") #Pack text for our form lab.grid(row=1, column=0,padx=5,pady=5) patientname.grid(row=1, column=2,padx=5,pady=5) patientage.grid(row=2,column=2, padx=5,pady=5) patientsex.grid(row=3,column=2,padx=5,pady=5) orderdate.grid(row=4, column=2,padx=5,pady=5) reqdate.grid(row=5, column=2,padx=5,pady=5) date.grid(row=4,column=3,sticky=tk.W,padx=5,pady=5) work.grid(row=2, column=0,padx=5,pady=5) toothnumber.grid(row=3, column=0,padx=5,pady=5) material.grid(row=4, column=0,padx=5,pady=5) shade.grid(row=5, column=0,padx=5,pady=5) incisal.grid(row=6,column=0,padx=5,pady=5) middle.grid(row=7,column=0,padx=5,pady=5) cervical.grid(row=8,column=0,padx=5,pady=5) value.grid(row=6,column=1,padx=5,pady=5,sticky=tk.E) chroma.grid(row=7,column=1,padx=5,pady=5,sticky=tk.E) hue.grid(row=8,column=1,padx=5,pady=5,sticky=tk.E) lessspace.grid(row=10,column=2) # Tkinter variable for storing entries labvalue = StringVar() patientnamevalue = StringVar() patientagevalue=StringVar() patientsexvalue=StringVar() orderdatevalue= StringVar() reqdatevalue=StringVar() workvalue = StringVar() toothnumbervalue = StringVar() materialvalue = StringVar() incisalvalue=StringVar() middlevalue=StringVar() cervicalvalue=StringVar() valuevalue=StringVar() chromavalue=StringVar() huevalue=StringVar() labservicevalue = StringVar(value=" ") toohreductionvalue=StringVar(value=" ") reductioncopingvalue=StringVar(value=" ") sendbackvalue=StringVar(value=" ") #Entries for our form labentry = ttk.Combobox(root,state="readonly",width=20,textvariable=labvalue) labentry['values']=options def caps(event): patientnamevalue.set(patientnamevalue.get().upper()) patientnameentry = Entry(root,width=23, textvariable=patientnamevalue) patientnameentry.bind("<KeyRelease>", caps) def validate_age(new_value): if len(new_value) == 0 or len(new_value) <= 2 and new_value.isdigit(): return True else: return False patientageentry = tk.Entry(root,width=7,textvariable=patientagevalue, validate="key", validatecommand=(root.register(validate_age), "%P")) patientsexentry=ttk.Combobox(root,state="readonly",width=4,textvariable=patientsexvalue) patientsexentry['values']=("M","F") patientsexentry.current() DateEntry=cal workentry =ttk.Combobox(root,state="readonly",width=20,textvariable=workvalue) workentry['values']=workr workentry.current() toothnumberentry =ttk.Combobox(root,state="readonly",width=20,textvariable=toothnumbervalue) toothnumberentry['values']=("18","17","16","15","14","13","12","11","21","22","23","24","25","26","27","28","38","37","36","35","34","33","32","31","41","42","43","44","45","46","47","48") toothnumberentry.current() materialentry =ttk.Combobox(root,state="readonly",width=20,textvariable=materialvalue) materialentry['values']=mater materialentry.current() incisalentry =ttk.Combobox(root,state="readonly",width=6,textvariable=incisalvalue) incisalentry['values']=("A1","A2","A3","A3.5","A4","B1","B2","B3","B4","C1","C2","C3","C4","D2","D3","D4") incisalentry.current() middleentry =ttk.Combobox(root,state="readonly",width=6,textvariable=middlevalue) middleentry['values']=("A1","A2","A3","A3.5","A4","B1","B2","B3","B4","C1","C2","C3","C4","D2","D3","D4") middleentry.current() cervicalentry =ttk.Combobox(root,state="readonly",width=6,textvariable=cervicalvalue) cervicalentry['values']=("A1","A2","A3","A3.5","A4","B1","B2","B3","B4","C1","C2","C3","C4","D2","D3","D4") cervicalentry.current() valuentry =ttk.Combobox(root,state="readonly",width=6,textvariable=valuevalue) valuentry['values']=("0","1","2","3","4","5") valuentry.current() chromaentry =ttk.Combobox(root,state="readonly",width=6,textvariable=chromavalue) chromaentry['values']=("L","M","R") chromaentry.current() hueentry =ttk.Combobox(root,state="readonly",width=6,textvariable=cervicalvalue) hueentry['values']=("1","2","3") hueentry.current() # Packing the Entries labentry.grid(row=1, column=1,padx=5,pady=5) patientnameentry.grid(row=1, column=3,sticky=tk.W,padx=5,pady=5) patientageentry.grid(row=2, column=3,sticky=tk.W,padx=5,pady=5) patientsexentry.grid(row=3, column=3,sticky=tk.W,padx=5,pady=5) DateEntry.grid(row=5,column=3,sticky=tk.W,padx=5,pady=5) #orderdateentry.grid(row=2, column=7) workentry.grid(row=2, column=1,padx=5,pady=5) toothnumberentry.grid(row=3, column=1,padx=5,pady=5) materialentry.grid(row=4, column=1,padx=5,pady=5) #shadeentry.grid(row=5, column=3,padx=5,pady=5) incisalentry.grid(row=6, column=1,sticky=tk.W,padx=5,pady=5) middleentry.grid(row=7, column=1,sticky=tk.W,padx=5,pady=5) cervicalentry.grid(row=8, column=1,sticky=tk.W,padx=5,pady=5) valuentry.grid(row=6, column=2,sticky=tk.W,padx=5,pady=5) chromaentry.grid(row=7, column=2,sticky=tk.W,padx=5,pady=5) hueentry.grid(row=8, column=2,sticky=tk.W,padx=5,pady=5) shadeguide=IntVar(value=1) def vita(): if shadeguide.get()== 2: incisalentry.configure(state="normal") middleentry.configure(state="normal") cervicalentry.configure(state="normal") valuentry.configure(state="disable") chromaentry.configure(state="disable") hueentry.configure(state="disable") else: incisalentry.configure(state="disable") middleentry.configure(state="disable") cervicalentry.configure(state="disable") valuentry.configure(state="normal") chromaentry.configure(state="normal") hueentry.configure(state="normal") Radiobutton(root, text="Vita Classic",bg="light blue", variable=shadeguide, value=1, command=vita()).grid(row=5, column=1, sticky=W) Radiobutton(root, text="Vita 3D",bg="light blue",variable=shadeguide, value=2, command=vita()).grid(row=5, column=2, sticky=W) labservice = tk.Checkbutton(text="Want metal coping trial",bg="light blue", variable = labservicevalue,onvalue="Yes",offvalue="No") labservice.grid(row=9, column=2,sticky=tk.W) toohreduction = tk.Checkbutton(text="Do opposite tooth reduction",bg="light blue", variable = toohreductionvalue,onvalue="Yes",offvalue="No") toohreduction.grid(row=11, column=2,sticky=tk.W) reductioncoping = tk.Checkbutton(text="Make reduction coping",bg="light blue", variable = reductioncopingvalue,onvalue="Yes",offvalue="No") reductioncoping.grid(row=12, column=2,sticky=tk.W) sendback = tk.Checkbutton(text="Send the Case Back for correction",bg="light blue", variable = sendbackvalue,onvalue="Yes",offvalue="No") sendback.grid(row=13, column=2,sticky=tk.W) def clear_alltext(): labvalue.set(' ') patientnamevalue .set(' ') patientagevalue.set(' ') patientsexvalue.set(' ') orderdatevalue.set(' ') reqdatevalue.set(' ') workvalue.set(' ') toothnumbervalue.set(' ') materialvalue.set(' ') #shadevalue = StringVar() incisalvalue.set(' ') middlevalue.set(' ') cervicalvalue.set(' ') labservicevalue.set(' ') toohreductionvalue.set(' ') reductioncopingvalue.set(' ') sendbackvalue.set(' ') #Button & packing it and assigning it a command btn3=Button(text=" submit ", command= lambda:[order(), clear_alltext()]) btn3.grid(row=15, column=2) root.mainloop()