界面:
import tkinter as tk
import time
import sys
import ttkbootstrap
from tkinter import Frame, ttk, scrolledtext, Button, VERTICAL, NS, NSEW, END, Label
class RunMain(object):
def __init__(self):
pass
def interface_main(self):
self.window = ttkbootstrap.Window(themename="litera")
# 设置标题
self.window.title('实验界面')
# 窗口的位置和大小
sw = self.window.winfo_screenwidth()
# 得到屏幕宽度
sh = self.window.winfo_screenheight()
# 得到屏幕高度
ww = 800
wh = 600
# 窗口宽高为500
x = (sw-ww) / 2
y = (sh-wh) / 2
self.window.geometry("%dx%d+%d+%d" % (ww, wh, x, y))
# 设置窗口是否可以变化长宽,默认可变
self.window.resizable(width=False, height=False)
self.window.iconbitmap("logo.ico")
"""
基本布局
"""
lable1_line = Label(self.window, text='-' * 66, font=('微软雅黑', 20), fg='black', bg='#C0D9D9')
lable1_line.place(x=0, y=40)
lable1_title = Label(self.window, text='实验界面', font=('微软雅黑', 30), fg='black', bg='#C0D9D9')
lable1_title.place(x=320, y=0)
lable1_edition = Label(self.window, text='版本号v1.0', font=('微软雅黑', 10), fg='black', bg='#C0D9D9')
lable1_edition.place(x=720, y=30)
# 设置窗口是否可以变化长宽,默认可变
self.window.resizable(width=False, height=False)
frame = Frame(self.window)
# 创建表格
self.tree_date = ttk.Treeview(frame, show='tree headings', height=17)
frame.place(x=10, y=100, width=580)
# 设置x轴和y轴的滚动条
ybar = ttk.Scrollbar(frame, orient=VERTICAL)
ybar.pack(side='right', fill='y')
xbar = ttk.Scrollbar(frame, orient=tk.HORIZONTAL)
xbar.pack(side='bottom', fill='x')
self.tree_date.configure(yscrollcommand=ybar.set, xscrollcommand=xbar.set)
# 定义列
self.tree_date["columns"] = ["name", "age", "weight", "number", "hang", "X"]
self.tree_date.pack()
# 使用command参数来绑定treevievw
ybar.config(command=self.tree_date.yview)
xbar.config(command=self.tree_date.xview)
# 设置列宽度
self.tree_date.column("#0", width=90, anchor="center")
self.tree_date.column("name", width=70, anchor="center")
self.tree_date.column("age", width=70, anchor="center")
self.tree_date.column("weight", width=70, anchor="center")
self.tree_date.column("number", width=70, anchor="center")
self.tree_date.column("hang", width=70, anchor="center")
self.tree_date.column("X", width=160, anchor="center")
# 添加列名
self.tree_date.heading("#0", text="循环名")
self.tree_date.heading("name", text="姓名")
self.tree_date.heading("age", text="年龄")
self.tree_date.heading("weight", text="体重")
self.tree_date.heading("number", text="状态")
self.tree_date.heading("hang", text="行")
self.tree_date.heading("X", text="完成时间")
# # 添加列名
# self.tree_date.heading("#0", text="制式")
# for j in date_columns:
# self.tree_date.heading(j, text=j)
"""
进度条
"""
self.jin_du = ttkbootstrap.Meter(
master=self.window,
bootstyle="success",
subtextstyle="warning",
metertype="full", # 将仪表显示为一个完整的圆形或半圆形(semi)
# wedgesize=5, #设置弧周围的指示器楔形长度,如果大于 0,则此楔形设置为以当前仪表值为中心的指示器
amounttotal=100, # 仪表的最大值,默认100
amountused=100, # 仪表的当前值
metersize=180, # 仪表大小
showtext=True, # 指示是否在仪表上显示左、中、右文本标签
interactive=False, # 是否可以手动调节数字的大小
textleft='进度:', # 插入到中心文本左侧的短字符串
textright='%',
textfont="-size 20", # 中间数字大小
# subtext=" ",
subtextfont="-size 20", # 文本大小
)
self.jin_du.place(x=600, y=250)
"""
下方说明框
"""
self.scr_explain = scrolledtext.ScrolledText(self.window, font=('微软雅黑', 15), width=63, height=5)
self.scr_explain.place(x=10, y=440)
self.run_button = Button(self.window, text="开始运行", font=('微软雅黑', 20))
self.run_button.place(x=620, y=100)
self.exit_button = Button(self.window, text="退出界面", font=('微软雅黑', 20))
self.exit_button.place(x=620, y=180)
处理函数:
import time
import sys
import threading
from tkinter import END, messagebox
from configure import RunMain
test_item_num = 0
class MyThread(threading.Thread):
def __init__(self, func, *args):
super().__init__()
self.func = func
self.args = args
self.setDaemon(True)
self.start() # 在这⾥开始
def run(self):
self.func(*self.args)
class DataHandle(RunMain):
def __init__(self):
super().__init__()
# 计算所有的测试项的总数
def test_number(self):
all_number = 18
return all_number
# 进度条参数显示
def step_number(self):
global test_item_num
all_number = self.test_number()
# print(all_number)
number = (1 / all_number) * 100
test_item_num = test_item_num + number
self.jin_du.configure(amountused=int(test_item_num))
# 输出到下方提示框
def out_put(self, output: str):
self.scr_explain.insert(END, "{}\n".format(output))
self.scr_explain.yview_moveto(1)
def tool_exit(self):
confirmation_information = messagebox.askquestion('确认操作', '确定退出工具?')
if confirmation_information == "no":
self.out_put("退出操作已取消!")
return
sys.exit()
# 清空表格
def delButton(self, tree):
x = tree.get_children()
for item in x:
tree.delete(item)
def run_main(self):
# 清空表格和下方提示框
self.scr_explain.delete(0.0, END)
self.delButton(self.tree_date)
# 进度条清空
global test_item_num
test_item_num = 0
self.jin_du.configure(amountused=0)
# 进度条显示变蓝
self.jin_du.configure(bootstyle="info")
self.scr_explain.delete(0.0, END)
self.delButton(self.tree_date)
# 建立起制式表头
sf1 = self.tree_date.insert('', END, text='第一个循环', open=True)
complete_time = time.strftime("%Y.%m.%d %H:%M:%S", time.localtime())
for num in range(1, 10):
# 显示进度条
self.step_number()
time.sleep(0.1)
self.out_put("表格加载第{}次!".format(num))
data = ["张三", "18", "70kg", "ok", num, complete_time]
# 直接在已有数据后填充
idd = self.tree_date.insert(sf1, END, values=tuple(data))
self.tree_date.see(idd)
self.tree_date.update()
num += 1
# 建立起制式表头
sf2 = self.tree_date.insert('', END, text='第二个循环', open=True)
for num in range(1, 10):
# 显示进度条
self.step_number()
time.sleep(0.1)
self.out_put("表格加载第{}次!".format(num))
data = ["李四", "18", "70kg", "ok", num, complete_time]
# 直接在已有数据后填充
idd = self.tree_date.insert(sf2, END, values=tuple(data))
self.tree_date.see(idd)
self.tree_date.update()
num += 1
# 进度条
test_item_num = 0
self.jin_du.configure(bootstyle="success", amountused=100)
self.out_put("程序运行完成!")
def set_command(self):
self.exit_button.configure(command=self.tool_exit)
self.run_button.configure(command=lambda: MyThread(self.run_main))
# 用x键,防止文件未创建
self.window.protocol("WM_DELETE_WINDOW", self.tool_exit)
def main(self):
self.interface_main()
self.set_command()
self.window.mainloop()
if __name__ == '__main__':
data_handle = DataHandle()
data_handle.main()
最后效果图·:
点击按键《开始运行》就可以看到运行的效果了。