【问题标题】:how to display a timer in python如何在python中显示计时器
【发布时间】:2020-03-14 06:56:06
【问题描述】:

嘿,我正在制作一个记录桌面屏幕的程序。

所以我想要做的是当我在我的 gui 窗口中点击我的开始按钮(tkinter Gui)时。

它应该直接在我的桌面屏幕上而不是在我的 tkinter 窗口上以大字体启动像 3.... ,2.... ,1.... 这样的计时器。然后我的函数应该开始了。

我该怎么做..

import tkinter as tk
from tkinter import *

root = Tk()
root.title("our program")
start_cap =tk.button(text='start recording' command=start_capute)
start_cap.pack()
root.mainloop()

这里没有提及函数和整个代码,因为代码工作正常,我只想在其中添加计时器的新功能。

【问题讨论】:

  • 这个网站上有很多关于计时器和时钟的问题。在提出问题之前您是否进行过任何研究?

标签: python-3.x tkinter


【解决方案1】:

一个最小的例子:

import tkinter as tk
# from tkinter import *

def Start():

    def Count(Number):
        if Number == -1:
            win.withdraw()
            print("Start") # what you want to do
            return False
        NumberLabel["text"] = Number
        win.after(1000,Count,Number-1)
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    win = tk.Toplevel()
    win.geometry("+%d+%d"%((screen_width-win.winfo_width())/2,(screen_height-win.winfo_height())/2)) # make it in the center.
    win.overrideredirect(1)
    win.wm_attributes('-topmost',1) # top window
    win.wm_attributes('-transparentcolor',win['bg']) # background transparent.
    NumberLabel = tk.Label(win,font=("",40,"bold"),fg='white')
    NumberLabel.pack()
    win.after(0,lambda :Count(3))
    win.mainloop()

root = tk.Tk()
root.title("our program")
start_cap = tk.Button(text='start recording',command=Start)
start_cap.pack()
root.mainloop()

【讨论】:

  • 无需使用全局Number。将其作为参数传递给Count()
  • 使用 win.after(1000, Count, Number-1) 代替 lambda 和 Number -= 1
猜你喜欢
  • 2010-11-11
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
相关资源
最近更新 更多