Home > database >  Update the number of messages sent
Update the number of messages sent

Time:09-07

I can't find a way to keep tkinter updating as the message are getting sent

import tkinter as tk   # PEP8: `import *` is not preferred


import pyautogui
import time


msg = input("Enter the message: ")
num = input("How many times ?: ")
wait = input("How much time to wait before the boom? ")


time.sleep(int(wait))

for i in range(0, int(num)):

    pyautogui.typewrite(msg   '\n')


def update_clock():
    # get current time as text

    # udpate text in Label
    lab.config(text=i)
    #lab['text'] = current_time

    # run itself again after 1000 ms
    root.after(1000, update_clock)




root = tk.Tk()

lab = tk.Label(root)
lab.pack()

# run first time at once
update_clock()

# run furst time after 1000ms (1s)
#root.after(1000, update_clock)

root.mainloop()

CodePudding user response:

For this example it would be simpler to set text in label inside your for-loop and use root.update() instead of root.mainloop() to force tkinter to redraw widgets in window.

import tkinter as tk
import pyautogui
import time

msg = input("Enter the message: ")
num = input("How many times ?: ")
wait = input("How much time to wait before the boom? ")

time.sleep(int(wait))

# --- GUI ---

root = tk.Tk()

lab = tk.Label(root)
lab.pack()

for i in range(int(num)):
    pyautogui.typewrite(msg   '\n')
    
    # set text in widget (but it will NOT redraw widget in window)
    lab.config(text=str(i))
    
    # force tkinter to redraw widgets in window
    root.update()

BTW: It will close GUI after displaying last value. If you want to keep it open then use root.mainloop() after for-loop.

  • Related