我正试图创建一个简单的应用程序,在24小时(军事)和12小时之间切换的上午/下午(标准)时间.我正在使用带有2个按钮小工具的Tkinter,所以如果我按下标准时间按钮,应用程序将切换到标准时间按钮,禁用标准时间按钮,启用军事时间按钮,如果我按下军事时间按钮,应用程序将切换到军事时间,军事按钮将被禁用,并启用标准时间按钮.

我面临的问题是,当我点击应用程序上的按钮小工具切换到军事时间时,它开始失控,我无法切换回标准时间,所以我在这里遇到了障碍.我有两个函数,每个函数用于当一个按钮被按下时,函数被调用,display_standard_time()display_military_time().我也try 了只使用一个功能来启用和禁用switch ,结果与上面提到的相同.

下面的代码是我目前拥有的代码:

import tkinter as tk
from tkinter import ttk, Label #used to create buttons and widgets
from time import strftime


def display_military_time():
 current_time = strftime('%H: '+'%M: '+'%S '+'%p') + \
                '\n'+ strftime('%b '+  '%d')+ ','  + strftime('%Y')
 display_clock.configure(text=current_time)
 military_time_button["state"] = "disabled"#disable military_button
 standard_time_button["state"] = "normal"#enable standard_button
 display_clock.after(80, display_military_time) #this statement allows to execute display_military_time() after 100ms


def display_standard_time():

 current_time = strftime('%I: ' + '%M: ' + '%S ' + '%p') + \
                '\n'+ strftime('%b '+  '%d')+ ','  + strftime('%Y')
 display_clock.configure(text=current_time)
 standard_time_button["state"] = "disabled"#disable standard_button
 military_time_button["state"] = "normal"#enable military_button
 display_clock.after(80, display_standard_time) 

if __name__ == '__main__':

 main_window = tk.Tk()#Create an instance of tk.Tk class and create an application window

#Place label on window and write text in the window
 main_window.title('Digital Clock') #Name of the window
 main_window.geometry('800x600') #method to change the size and location of the window. , widthxheight±x±y
 main_window.resizable(False, False)#Prevents window from being resized
 tabControl = ttk.Notebook(main_window)#Initialize Tabs
 clock_tab = ttk.Frame(tabControl)

 tabControl.add(clock_tab, text='Clock')
 tabControl.pack(expand=1, fill="both")


 display_clock = Label(clock_tab,fg='black', font=("arial", 80))
 display_clock.place(x=50, y=200)
 clock_label = Label(clock_tab,fg='black',font=("arial", 60, 'bold'), text = 'Current Time')
 clock_label.place(x=150, y=100)
 military_time_button = tk.Button(clock_tab,text="MILITARY TIME",bd=10,bg="grey", fg="blue",command=display_military_time,activeforeground="Orange",
                   activebackground="blue",
                   font="Andalus",
                   height=2,
                   highlightcolor="purple",
                   justify="right",
                   state = 'normal')
 military_time_button.place(x=100, y=470)
 standard_time_button = tk.Button(clock_tab,text="STANDARD TIME",bd=10,bg="grey", fg="red",command=display_standard_time,activeforeground="Orange",
                   activebackground="blue",
                   font="Andalus",
                   height=2,
                   highlightcolor="purple",
                   justify="right",
                   state= 'disabled')
 standard_time_button.place(x=500, y=470)

 display_standard_time()
 main_window.mainloop()#keeps the window displaying otherwise it disappears

推荐答案

您应该将时钟更新分离到另一个函数中,并在该函数上使用.after():

import tkinter as tk
from tkinter import ttk, Label
from time import strftime

# function to update the clock periodically
def update_clock():
    display_clock['text'] = strftime(time_fmt)
    display_clock.after(1000, update_clock)

def display_military_time():
    global time_fmt # store the clock format
    time_fmt = '%H:%M:%S %p\n%b %d, %Y'
    military_time_button["state"] = "disabled"
    standard_time_button["state"] = "normal"

def display_standard_time():
    global time_fmt
    time_fmt = '%I:%M:%S %p\n%b %d, %Y'
    military_time_button["state"] = "normal"
    standard_time_button["state"] = "disabled"

if __name__ == '__main__':

    main_window = tk.Tk()

    main_window.title('Digital Clock')
    main_window.geometry('800x600')
    main_window.resizable(False, False)
    tabControl = ttk.Notebook(main_window)
    clock_tab = ttk.Frame(tabControl)

    tabControl.add(clock_tab, text='Clock')
    tabControl.pack(expand=1, fill="both")


    display_clock = Label(clock_tab,fg='black', font=("arial", 80))
    display_clock.place(x=50, y=200)
    clock_label = Label(clock_tab,fg='black',font=("arial", 60, 'bold'), text = 'Current Time')
    clock_label.place(x=150, y=100)
    military_time_button = tk.Button(clock_tab,text="MILITARY TIME",bd=10,bg="grey", fg="blue",
                                     command=display_military_time,
                                     activeforeground="Orange",
                                     activebackground="blue",
                                     font="Andalus",
                                     height=2,
                                     highlightcolor="purple",
                                     justify="right",
                                     state = 'normal')
    military_time_button.place(x=100, y=470)
    standard_time_button = tk.Button(clock_tab,text="STANDARD TIME",bd=10,bg="grey", fg="red",
                                     command=display_standard_time,
                                     activeforeground="Orange",
                                     activebackground="blue",
                                     font="Andalus",
                                     height=2,
                                     highlightcolor="purple",
                                     justify="right",
                                     state= 'disabled')
    standard_time_button.place(x=500, y=470)

    display_standard_time() # set initial clock format
    update_clock() # start updating clock
    main_window.mainloop()

Python相关问答推荐

'discord.ext. commanders.cog没有属性监听器'

Python上的Instagram API:缺少client_id参数"

Pandas 有条件轮班操作

运行Python脚本时,用作命令行参数的SON文本

修复mypy错误-赋值中的类型不兼容(表达式具有类型xxx,变量具有类型yyy)

"使用odbc_connect(raw)连接字符串登录失败;可用于pyodbc"

给定高度约束的旋转角解析求解

实现神经网络代码时的TypeError

考虑到同一天和前2天的前2个数值,如何估算电力时间序列数据中的缺失值?

在Python中调用变量(特别是Tkinter)

用SymPy在Python中求解指数函数

如何合并具有相同元素的 torch 矩阵的行?

Seaborn散点图使用多个不同的标记而不是点

在任何要保留的字段中添加引号的文件,就像在Pandas 中一样

我如何处理超类和子类的情况

如何在Polars中创建条件增量列?

Pandas 删除只有一种类型的值的行,重复或不重复

try 在单个WITH_COLUMNS_SEQ操作中链接表达式时,使用Polars数据帧时出现ComputeError

对包含JSON列的DataFrame进行分组

大Pandas 每月重新抽样200万只和300万只