In this article, I will show the data in the worldclassics table in the Mysql database in the combobox. When the button is clicked, the index information selected in the Combobox is displayed on the label.
First, let's add the Mysql class as follows. Then I add the connector that will provide the connection to the Mysql Database.
Figure 1
Figure 2
import mysql.connector
from tkinter import *
from tkinter import ttk
from tkinter.font import Font
con1 = mysql.connector.connect(
host="localhost",
user="root",
password="2344",
database="book"
)
cur = con1.cursor()
root= Tk()
root.geometry("600x400")
root.title("combobox...bs")
# to make the GUI dimensions fixed..
root.resizable(False, False)
font = Font(family = "Arial", size = 16)
root.option_add("*TCombobox*Listbox*Font", font)
cboxstyle = ttk.Style()
cboxstyle.theme_create('combostyle', parent='alt',
settings = {'TCombobox':
{'configure':
{'selectbackground': 'deepskyblue',
'fieldbackground': '#CCFFFF',
'background': 'lightgreen'
}}}
)
cboxstyle.theme_use('combostyle')
# Create a function to clear the combobox..
def clear_cb():
cbox.set('')
cur1 = con1.cursor()
cur1.execute("Select Author,Book From worldclassics")
rows = cur1.fetchall()
con1.close()
# Function to print the index of selected option in Combobox..
def callback(*arg):
Label(root, text= "Id=" + str(cbox.current()) +" "+ str(var.get()), font= ('Arial','12','normal')).pack()
# Create a combobox widget..
var = StringVar()
cbox = ttk.Combobox(root, textvariable=var,font=('Arial', 16, 'normal'),width=50,height=30)
cbox['values'] = rows
cbox['state'] = 'readonly'
cbox.current(0)
#cbox.grid(row=10, column=1, padx=50, pady=50)
#cbox.place(x=20, y=20, width=380, height=50)
cbox.pack(padx=20,pady=20)
# Set the tracing for the given variable
var.trace('w', callback)
# Create a button to clear the selected combobox text value
button=Button(root, text= "Clear ( Temizle )", command= clear_cb,width=23,height=2,font= ('Arial','12','bold'),fg="white",bg="fuchsia")
#button.place(x=20, y=80, width=380, height=50)
button.pack(padx=50,pady=5)
root.mainloop()
Happy coding.
No comments:
Post a Comment