Создание GUI в Tkinter с таймером? - PullRequest
1 голос
/ 22 апреля 2019

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, и я получаю вывод, но когда я иду, чтобы переместить его в код для захвата переменных

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

Когда он работает, печать в оболочке Тони:

Оставшееся время: 44 минуты 58 секунд 15: 59: 14

2698

Но когда я включаю его для захвата входных данных из первого окна графического интерфейса, он не показывает никаких данных во втором окне, которое я создал.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...