Connecting and displaying MySQL table data in Tkinter window using Treeview insert with columns

plus2net
plus2net
9.9 هزار بار بازدید - 3 سال پیش - Treeview widget of Tkinter provides
Treeview widget of Tkinter provides better flexibility for displaying tabular data in a tkinter window. Because of its Tree type structure we can easily access the rows and data by using Parent child concepts.
https://www.plus2net.com/python/tkint...
Treeview is part of ttk class so we need to import this first.

Collecting data and displaying
Before using Tkinter window, we can connect to MySQL database and collect the records from our sample table. The dame rows of data we can display and ensure that all our data collection part is working fine. After ensuring data collection we can integrate the same to our Tkinter window by using Treeview.
To connect to MySQL database we will use salalchemy connect_engine.
After entering your login details, we can create the connection object and collect records from our sample student table.
SELECT * FROM student LIMIT 0,10
This query will execute and get 10 records as record set.
We will create one Tkinter window and develop one Treeview object and place it in a grid.

trv=ttk.Treeview(my_w,selectmode='browse')
trv.grid(row=1,column=1,padx=20,pady=20)
trv["columns"]=("1","2","3","4","5")
trv['show']='headings'
We will have 5 columns as our sample student table has 5 columns.
Similarly we can manage headings by assigning width and alignment to this.

Once the layout is ready we will then insert the rows into the Treeview by using insert method inside a for loop.
for dt in r_set:
   trv.insert("",'end',iid=dt[0],values=(dt[0],dt[1],dt[2],dt[3],dt[4]))

#treeview  #tkintertreeview #mysqltkintertreeview
3 سال پیش در تاریخ 1400/02/04 منتشر شده است.
9,931 بـار بازدید شده
... بیشتر