1.png (4.61 KB)
下載附件
2023-9-29 01:04 上傳
用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
復制代碼
【必讀】版權免責聲明
1、本主題所有言論和內容純屬會員個人意見,與本論壇立場無關。2、本站對所發內容真實性、客觀性、可用性不做任何保證也不負任何責任,網友之間僅出于學習目的進行交流。3、對提供的數字內容不擁有任何權利,其版權歸原著者擁有。請勿將該數字內容進行商業交易、轉載等行為,該內容只為學習所提供,使用后發生的一切問題與本站無關。 4、本網站不保證本站提供的下載資源的準確性、安全性和完整性;同時本網站也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的損失或傷害。 5、本網站所有軟件和資料均為網友推薦收集整理而來,僅供學習用途使用,請務必下載后兩小時內刪除,禁止商用。6、如有侵犯你版權的,請及時聯系我們(電子郵箱1370723259@qq.com)指出,本站將立即改正。
|