找回密码
 注册

QQ登录

只需一步,快速开始

搜索

python写的秒表程序

[复制链接]
bluesky1 发表于 2023-9-29 01:04:30 | 显示全部楼层 |阅读模式
1.png

用PyCharm 编辑,可以让大家学习python的多线程
以下是源代码
  1. import time
  2. import tkinter as tk
  3. from tkinter import *  # Frame use

  4. import threading
  5. import tkinter as tk
  6. import time
  7. import os
  8. import sys

  9. wcmsec = 0
  10. wcsec = 0
  11. wcminutes = 0
  12. wcrun = 0
  13. timegetbk = 0
  14. global t
  15. # ---------main-------------------
  16. if __name__ == '__main__':
  17.     def main():
  18.         global wcrun
  19.         win = tk.Tk()
  20.         win.geometry('350x160')

  21.         win.minsize(350, 160)  # 最小尺寸  python tkinter 设置窗口大小不可缩放
  22.         win.maxsize(350, 160)  # 最大尺寸

  23.         win.wm_attributes('-topmost', 1)  # python tkinter窗口弹出置顶的方法

  24.         win.title("秒表REV2")

  25.         def startwatch():
  26.             global wcrun, timegetbk
  27.             timegetbk = time.time()
  28.             wcrun = 1

  29.         def Resetwatch():
  30.             global wcmsec, wcsec, wcminutes, wcrun
  31.             wcmsec = 0
  32.             wcsec = 0
  33.             wcminutes = 0
  34.             wcrun = 0

  35.             text.delete(1.0, tk.END)
  36.             str1 = "00" + ":" + "00" + ":" + "00"
  37.             text.insert(tk.INSERT, str1)

  38.         def stopwatch():
  39.             global wcrun, timegetbk
  40.             if (wcrun == 1):
  41.                 wcrun = 0

  42.             else:
  43.                 wcrun = 1

  44.                 timegetbk = time.time()

  45.             # time.sleep(0.3)  # 休眠1秒

  46.         def timecycle():
  47.             global wcmsec, wcsec, wcminutes, wcrun, timegetbk

  48.             # button3.configure(text="--- World!", command=stopwatch)
  49.             if (wcrun == 1):
  50.                 win.title("已运行")
  51.                 button3.configure(text="停止!", command=stopwatch)
  52.             else:
  53.                 win.title("已停止")
  54.                 button3.configure(text="运行!", command=stopwatch)

  55.             text.delete(1.0, tk.END)
  56.             win.focus_set()
  57.             if (wcrun > 0):
  58.                 timeget = time.time()
  59.                 if (timeget > timegetbk):
  60.                     wcmsec = wcmsec + ((timeget - timegetbk) * 1000)
  61.                     timegetbk = time.time()

  62.             if (wcmsec > 999):
  63.                 wcmsec = wcmsec - 1000
  64.                 wcsec = wcsec + 1
  65.                 if (wcsec > 59):
  66.                     wcsec = wcsec - 60
  67.                     wcminutes = wcminutes + 1
  68.                     if (wcminutes > 99):
  69.                         wcminutes = 0

  70.             wcnum = int(wcmsec / 10)
  71.             if (wcnum < 10):
  72.                 dispmsec = "0" + str(wcnum)
  73.             else:
  74.                 dispmsec = str(wcnum)

  75.             if (wcsec < 10):
  76.                 dispsec = "0" + str(wcsec)
  77.             else:
  78.                 dispsec = str(wcsec)

  79.             if (wcminutes < 10):
  80.                 dispmin = "0" + str(wcminutes)
  81.             else:
  82.                 dispmin = str(wcminutes)

  83.             str1 = dispmin + ":" + dispsec + ":" + dispmsec
  84.             text.insert(tk.INSERT, str1)

  85.             #threading.Timer(0.1, timecycle).start()

  86.         def display():
  87.                 for i in range(1, 500000):
  88.                     time.sleep(0.1)
  89.               #      print("hello world")
  90.                     timecycle()

  91.             #global t
  92.            # t = threading.Timer(0.1, timecycle)
  93.            # t.start()

  94.         def xFunc1(event):
  95.             if (event.char == "v") or (event.char == "V"):
  96.                 startwatch()
  97.             if (event.char == "b") or (event.char == "B"):
  98.                 startwatch()
  99.             if (event.char == "n") or (event.char == "N"):
  100.                 startwatch()
  101.             if (event.char == " "):
  102.                 stopwatch()

  103.         def kill(pid):
  104.             try:
  105.                 # command = 'taskkill /F /IM %d' %pid
  106.                 # print type(command)
  107.                 # os.system(command) #1111
  108.                 import subprocess
  109.                 subprocess.Popen("cmd.exe /k taskkill /F /T /PID %i" % pid, shell=True)
  110.             except:
  111.                 print('no process')

  112.         def on_closing():

  113.             time.sleep(0.1)  # 休眠1秒
  114.             #t.cancel()  # 关闭线程

  115.             time.sleep(0.1)  # 休眠1秒
  116.             win.destroy()  # 关闭窗口
  117.             time.sleep(0.1)  # 休眠1
  118.             time.sleep(0.1)  # 休眠1

  119.             pid = os.getpid()
  120.             # os.system('taskkill /f /pid %s' % '20500')
  121.             kill(pid)

  122.             time.sleep(0.1)  # 休眠1
  123.             sys.exit()
  124.             try:
  125.                 sys.exit(0)
  126.             except:
  127.                 sys.exit(1)

  128.         text = Text(
  129.             master=win,  # 父容器
  130.             bg='pink',  # 背景颜色
  131.             fg='red',  # 文本颜色
  132.             relief='sunken',  # 边框的3D样式 flat、sunken、raised、groove、ridge、solid。
  133.             bd=3,  # 边框的大小
  134.             width=8,  # 宽度
  135.             height=1,  # 高度
  136.             state='normal',  # 设置状态 normal、readonly、 disabled
  137.             cursor='arrow',  # 鼠标移动时样式 arrow, circle, cross, plus...
  138.             font=('黑体', 55),  # 字体
  139.             wrap='char',  # 字数够width后是否换行 char, none,  word
  140.         )
  141.         text.pack()

  142.         y_setposition = 100
  143.         x_setposition = 20
  144.         x_space_set = 115
  145.         x_position = x_setposition

  146.         # button2 = tk.Button(win, text="启动", command=startwatch)
  147.         # button2 = tk.Button(win, text="发送版本", command=sw.stopwatch(), width=15, height=5)

  148.         '''
  149.         button2 = tk.Button(win, text="启动", command=startwatch)
  150.         button2.place(x = x_position,y = y_setposition,width = 80,height = 50)
  151.         '''

  152.         button2 = Button(
  153.             master=win,  # 父容器
  154.             activebackground='red',  # 背景颜色
  155.             activeforeground="white",
  156.             bd="2",
  157.             command=startwatch,
  158.             text="启动",
  159.             font=('黑体', 22),
  160.         )

  161.         button2.place(x=x_position, y=y_setposition, width=80, height=50)

  162.         button3 = tk.Button(win, text="停止", command=stopwatch)
  163.         x_position = x_setposition + x_space_set
  164.         button3.place(x=x_position, y=y_setposition, width=80, height=50)

  165.         button1 = tk.Button(win, text="复位", command=Resetwatch)
  166.         x_position = x_setposition + x_space_set + x_space_set
  167.         button1.place(x=x_position, y=y_setposition, width=80, height=50)

  168.         win.bind("<Key>", xFunc1)

  169.         global t1
  170.         t1 = threading.Timer(0.1, timecycle())
  171.         t1.start()

  172.         rx1 = threading.Thread(target=display)  # 多线程
  173.         rx1.start()

  174.         win.protocol("WM_DELETE_WINDOW", on_closing)
  175.         win.mainloop()


  176.     main()

  177. #  pyinstaller -F -w main.py   build1
  178. #  pyinstaller -w main.py
复制代码

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-29 16:32

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表