找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1299|回复: 0
打印 上一主题 下一主题
收起左侧

python写的跑表程序

[复制链接]
跳转到指定楼层
楼主
用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样式 flatsunkenraisedgrooveridgesolid
            bd=3,  # 边框的大小
            width=8,  # 宽度
            height=1,  # 高度
            state='normal',  # 设置状态 normalreadonly 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

stopwatch.png (4.61 KB, 下载次数: 65)

app界面

app界面

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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