Tkinter动态标签展示

问题描述 投票:0回答:1

我正在制作简单的窗口,喜欢在里面有时钟。了解如何在单独的窗口中动态显示时间: enter image description here

不知何故,放入我的项目中的相同代码仅显示标签的紫色背景:

def time():
    string = strftime('%H:%M:%S %p')
    lbl.config(text=string)
    lbl.after(1000, time)

class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        main_frame = tk.Frame(self, height=600, width=1024)
        main_frame.pack_propagate(0)
        main_frame.pack(fill="both", expand="true")
        main_frame.grid_rowconfigure(0, weight=1)
        main_frame.grid_columnconfigure(0, weight=1)

        frame1 = tk.LabelFrame(self, text="Parametry Pracy")
        frame1.place(rely=0.05, relx=0.02, height=400, width=400)


        pp=tk.Frame(frame1)
        pp.pack(pady=2)

        lbl = Label(pp, font=('arial', 20, 'bold'),bg='purple', fg='white', width=11)
        lbl.grid(row=0, column=0, sticky="W", padx=20)

enter image description here

tkinter tkinter-layout tkinter-label
1个回答
0
投票

这是经过更改的工作代码:

class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        main_frame = tk.Frame(self, height=600, width=1024)
        main_frame.pack_propagate(0)
        main_frame.pack(fill="both", expand="true")
        main_frame.grid_rowconfigure(0, weight=1)
        main_frame.grid_columnconfigure(0, weight=1)

        frame1 = tk.LabelFrame(self, text="Parametry Pracy")
        frame1.place(rely=0.05, relx=0.02, height=400, width=400)


        pp=tk.Frame(frame1)
        pp.pack(pady=2)

        def time():
            string = strftime('%H:%M:%S %p')
            lbl.config(text=string)
            lbl.after(1000, time)

        # Styling the label widget so that clock
        # will look more attractive
        lbl = Label(pp, font=('arial', 40, 'bold'), width=11)
        lbl.grid(row=0, column=0, sticky="W", padx=20)
        #lbl.pack(side = LEFT)
        time()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.