第一次接触python的tkinter库
程序代码:
from tkinter import *
from tkinter.ttk import *
import os
import xlrd
import detailgui
import detailgui1
import detailgui2
import detailgui3
import detailgui4
import detailgui5
import win32api
import win32con
for i in range(1,1500):
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL,0,0,-1)
#driver.maximize_window() #将浏览器最大化显示
class MainWindow(Tk):
def __init__(self):
super().__init__()
self.title("底盘前后轴总成螺栓管理可视化系统")
self.geometry("1300x900+200+100")
self.resizable(True, True)#self.resizable(0,0)
self["bg"] = "lavender"#lavender
self.setup_UI()
#light steel blue
#lavender
def setup_UI(self):
# 设定Style
self.Style01 = Style()
# self.Style01.configure("left.TPanedwindow", background="sky blue")
self.Style01.configure("right.TPanedwindow", background="lavender")#lavender
self.Style01.configure("TButton", width=3, font=("宋体", 10,"normal") )
# Top_banner
self.Login_image = PhotoImage(file="D:\\bolt\\bolt.PNG")
self.Lable_image = Label(self, image=self.Login_image)
self.Lable_image.pack()
self.Pane_right = PanedWindow(width=1400, height=1000, style="right.TPanedwindow")
self.Pane_right.place(x=0, y=200)#250
# 查询、TreeView
self.Pane_right = PanedWindow(width=1700, height=480, style="right.TPanedwindow")
self.Pane_right.place(x=0, y=180)
# LabelFrame
self.LabelFrame_query = LabelFrame(self.Pane_right, text="零件信息查询", width=967, height=150)
self.LabelFrame_query.place(x=145, y=-2)
# 添加控件
self.Label_sno = Label(self.LabelFrame_query, text="零件号:")
self.Label_sno.place(x=10, y=20)
self.Entry_sno = Entry(self.LabelFrame_query, width=12)
self.Entry_sno.place(x=65, y=20)
self.Label_name = Label(self.LabelFrame_query, text="位置标号:")
self.Label_name.place(x=290, y=20)
self.Entry_name = Entry(self.LabelFrame_query, width=5)
self.Entry_name.place(x=360, y=20)
self.Label_id = Label(self.LabelFrame_query, text="车型:")
self.Label_id.place(x=170, y=20)
self.Entry_id = Entry(self.LabelFrame_query, width=8)
self.Entry_id.place(x=215, y=20)
self.Button_query = Button(self.LabelFrame_query, text="查询", width=8,command = self.get_query_result)
self.Button_query.place(x=450, y=20)
self.Button_all = Button(self.LabelFrame_query, text="显示全部", width=10,command = self.load_all_student)
self.Button_all.place(x=550, y=20)
#添加MQB,MLB,MEB三种平台的零件装配图
self.Button_MQB = Button(self.LabelFrame_query, text="MQB.FA", width=10,command = self.update_student)
self.Button_MQB.place(x=680, y=5)
self.Button_MQB1 = Button(self.LabelFrame_query, text="MQB.RA", width=10, command=self.update_student1)
self.Button_MQB1.place(x=680, y=35)
self.Button_MLB = Button(self.LabelFrame_query, text="MLB.FA", width=10, command=self.update_student2)
self.Button_MLB.place(x=780, y=5)
self.Button_MLB1 = Button(self.LabelFrame_query, text="MLB.RA", width=10, command=self.update_student3)
self.Button_MLB1.place(x=780, y=35)
self.Button_MEB = Button(self.LabelFrame_query, text="MEB.FA", width=10, command=self.update_student4)
self.Button_MEB.place(x=880, y=5)
self.Button_MEB1 = Button(self.LabelFrame_query, text="MEB.RA", width=10, command=self.update_student5)
self.Button_MEB1.place(x=880, y=35)
# 添加TreeView控件
self.Tree = Treeview(self.Pane_right, columns=("sno", "names","platform","gender", "birthday","mobile","email","address","number"),show="headings", height=18)
# 设置每一个列的宽度和对齐的方式
self.Tree.column("sno", width=100, anchor="center")
self.Tree.column("names", width=80, anchor="center")
self.Tree.column("platform", width=50, anchor="center")
self.Tree.column("gender", width=75, anchor="center")
self.Tree.column("birthday", width=80, anchor="center")
self.Tree.column("mobile", width=150, anchor="center")
self.Tree.column("email", width=100, anchor="center")
self.Tree.column("address", width=280, anchor="center")
self.Tree.column("number", width=50, anchor="center")
# 设置每个列的标题
self.Tree.heading("sno", text="零件号")
self.Tree.heading("names", text="车型")
self.Tree.heading("platform", text="平台")#新加
self.Tree.heading("gender", text="前轴/后轴")
self.Tree.heading("birthday", text="位置标号")
self.Tree.heading("mobile", text="装配位置")
self.Tree.heading("email", text="PDM图号")
self.Tree.heading("address", text="安装力矩")
self.Tree.heading("number",text="数量")#新加
self.Tree.place(x=145, y=95)
self.all_student_list = []
self.file_path = "D:\\bolt\\bolt.xls"
self.load_file_student_info()
data = xlrd.open_workbook("D:\\bolt\\bolt.xls")
table = data.sheets()[0]
nrows = table.nrows
sl = Scrollbar(self)
Listbox(self.Tree)['yscrollcommand'] = sl.set
sl['command'] = Listbox(self.Tree).yview
self.Tree.bind("" , self.view_student)
self.Tree.bind("" , self.view_pdm)
def load_file_student_info(self):
if not os.path.exists(self.file_path):
showinfo("系统消息", "提供的文件名不存在!")
else:
try:
data=xlrd.open_workbook("D:\\bolt\\bolt.xls")
table=data.sheets()[0]
nrows=table.nrows
for i in range(nrows):
if i==0:
continue
self.all_student_list.append(table.row_values(i))
except:
showinfo("系统消息", "文件读取出现异常!")
# self.load_file_student_info()
self.load_treeview(self.all_student_list)
def load_treeview(self, current_list: list):
# 判断是否有数据:
if len(current_list) == 0:
showinfo("系统消息", "没有数据加载")
else:
for index in range(len(current_list)):
self.Tree.insert("", index, values=(current_list[index][0], current_list[index][1],current_list[index][2], current_list[index][3],current_list[index][4],current_list[index][5],current_list[index][6],current_list[index][7],current_list[index][8]))
# self.load_treeview(self.all_student_list)
self.query_result_list = []
self.var_sno = StringVar()
self.var_id = StringVar()
self.var_name = StringVar()
#textvariable = self.var_sno
self.query_result_list.clear()
def get_query_result(self):
# query_condition列表中的下标为0、1分别是零件号,车型,对应的数据源中的单个数据列表的位置是0,1
query_condition = []
query_condition.append(self.Entry_sno.get().strip()) # 采集零件号信息
query_condition.append(self.Entry_id.get().strip()) # 采集车型信息
query_condition.append(self.Entry_name.get().strip()) # 采集位置标号信息
# query_condition列表中的下标为0、1分别是零件号,车型,对应的数据源中的单个数据列表的位置是0,1
# 准备查询条件:获取零件号
query_condition[0] = self.Entry_sno.get()
# 准备查询条件:获取车型
query_condition[1] = self.Entry_id.get()
# 准备查询条件:获取位置标号
query_condition[2] = self.Entry_name.get()
for item in self.all_student_list:
if (query_condition[1] in item[1] and query_condition[0] in item[0]) and (query_condition[2] == item[4]):
self.query_result_list.append(item)
if (query_condition[1] in item[1] and query_condition[0] in item[0]) and (query_condition[2] == ''):
self.query_result_list.append(item)
# 把结果加载到TreeView中
for i in self.Tree.get_children():
self.Tree.delete(i)
self.load_treeview(self.query_result_list)
#实现显示全部数据的功能
def load_all_student(self):
# 加载所有的学生信息到treeview
self.var_sno.set("")
self.var_id.set("")
self.var_name.set("")
for i in self.Tree.get_children():
self.Tree.delete(i)
self.load_treeview(self.all_student_list)
def load_detail_window(self):
detail_window = detailgui.DetailWindow()
def update_student(self):
self.load_detail_window()
def load_detail_window1(self):
detail_window = detailgui1.DetailWindow()
def update_student1(self):
self.load_detail_window1()
def load_detail_window2(self):
detail_window = detailgui2.DetailWindow()
def update_student2(self):
self.load_detail_window2()
def load_detail_window3(self):
detail_window = detailgui3.DetailWindow()
def update_student3(self):
self.load_detail_window3()
def load_detail_window4(self):
detail_window = detailgui4.DetailWindow()
def update_student4(self):
self.load_detail_window4()
def load_detail_window5(self):
detail_window = detailgui5.DetailWindow()
def update_student5(self):
self.load_detail_window5()
def set(self, *args):
"""Set the fractional values of the slider position (upper and
lower ends as value between 0 and 1)."""
self.tk.call((self._w, 'set') + args)
#双击显示图片 如果车型是MQB并且是前轴的话,显示出MQB前轴装配图
def view_student(self, event):
sub_str = "MQB"
sub_str1 = "MLB"
sub_str2 = "MEB"
item = self.Tree.selection()[0]#获取点击的这一行数据
Temp_student_list = self.Tree.item(item, "values")
if Temp_student_list[3] == '前轴':#逻辑没问题,但是不应该遍历整个表,应该看点击的数据信息的item【2】和item【3】是啥
if Temp_student_list[2] == 'MLB':
self.load_detail_window2()
else:
if Temp_student_list[2] == 'MQB':
self.load_detail_window()
else:
self.load_detail_window4()
else:
if Temp_student_list[2] == 'MQB':
self.load_detail_window1()
else:
if Temp_student_list[2] == 'MLB':
self.load_detail_window3()
else:
self.load_detail_window5()
def view_pdm(self, event):
sub_str = '1EA.199'
sub_str1 = '1EA.400'
sub_str2 = '1EA.423'
sub_str3 = '1EA.501'
sub_str4 = '1EA.816'
sub_str5 = '1ED.615'
sub_str6 = '2Q0.199'
sub_str7 = '2Q0.400'
sub_str8 = '2Q0.423'
sub_str9 = '2Q0.615'
sub_str10 = '3Q0.400'
sub_str11 = '3QF.400'
sub_str12 = '4M0.199'
sub_str13 = '5Q0.199'
sub_str14 = '5Q0.400'
sub_str15 = '5Q0.423'
sub_str16 = '5Q0.500'
sub_str17 = '5Q0.501'
sub_str18 = '5Q0.615'
sub_str19 = '8W0.400'
sub_str20 = '8W0.501'
sub_str21 = '8W0.615'
sub_str22 = '80A.400'
sub_str23 = '80A.501'
sub_str24 = '80A.615'
sub_str25 = '180.199'
sub_str26 = '180.400'
sub_str27 = '180.615'
sub_str28 = 'TAB.017.598'
sub_str29 = 'TAB.017.694'
sub_str30 = '4M0.501'
sub_str31 = '4N0.501'
item = self.Tree.selection()[0] # 获取点击的这一行数据
Temp_student_list = self.Tree.item(item, "values")
if sub_str in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.1EA.199.pdf")
if sub_str1 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.1EA.400.pdf")
if sub_str2 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.1EA.423.pdf")
if sub_str3 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.1EA.501.pdf")
if sub_str4 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.1EA.816.pdf")
if sub_str5 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.1ED.615.pdf")
if sub_str6 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.2Q0.199.pdf")
if sub_str7 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.2Q0.400.pdf")
if sub_str8 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.2Q0.423.pdf")
if sub_str9 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.2Q0.615.pdf")
if sub_str10 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.3Q0.400.pdf")
if sub_str11 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.3QF.400.pdf")
if sub_str12 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.4M0.199.pdf")
if sub_str13 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.5Q0.199.pdf")
if sub_str14 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.5Q0.400.pdf")
if sub_str15 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.5Q0.423.pdf")
if sub_str16 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.5Q0.500.pdf")
if sub_str17 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.5Q0.501.pdf")
if sub_str18 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.5Q0.615.pdf")
if sub_str19 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.8W0.400.pdf")
if sub_str20 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.8W0.501.pdf")
if sub_str21 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.8W0.615.pdf")
if sub_str22 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.80A.400.pdf")
if sub_str23 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.80A.501.pdf")
if sub_str24 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.80A.615.pdf")
if sub_str25 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.180.199.pdf")
if sub_str26 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.180.400.pdf")
if sub_str27 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.180.615.pdf")
if sub_str28 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\TAB.017.598.tif")
if sub_str29 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\TAB.017.694.tif")
if sub_str30 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.80A.501.pdf")
if sub_str31 in Temp_student_list[6] :
os.system("start explorer D:\\bolt\\PDM\\PDM.80A.501.pdf")
mainwindow = MainWindow()
mainwindow.mainloop()
detailgui.py
from tkinter import *
from tkinter.ttk import *
import os
class DetailWindow(Toplevel):
def __init__(self):
super().__init__()
self.title("MQB前轴装配图")
self.geometry("1100x1100+180+80")
self.resizable(True, True) # 能改变大小
# 加载控件
self.setup_UI()
def setup_UI(self):
# 设置style
self.Style01 = Style()
self.Login_image = PhotoImage(file="D:\\bolt\\MQB前.PNG")
#self.Login_image.(100,100)
self.Lable_image = Label(self, image=self.Login_image)
self.Lable_image.pack()
if __name__ == '__main__':
this_window = DetailWindow()
this_window.mainloop()
detailgui1.py同上,只需变换下路径以及名称即可
最后运行的结果
后续会增加一些对代码的解释,以及这几天的学习心得。