Trv.tag_configure('pass', background='lightgreen')trv.tag_configure('fail', background='yellow') Adding to rows based on (one liner) if condition check for dt in r_set: my_tag='pass' if dt >=75 else 'fail' trv.insert("", 'end',iid=dt, text=dt, values =(dt,dt,dt,dt,dt), tags=(my_tag))We can add more styles to the tag_configure(). Tkinter Treeview row background colour configure using tag_configure() based on conditions Here if the mark column value is more than or equal to 75 then the row colour will be green. We can add tags to show different styles to the rows. # width of columns and alignment trv.column("1", width = 30, anchor ='c')trv.column("2", width = 80, anchor ='c')trv.column("3", width = 80, anchor ='c')trv.column("4", width = 80, anchor ='c')trv.column("5", width = 80, anchor ='c')# Headings # respective columnstrv.heading("1", text ="id")trv.heading("2", text ="Name")trv.heading("3", text ="Class")trv.heading("4", text ="Mark") trv.heading("5", text ="Gender")# getting data from MySQL student table r_set=my_conn.execute('''SELECT * from student LIMIT 0,10''')for dt in r_set: trv.insert("", 'end',iid=dt, text=dt, values =(dt,dt,dt,dt,dt))my_w.mainloop()This will print 10 rows of records from student table. With Tkinter the Full code is here from tkinter import ttkimport tkinter as tkfrom sqlalchemy import create_enginemy_conn = create_engine("mysql+mysqldb:// userid: localhost/ my_db") # Creating tkinter my_wmy_w = tk.Tk()my_w.geometry("400x280") my_w.title("# Using treeview widgettrv = ttk.Treeview(my_w, selectmode ='browse')trv.grid(row=1,column=1,padx=20,pady=20)# number of columnstrv = ("1", "2", "3","4","5")# Defining headingtrv = 'headings' After creating all these we will insert the rows of data using above code. The Treeview we created will have header and columns. for dt in r_set: trv.insert("",'end',iid=dt,values=(dt,dt,dt,dt,dt)) To one Tkinter window we will integrate the above code. from sqlalchemy import create_enginemy_conn = create_engine("mysql+mysqldb:// userid: localhost/ my_db")r_set=my_conn.execute('''SELECT * from student LIMIT 0,10''') for dt in r_set: print(dt) Integrating MySQL data to TreeviewIn above code we have the record r_set which we can loop through and insert the rows to the Treeview. If this code is working fine then you can focus on Tkinter part. So we used another for loop to display each element of this tuple.ĭisplaying records: Checking connectionRun this code without using Tkinter to check your MySQL record retrieval. Each row of data ( here student ) is a tuple. We will use one for loop to collect each row of record from the data set. We used the returned MySQLCursor i.e my_conn as an iterator We will use Query with LIMIT to collect 10 records from the student table. Connecting and displaying MySQL table data in Tkinter window using Treeview insert with columnsĬonnect to MySQL database from sqlalchemy import create_enginemy_conn = create_engine("mysql+mysqldb:// userid: localhost/ my_db")We will use my_conn in our further script as the connection object to get our records.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |