Python Forum
[Tkinter] Dropdown box showing weird entry
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Dropdown box showing weird entry
#1
Hi all.
I am working on a tkinter form. I have a dropdown combobox which is supposed to show the values from a MySql database. The problem is that it is showing a weird entry(,!!label3). this entry is not in the database table. when I run the query in Mysql cmd i get the column with all the entries. I added the print command for the variable and the entries printed on the console. Can somebody please explain what is happening here.
from tkinter import *
import tkinter as tk
import datetime as dt
from tkinter import ttk
from tkcalendar import DateEntry
import mysql.connector

mydb= mysql.connector.connect(host="localhost",user="root", password="mudit",database="Clinicmaster",auth_plugin="mysql_native_password")
cursor=mydb.cursor()

sql="select labname from clinicmaster"
cursor.execute(sql)
labname=cursor.fetchall()

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)
d = f'{dt.datetime.now():%a, %b %d %Y}'

cal = DateEntry(root, width=12, day=20, month=6, year=22, 
background='darkblue', foreground='white', borderwidth=2)
#,cal.pack(padx=10, pady=10)

def getvals():
    print("Submitting form")

    print(f"{labnamevalue.get(),patientnamevalue.get(),patientagevalue.get(),patientsexvalue.get(), workvalue.get(), reqdatevalue.get(),toothnumbervalue.get(), materialvalue.get(), incisalvalue.get(),middlevalue.get(),cervicalvalue.get(), labservicevalue.get(),toothreductionvalue.get(),reductioncopingvalue.get(),sendbackvalue.get()} ",d)
    



    with open("records.txt", "a") as f:
        f.write(f"{labnamevalue.get(),patientnamevalue.get(),patientagevalue.get(),patientsexvalue.get(), workvalue.get(),reqdatevalue.get(), toothnumbervalue.get(), materialvalue.get(), incisalvalue.get(),middlevalue.get(),cervicalvalue.get(), labservicevalue.get(),toothreductionvalue.get(),reductioncopingvalue.get(),sendbackvalue.get()}\n")


root.geometry("500x300")
root.configure(background='light blue')
#root.resizable(False, False)
#Heading
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)

#Text for our form
labname = 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")
lessspace=Label(root,text=" In Case of inadequate occlussal clearance",font='Helvetica 18 bold', bg="light blue")

#Pack text for our form
labname.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)
lessspace.grid(row=10,column=2)

# Tkinter variable for storing entries
labnamevalue = StringVar()
patientnamevalue = StringVar()
patientagevalue=StringVar()
patientsexvalue=StringVar()
orderdatevalue= StringVar()
reqdatevalue=StringVar()
workvalue = StringVar()
toothnumbervalue = StringVar()
materialvalue = StringVar()
#shadevalue = StringVar()
incisalvalue=StringVar()
middlevalue=StringVar()
cervicalvalue=StringVar()
labservicevalue = IntVar()
toothreductionvalue=IntVar()
reductioncopingvalue=IntVar()
sendbackvalue=IntVar()



#Entries for our form
labnameentry = ttk.Combobox(root,width=20,textvariable=labnamevalue)
labnameentry['values']=labname
patientnameentry = Entry(root,width=23, textvariable=patientnamevalue)
patientageentry=Entry(root,width=6, textvariable=patientagevalue)
patientsexentry=ttk.Combobox(root,width=6,textvariable=patientsexvalue)
patientsexentry['values']=("M","F")
patientsexentry.current()
DateEntry=cal
#orderdateentry=Entry(root,textvariable=orderdatevalue)
workentry =ttk.Combobox(root,width=20,textvariable=workvalue)
workentry['values']=("Crown","Bridge","Onlay","Inlay","Veneer")
workentry.current()
#workentry = Entry(root, textvariable=workvalue)
toothnumberentry =ttk.Combobox(root,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,width=20,textvariable=materialvalue)
materialentry['values']=("PFM","Zirconia","NiCr","NiCr+Ceramic facing","EMax")
materialentry.current()

#materialentry = Entry(root, textvariable=materialvalue)
#shadeentry =ttk.Combobox(root,width=20,textvariable=shadevalue)
#shadeentry['values']=("A1","A2","A3","A3.5","A4","B1","B2","B3","B4","C1","C2","C3","C4","D2","D3","D4")
#shadeentry.current()
#shadeentry = Entry(root, textvariable=shadevalue)
incisalentry =ttk.Combobox(root,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,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,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()
# Packing the Entries
labnameentry.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)

#Checkbox & Packing it
labservice = Checkbutton(text="Want metal coping trial",bg="light blue", variable = labservicevalue)
labservice.grid(row=9, column=2,sticky=tk.W)
toothreduction = Checkbutton(text="Do opposite tooth reduction",bg="light blue", variable = toothreductionvalue)
toothreduction.grid(row=11, column=2,sticky=tk.W)
reductioncoping = Checkbutton(text="Make reduction coping",bg="light blue", variable = reductioncopingvalue)
reductioncoping.grid(row=12, column=2,sticky=tk.W)
sendback = Checkbutton(text="Send the Case Back for correction",bg="light blue", variable = sendbackvalue)
sendback.grid(row=13, column=2,sticky=tk.W)


#Button & packing it and assigning it a command
Button(text=" submit ", command=getvals).grid(row=15, column=2)



root.mainloop()
Output:
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ===== RESTART: C:\Users\INDIAN\Desktop\python exercises\order form\forum.py ==== [(' Sameer',), ('Mustafa',), ('M S Dental Art',), (' Katara',), (' xyz',)] >>>
Reply
#2
On line 13 labname is a pointer to the results from a cursor.fetchall
sql="select labname from clinicmaster"
cursor.execute(sql)
labname=cursor.fetchall()

On line 48 labname is changed to a pointer to a tkinter label
labname = Label(root, text=" Lab. Name",bg="light blue")

On line 104 using labname the string representation of the tkinter label is added to the combobox
labnameentry = ttk.Combobox(root,width=20,textvariable=labnamevalue)
labnameentry['values']=labname
Reply
#3
When you have this kind of problem you should print out the value and type. The first thing I would have done is print out labname around where it is used to populate the combobox:
print(labname, type(labname))
labnameentry = ttk.Combobox(root,width=20,textvariable=labnamevalue)
labnameentry['values']=labname
Output:
l.!label2 <class 'tkinter.Label'>
You would see that labname was one thing after the sql query and a different thing when you use it to populate the combobox. Something happened between those two places to change the value. A search for all occurrences of labname would quickly reveal the problem.

I see you still have your problem with the DateEntry=cal. Second time you were bit for being sloppy with variables.
Reply
#4
Thanks deanhystad and Yoriz . I changed the labname instances from the tkinter form code and now this is working and am getting the values in the dropbox from the database . however these entries are showing with curly brackets . How can I remove them.
from tkinter import *
import tkinter as tk
import datetime as dt
from tkinter import ttk
from tkcalendar import DateEntry
import mysql.connector

mydb= mysql.connector.connect(host="localhost",user="root", password="mudit",database="Clinicmaster",auth_plugin="mysql_native_password")
cursor=mydb.cursor()

sql="select labname from clinicmaster"
cursor.execute(sql)
labname=cursor.fetchall()

#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)
d = f'{dt.datetime.now():%a, %b %d %Y}'

cal = DateEntry(root, width=12, day=20, month=6, year=22, 
background='darkblue', foreground='white', borderwidth=2)
#,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(),toothreductionvalue.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(),toothreductionvalue.get(),reductioncopingvalue.get(),sendbackvalue.get()}\n")


root.geometry("500x300")
root.configure(background='light blue')
#root.resizable(False, False)
#Heading
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)

#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")
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)
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()
#shadevalue = StringVar()
incisalvalue=StringVar()
middlevalue=StringVar()
cervicalvalue=StringVar()
labservicevalue = IntVar()
toothreductionvalue=IntVar()
reductioncopingvalue=IntVar()
sendbackvalue=IntVar()



#Entries for our form
labentry = ttk.Combobox(root,width=20,textvariable=labvalue)
labentry['values']=labname
patientnameentry = Entry(root,width=23, textvariable=patientnamevalue)
patientageentry=Entry(root,width=6, textvariable=patientagevalue)
patientsexentry=ttk.Combobox(root,width=6,textvariable=patientsexvalue)
patientsexentry['values']=("M","F")
patientsexentry.current()
DateEntry=cal
#orderdateentry=Entry(root,textvariable=orderdatevalue)
workentry =ttk.Combobox(root,width=20,textvariable=workvalue)
workentry['values']=("Crown","Bridge","Onlay","Inlay","Veneer")
workentry.current()
#workentry = Entry(root, textvariable=workvalue)
toothnumberentry =ttk.Combobox(root,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,width=20,textvariable=materialvalue)
materialentry['values']=("PFM","Zirconia","NiCr","NiCr+Ceramic facing","EMax")
materialentry.current()

#materialentry = Entry(root, textvariable=materialvalue)
#shadeentry =ttk.Combobox(root,width=20,textvariable=shadevalue)
#shadeentry['values']=("A1","A2","A3","A3.5","A4","B1","B2","B3","B4","C1","C2","C3","C4","D2","D3","D4")
#shadeentry.current()
#shadeentry = Entry(root, textvariable=shadevalue)
incisalentry =ttk.Combobox(root,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,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,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()
# 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)

#Checkbox & Packing it
labservice = Checkbutton(text="Want metal coping trial",bg="light blue", variable = labservicevalue)
labservice.grid(row=9, column=2,sticky=tk.W)
toothreduction = Checkbutton(text="Do opposite tooth reduction",bg="light blue", variable = toothreductionvalue)
toothreduction.grid(row=11, column=2,sticky=tk.W)
reductioncoping = Checkbutton(text="Make reduction coping",bg="light blue", variable = reductioncopingvalue)
reductioncoping.grid(row=12, column=2,sticky=tk.W)
sendback = Checkbutton(text="Send the Case Back for correction",bg="light blue", variable = sendbackvalue)
sendback.grid(row=13, column=2,sticky=tk.W)


#Button & packing it and assigning it a command
Button(text=" submit ", command=getvals).grid(row=15, column=2)



root.mainloop()
Reply
#5
What do curly brackets mean in Python? What type are the objects are returned by the sql query? If you don't know, how can you find out? What type of objects do you want for your combobox? How can you convert the sql return values to a type appropriate for the combobox?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Entry box not showing 2 decimal places Chuck_Norwich 3 5,701 Apr-24-2020, 05:28 PM
Last Post: deanhystad
  [Tkinter] populating dropdown from Method mikisDW 2 3,853 Apr-06-2020, 08:06 PM
Last Post: mikisDW
  Option dropdown with Pyinquerer julio2000 0 1,534 Mar-22-2020, 04:11 PM
Last Post: julio2000
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,463 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] Choose from dropdown list and then do something? Selfiatus1 2 5,437 Jun-07-2019, 08:43 PM
Last Post: Selfiatus1
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,492 Mar-18-2019, 05:36 PM
Last Post: woooee
  Dropdown menu- Store variable aking76 1 3,403 Sep-11-2018, 01:30 PM
Last Post: aking76

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020