English 中文(简体)
改写标签(Custom tkinter)
原标题:Changing the label progresivelly (Custom tkinter)

I ve been trying to program some kind of countdown in python with UI. It s my first time working with Custom tkinter. I ve encountered a small problem where if I input the time, the label won t change as it should. If you know how to change the text of the label into 00:00 format and make it change progresivelly, please let me know.

我在座谈了许多东西,如果有人对整部法典感兴趣的话:

from customtkinter import *
from PIL import Image
import time

app = CTk()
app.title( Countdown, hopefully )
app.geometry("500x400")
set_appearance_mode( light ) #or  light 


minutes, seconds = 0, 0
switch_var_1 = StringVar(value="off")


def countdown(t):

    def therest():
        setted_text.configure( {minutes:02d}:{seconds:02d} )

    while t:
        mins, secs = divmod(t, 60)
        timer =  {:02d}:{:02d} .format(mins, secs)
        therest()
        print(timer, end = "
")
        time.sleep(1)
        t -= 1

    print("ring")

def switch_event():
    if switch_var_1.get() ==  on :
        set_appearance_mode( dark )
    elif switch_var_1.get() ==  off :
        set_appearance_mode( light )

def start():
    t = time_entry.get()
    countdown(int(t))


entry_frame = CTkFrame(
    master=app,
    width=234,
    height=55,
    corner_radius=7,
    bg_color= transparent ,
    fg_color= transparent 
)

time_entry = CTkEntry(
    master=entry_frame,
    placeholder_text= Enter the time (in seconds): ,
    placeholder_text_color=( gray ,  #e3d296 ),
    text_color=( black ,  #ffe894 ),
    border_color=( gray ,  #e3d296 ),
    border_width=3,
    width=200,
    height=25,
    corner_radius=7
)

setted_text = CTkLabel(
    master=entry_frame,
    text= 00:00 ,
    font=( Brass Mono , 20),
    text_color=( black ,  #ffe894 )
)

def set_text():
    setted_text.configure(text=f"{time_entry.get()}")

button_entry = CTkButton(
    master=entry_frame,
    text= Set ,
    bg_color= transparent ,
    fg_color= transparent ,
    text_color=( black , #ffe894 ),
    border_color=( gray ,  #e3d296 ),
    hover_color=( #d9d9d9 ,  black ),
    corner_radius=1,
    height=25,
    width=40,
    border_width=3,
    command=set_text
)

switch_1 = CTkSwitch(
    master=app,
    text= Mode ,
    text_color=( black ,  #ffe894 ),
    command=switch_event,
    variable=switch_var_1, onvalue="on", offvalue="off",
    progress_color= #ddc774 ,
    button_color=( black ,  #ffe894 ),
    button_hover_color=( #3c3c3c ,  #ffeeac )
)

button_start = CTkButton(
    master=entry_frame,
    text= Start Countdown ,
    bg_color= transparent ,
    fg_color= transparent ,
    text_color=( black , #ffe894 ),
    border_color=( gray ,  #e3d296 ),
    hover_color=( #d9d9d9 ,  black ),
    corner_radius=7,
    height=25,
    width=40,
    border_width=3,
    command=start
)


switch_1.place(relx=1, rely=0.9, anchor= se )
time_entry.place(relx=0, rely=0, anchor= nw )
button_entry.place(relx=1, rely=0, anchor= ne )
entry_frame.place(relx=0.1, rely=0.1, anchor= nw )
setted_text.place(relx=0, rely=1, anchor= sw )
button_start.place(relx=1, rely=1, anchor= se )

app.mainloop()
问题回答

Edit: 添加屏幕。

I ve encountered a small problem where if I input the time, the label won t change as it should.

Snippet:

def countdown(t):

    def therest():
        setted_text.configure( {seconds:02d} )

    while t:
        mins, secs = divmod(t, 60)
        timer =  {:01d} .format(secs)
        
        therest()
        print(timer)
        setted_text.configure(text=timer)  
        time.sleep(1)
         
        t -= 1
        app.update() 
        
    print("ring")
    setted_text.configure(text= ring )

检查:

“entergraph

如果你看上<条码>底盘内,CTkLabel.configure()的定义:

def configure(自称需要_redraw=False, **kwargs)

其后,封顶职能中的以下线将不更新标签:<代码>therest():

setted_text.configure( {minutes:02d}:{seconds:02d} )

由于您通过该说明作为论据的价值<代码>require_redraw,而不是text/code>。

另外,建议不要在临时申请中使用,因为它将阻止<条码>mainloop(>)处理待处理的事件和更新。

使用<代码>后(,而不是停留时间:

def countdown(t):
    mins, secs = divmod(t, 60)
    timer =  {:02d}:{:02d} .format(mins, secs)
    setted_text.configure(text=timer)
    print(timer, end="
")
    if t > 0:
        # update the label again after 1 second
        setted_text.after(1000, countdown, t-1)
    else:
        print("ring")

值得注意的是,在计票期间,这些纽子更容易脱掉。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签