How to run a function in the background of tkinter

后端 未结 5 2163
独厮守ぢ
独厮守ぢ 2021-01-04 21:48

I am new to GUI programming and I want to write a Python program with tkinter. All I want it to do is run a simple function in the background that can be influenced through

5条回答
  •  灰色年华
    2021-01-04 22:16

    Try to understand this example : clock updating in backgroud, and updating GUI ( no need for 2 threads ).

    # use Tkinter to show a digital clock
    # tested with Python24    vegaseat    10sep2006
    from Tkinter import *
    import time
    root = Tk()
    time1 = ''
    clock = Label(root, font=('times', 20, 'bold'), bg='green')
    clock.pack(fill=BOTH, expand=1)
    def tick():
        global time1
        # get the current local time from the PC
        time2 = time.strftime('%H:%M:%S')
        # if time string has changed, update it
        if time2 != time1:
            time1 = time2
            clock.config(text=time2)
        # calls itself every 200 milliseconds
        # to update the time display as needed
        # could use >200 ms, but display gets jerky
        clock.after(200, tick)
    tick()
    root.mainloop(  )
    

    credits: link to site

提交回复
热议问题