简体   繁体   English

在 tkinter、时钟应用程序中的帧之间切换

[英]Switching between frames in tkinter, clock app

Good day, guys.美好的一天,伙计们。 I've got a problem with switching between frames in tkinter.我在 tkinter 中的帧之间切换时遇到问题。 I'm trying to make a clock app with time, stopwatch, timer and alarm.我正在尝试制作一个带有时间、秒表、计时器和闹钟的时钟应用程序。 If I click on the first frame it's working, but if I select the second frame it's breaks.如果我点击第一帧它正在工作,但如果我选择第二帧它会中断。 The previous frame just don't open because the second frame refreshing too fast (lbl.after).前一帧没有打开,因为第二帧刷新太快(lbl.after)。 Can you help me with switching between frames?你能帮我在帧之间切换吗?

帧间切换

from tkinter import *
from tkmacosx import *
from time import strftime

root = Tk()
root.title('Time')
root.geometry('500x500')
root.resizable(False, False)

counter = -1
running = False

def time_frame():
    string = strftime("%H:%M:%S %A %d %Y")
    frame_time = Frame(root, width = 480, height = 410, bg = 'gray70')
    frame_time.place(x = 10, y = 80)
    lbl1 = Label(frame_time, text = list, width = 32, height = 10, font = ('Arial', 25))
    lbl1.place(x = 10, y = 80)
    lbl1.config(text=string)
    lbl1.after(1000, time_frame)

def stopwatch_frame():
    frame_stopwatch = Frame(root, width = 480, height = 410, bg = 'gray70')
    frame_stopwatch.place(x = 10, y = 80)
    lbl2 = Label(frame_stopwatch, text = '0', width = 25, height = 5, font = ('Arial', 25))
    lbl2.place(x = 10, y = 80)
    start_button = Button(frame_stopwatch, width = 80, height = 40, text = 'Start')
    start_button.place(x = 10, y = 240)
    stop_button = Button(frame_stopwatch, width = 80, height = 40, text = 'Stop')
    stop_button.place(x = 100, y = 240)
    reset_button = Button(frame_stopwatch, width = 80, height = 40, text = 'Reset')
    reset_button.place(x = 190, y = 240)
    lbl2.after(1, stopwatch_frame)

frame_top = Frame(root, width = 500, height = 70, bg = 'gray64')
frame_top.place(x = 0, y = 0)

time_btn = Button(frame_top, text = 'Time', width = 50, height = 50, command = time_frame)
time_btn.place(x = 10, y = 10)

stopwatch_btn = Button(frame_top, text = 'Stopwatch', width = 50, height = 50, font = ('Arial', 9), command = stopwatch_frame)
stopwatch_btn.place(x = 80, y = 10)

timer_btn = Button(frame_top, text = 'Timer', width = 50, height = 50)
timer_btn.place(x = 150, y = 10)

alarm_btn = Button(frame_top, text = 'Alarm', width = 50, height = 50)
alarm_btn.place(x = 220, y = 10)

root.mainloop()

Here is a simplified example of a clock and stop watch class.这是时钟和秒表类的简化示例。

import tkinter as tk
from tkmacosx import *
from time import strftime, process_time

class timer:

    delaya = None
    delayb = None

    def __init__( self ):
        self.root = tk.Tk()
        self.root.title('Time')
        self.buttona = tk.Button( self.root, text = 'clock', width = 40, command = self.setclock )
        self.buttonb = tk.Button( self.root, text = 'watch', width = 40, command = self.setwatch )
        self.buttona.grid( row=0, column=0, sticky='ew' )
        self.buttonb.grid( row=0, column=1, sticky='ew' )
        self.root.update_idletasks()
        self.root.resizable( False, False )

    def clock( self ):
        self.root.after_cancel( self.delaya )
        self.delaya = None
        self.buttona[ 'text' ] = strftime("%H:%M:%S %A %d %Y")
        self.delaya = self.root.after( 1000, self.clock )

    def watch( self ):
        self.root.after_cancel( self.delayb )
        self.delayb = None
        self.buttonb[ 'text' ] = f'{process_time():0.5f}'
        self.delayb = self.root.after( 1, self.watch )

    def setclock( self ):
        if self.delaya == None:
            self.delaya = self.root.after( 1000, self.clock )
        else:
            self.root.after_cancel( self.delaya )
            self.delaya = None
            self.buttona[ 'text' ] = 'clock'

    def setwatch( self ):
        if self.delayb == None:
            self.delayb = self.root.after( 1, self.watch )
        else:
            self.root.after_cancel( self.delayb )
            self.delayb = None
            self.buttonb[ 'text' ] = 'watch'

if __name__ == '__main__':

    device = timer( )
    tk.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM