用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)指出,本站将立即改正。
|
|