用PyCharm 編輯,可以讓大家學習python的多線程
以下是源代碼
import time
import tkinter as tk
from tkinter import * # Frame use
import threading
import tkinter as tk
import time
import os
import sys
wcmsec = 0
wcsec = 0
wcminutes = 0
wcrun = 0
timegetbk = 0
global t
# ---------main-------------------
if __name__ == '__main__':
def main():
global wcrun
win = tk.Tk()
win.geometry('350x160')
win.minsize(350, 160) # 最小尺寸 python tkinter 設置窗口大小不可縮放
win.maxsize(350, 160) # 最大尺寸
win.wm_attributes('-topmost', 1) # python tkinter窗口彈出置頂的方法
win.title("秒表REV2")
def startwatch():
global wcrun, timegetbk
timegetbk = time.time()
wcrun = 1
def Resetwatch():
global wcmsec, wcsec, wcminutes, wcrun
wcmsec = 0
wcsec = 0
wcminutes = 0
wcrun = 0
text.delete(1.0, tk.END)
str1 = "00" + ":" + "00" + ":" + "00"
text.insert(tk.INSERT, str1)
def stopwatch():
global wcrun, timegetbk
if (wcrun == 1):
wcrun = 0
else:
wcrun = 1
timegetbk = time.time()
# time.sleep(0.3) # 休眠1秒
def timecycle():
global wcmsec, wcsec, wcminutes, wcrun, timegetbk
# button3.configure(text="--- World!", command=stopwatch)
if (wcrun == 1):
win.title("已運行")
button3.configure(text="停止!", command=stopwatch)
else:
win.title("已停止")
button3.configure(text="運行!", command=stopwatch)
text.delete(1.0, tk.END)
win.focus_set()
if (wcrun > 0):
timeget = time.time()
if (timeget > timegetbk):
wcmsec = wcmsec + ((timeget - timegetbk) * 1000)
timegetbk = time.time()
if (wcmsec > 999):
wcmsec = wcmsec - 1000
wcsec = wcsec + 1
if (wcsec > 59):
wcsec = wcsec - 60
wcminutes = wcminutes + 1
if (wcminutes > 99):
wcminutes = 0
wcnum = int(wcmsec / 10)
if (wcnum < 10):
dispmsec = "0" + str(wcnum)
else:
dispmsec = str(wcnum)
if (wcsec < 10):
dispsec = "0" + str(wcsec)
else:
dispsec = str(wcsec)
if (wcminutes < 10):
dispmin = "0" + str(wcminutes)
else:
dispmin = str(wcminutes)
str1 = dispmin + ":" + dispsec + ":" + dispmsec
text.insert(tk.INSERT, str1)
#threading.Timer(0.1, timecycle).start()
def display():
for i in range(1, 500000):
time.sleep(0.1)
# print("hello world")
timecycle()
#global t
# t = threading.Timer(0.1, timecycle)
# t.start()
def xFunc1(event):
if (event.char == "v") or (event.char == "V"):
startwatch()
if (event.char == "b") or (event.char == "B"):
startwatch()
if (event.char == "n") or (event.char == "N"):
startwatch()
if (event.char == " "):
stopwatch()
def kill(pid):
try:
# command = 'taskkill /F /IM %d' %pid
# print type(command)
# os.system(command) #1111
import subprocess
subprocess.Popen("cmd.exe /k taskkill /F /T /PID %i" % pid, shell=True)
except:
print('no process')
def on_closing():
time.sleep(0.1) # 休眠1秒
#t.cancel() # 關閉線程
time.sleep(0.1) # 休眠1秒
win.destroy() # 關閉窗口
time.sleep(0.1) # 休眠1
time.sleep(0.1) # 休眠1
pid = os.getpid()
# os.system('taskkill /f /pid %s' % '20500')
kill(pid)
time.sleep(0.1) # 休眠1
sys.exit()
try:
sys.exit(0)
except:
sys.exit(1)
text = Text(
master=win, # 父容器
bg='pink', # 背景顏色
fg='red', # 文本顏色
relief='sunken', # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。
bd=3, # 邊框的大小
width=8, # 寬度
height=1, # 高度
state='normal', # 設置狀態 normal、readonly、 disabled
cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus...
font=('黑體', 55), # 字體
wrap='char', # 字數夠width后是否換行 char, none, word
)
text.pack()
y_setposition = 100
x_setposition = 20
x_space_set = 115
x_position = x_setposition
# button2 = tk.Button(win, text="啟動", command=startwatch)
# button2 = tk.Button(win, text="發送版本", command=sw.stopwatch(), width=15, height=5)
'''
button2 = tk.Button(win, text="啟動", command=startwatch)
button2.place(x = x_position,y = y_setposition,width = 80,height = 50)
'''
button2 = Button(
master=win, # 父容器
activebackground='red', # 背景顏色
activeforeground="white",
bd="2",
command=startwatch,
text="啟動",
font=('黑體', 22),
)
button2.place(x=x_position, y=y_setposition, width=80, height=50)
button3 = tk.Button(win, text="停止", command=stopwatch)
x_position = x_setposition + x_space_set
button3.place(x=x_position, y=y_setposition, width=80, height=50)
button1 = tk.Button(win, text="復位", command=Resetwatch)
x_position = x_setposition + x_space_set + x_space_set
button1.place(x=x_position, y=y_setposition, width=80, height=50)
win.bind("<Key>", xFunc1)
global t1
t1 = threading.Timer(0.1, timecycle())
t1.start()
rx1 = threading.Thread(target=display) # 多線程
rx1.start()
win.protocol("WM_DELETE_WINDOW", on_closing)
win.mainloop()
main()
# pyinstaller -F -w main.py build1
# pyinstaller -w main.py
|