tkinter的背景色要求在tkinter之后否则treeview等不会有颜色

发布时间 2023-05-05 21:48:59作者: 祥瑞哈哈哈
import tkinter
from tkinter import ttk
from tkinter import *
from tkinter.ttk import *
import pymysql
# 导入消息对话框子模块
import tkinter.messagebox


 
def fixed_map(option):
    # Returns the style map for 'option' with any styles starting with
    # ("!disabled", "!selected", ...) filtered out
    # style.map() returns an empty list for missing options, so this should
    # be future-safe
    return [elm for elm in style.map("Treeview", query_opt=option)
            if elm[:2] != ("!disabled", "!selected")]
root = Tk()
root.geometry("400x350")
style = ttk.Style()
style.map("Treeview",
                  foreground=fixed_map("foreground"),
                  background=fixed_map("background"))


import tkinter
from tkinter import ttk
from tkinter import *
from tkinter.ttk import *
import pymysql
# 导入消息对话框子模块
import tkinter.messagebox


#定义背景色风格



def select_allstudent_study():    
    #root =Tk()
   
   

    yscrollbar=Scrollbar(root)
    yscrollbar.pack(side=RIGHT,fill=Y)
   
    root.title('所有学生的自习状态')
    root.geometry('600x800')
    root.config(bg='#FFC0CB')
    tree=ttk.Treeview(root)#表格
    tree["columns"]=("时间段","动作类型")
    tree.column("#0",width=50)   #表示列,不显示
    tree.column("时间段",width=50)
    tree.column("动作类型",width=50)
    tree.tag_configure('FAIL', background='red', foreground="white")
   
    tree.heading('#0', text='序号')
    tree.heading('时间段', text='时间段')
    tree.heading('动作类型', text='动作类型')
    tree.tag_configure('tag_name', background="red")


   
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='1234567890', db='studentmemo')
    cursor = conn.cursor()    
    c = cursor.execute("SELECT * FROM studentmemo.all_student;")
 #2.将查询结果保存到list_re
    list_re=cursor.fetchall()
    for i in range(len(list_re)):

        tree.insert('', "end", text=list_re[i][0],values=(list_re[i][1],list_re[i][2]),tags="FAIL")    
        #else:
            #tree.insert('', END, text=list_re[i][0],values=(list_re[i][1],list_re[i][2]))    
   

    yscrollbar.config(command=tree.yview)    
    tree.configure(yscrollcommand=yscrollbar.set)
    tree.pack(fill=BOTH,expand=True)
    root.mainloop()
select_allstudent_study()