在Tkinter中使用计时器制作GUI?

时间:2019-04-21 22:23:11

标签: python user-interface tkinter

Here is the image when it is outside not capturing the data from the gui

This is what happens when it captures but does not output to the gui

我试图显示与打印时输出类似的计时器,但只是将输出更新放在同一位置,类似于在gui中创建时钟以查看其自我更新。

我尝试了不同的代码来尝试在GUI工作的地方并获得输出,但是当我将其移到代码中以捕获变量时

from tkinter import *
from tkinter import font
import time
import humanfriendly
from timeit import default_timer as timer
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522 


import RPi.GPIO as GPIO
timeh=0
timem=0
paystat=0



win = Tk()
win2 = Tk()

myFont1=font.Font (family = 'Times New Roman', size = 18, weight = 'bold')
myFont2=font.Font (family = 'Times New Roman', size = 50, weight = 'bold')
font.families()


def add():
    global timem
    global timeh
    print("Time added")
    if paystat==0:
        if timeh<4:
            timem=timem+15
            if timem%60==0:
                timeh=timeh+1
                timem=0
        else: 
            return 0 

    timeDisp1.delete(1.0,END) #clear display
    timeDisp1.insert(END,timem) #append display
    #(END, timem)
    timeDisp2.delete(1.0,END) #clear display
    timeDisp2.insert(END,timeh) #append display
    #(END, timeh)
    return (timeh,timem)

def sub():
    global timem
    global timeh
    print("Time subtracted")
    if paystat==0:
        timem=timem-15
        if timem<0 and timeh!=0:
            timeh=timeh-1
            timem+=60
        if timem<0 and timeh==0:
            timeh=0
            timem=0
   timeDisp1.delete(1.0,END) #clear display
    timeDisp1.insert(END,timem) #append display
    #(END, timem
    timeDisp2.delete(1.0,END) #clear display
    timeDisp2.insert(END,timeh) #append display

    return (timeh,timem)    
def pay(): 
    paystat=1
    reader = SimpleMFRC522()
   try:
        id, text = reader.read()
        print(id)
        print(text)
        if paystat==1:
            win.after(1000,win.destroy)
                timer1 = ''
                timer2= Label(win2, font=('times', 20, 'bold'))
                timer2.pack(fill=BOTH, expand=1)

                def timer():
                        global timeh
                        global timem
                        global timer1
                        timeh1=timeh*3600
                        timem1=timem*60

                        truetime=timeh1+timem1
                        print("Time Expired is:",truetime)
                        start_time=time.time()
                        struct = time.localtime (start_time)

                        print ("Starting Time at:", time.strftime("%X", struct))

                        t = int(time.time())
                        expiration_time = t + truetime
                        difference= round(expiration_time-start_time)
                        struct2= time.localtime (expiration_time)
                        i= timeh1+timem1

                        while i>-1:
                            print(i)
                            letsgo=humanfriendly.format_timespan(i)
                            i-=1
                            time.sleep(1)
                            if letsgo!= timer1:
                            timer1=letsgo
                            letsgo=humanfriendly.format_timespan(i)
                            print("\nTime Left is:",letsgo, "seconds",time.strftime("%X",struct2))
                            timer2.config(text=letsgo)


                timer()
    finally:
    GPIO.cleanup()

time1 = ''
clock = Label(win, font=('times', 20, 'bold'))
clock.pack(fill=BOTH, expand=1)
def tick():
    global time1
    time2 = time.strftime('%H:%M:%S')

    if time2 != time1:
        time1 = time2
        clock.config(text=time2)

    clock.after(200, tick)
tick()

time1 = ''
clock = Label(win2, font=('times', 20, 'bold'))
clock.pack(fill=BOTH, expand=1)
def tick():
    global time1

    time2 = time.strftime('%H:%M:%S')

    if time2 != time1:
        time1 = time2
        clock.config(text=time2)

    clock.after(200, tick)
tick()



win.title("First GUI")
win.geometry('480x320')



addButton = Button(win, text = "+", font = myFont1, command = add, height = 2, width = 4)
addButton.place(x=310, y=50)

subButton = Button(win, text = "-", font = myFont1, command = sub, height = 2, width = 4)
subButton.place(x=85, y=50)

timeDisp1 = Text(win,font=myFont2, height = 1, width = 2) #Time display minutes 
timeDisp1.place(x=235, y=50)

timeDisp2 = Text(win,font=myFont2, height = 1, width = 2) #Time display hours
timeDisp2.place(x=160, y=50)

payButton = Button(win, text = "PAY", font = myFont1, command = pay, height = 2, width = 6)
payButton.place(x=188, y=250)

timeText = Label(win, text = 'ADD DESIRED TIME\n MAX TIME IS 4 HOURS')
timeText.place(x=165, y= 20)

startButton= Button(win,text="Press To Begin Adding Time", font = myFont1, command = lambda : startButton.destroy(), height = 12, width = 40)
startButton.place(x=0, y=0)



#GUI Window 2
win2.title("Second GUI")
win2.geometry ('480x320')

time1Text = Label(win2, text = 'TIME REMAINING')
time1Text.place(x=165, y= 20)





win.mainloop()
win2.mainloop()

这是第二个代码的输出,其中没有捕获时间。

过期时间为:0     开始时间:14:27:36

在工作时,thonny外壳中的打印内容是:

剩余时间为:44分58秒15:59:14

2698

但是当我合并它以捕获第一个gui窗口的输入时,它在我创建的第二个窗口中没有显示任何数据。

0 个答案:

没有答案