码迷,mamicode.com
首页 > 其他好文 > 详细

pysimplegui模块实现倒计时UI框

时间:2020-01-21 16:04:54      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:upd   src   hit   gui   pre   white   timeout   set   image   

一、代码

import sys
if sys.version_info[0] >= 3:
    import PySimpleGUI as sg
else:
    import PySimpleGUI27 as sg
import time

sg.ChangeLookAndFeel(Black)
sg.SetOptions(element_padding=(0, 0))

layout = [[sg.Text(‘‘)],
         [sg.Text(‘‘, size=(8, 2), font=(Helvetica, 20), justification=center, key=text)],
         [sg.Button(Pause, key=button, button_color=(white, #001480)),
          sg.Button(Reset, button_color=(white, #007339), key=Reset),
          sg.Exit(button_color=(white, firebrick4), key=Exit)]]

window = sg.Window(Running Timer, layout, no_titlebar=True, auto_size_buttons=False, keep_on_top=True, grab_anywhere=True)
current_time = 0
paused = False
start_time = int(round(time.time() * 100))
while (True):
    # --------- Read and update window --------
    if not paused:
        event, values = window.read(timeout=10)
        current_time = int(round(time.time() * 100)) - start_time
    else:
        event, values = window.read()
    if event == button:
        event = window[event].GetText()
    # --------- Do Button Operations --------
    if event is None or event == Exit:        # ALWAYS give a way out of program
        break
    if event is Reset:
        start_time = int(round(time.time() * 100))
        current_time = 0
        paused_time = start_time
    elif event == Pause:
        paused = True
        paused_time = int(round(time.time() * 100))
        element = window[button]
        element.update(text=Run)
    elif event == Run:
        paused = False
        start_time = start_time + int(round(time.time() * 100)) - paused_time
        element = window[button]
        element.update(text=Pause)

    # --------- Display timer in window --------
    window[text].update({:02d}:{:02d}.{:02d}.format((current_time // 100) // 60,
                                                                  (current_time // 100) % 60,
                                                                  current_time % 100))

技术图片

pysimplegui模块实现倒计时UI框

标签:upd   src   hit   gui   pre   white   timeout   set   image   

原文地址:https://www.cnblogs.com/angelyan/p/12221859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!