0

So basically i want to create a python tkinter canvas that gets updated every tick(loop 1) and if an event(such as clicking or motion) some function gets executed based on the updated values in loop 1 A simple example would be a tkinter window that keeps changing its background color hue (starting with red)every tick(e.g 50ms) with a white text 'red' and at the same time, if the mouse is clicked, the text gets updated showing the name of the color(e.g the hue is in range(75, 150) so 'green' appears on the screen and so on). I didn't fully understood tkinter event loop and mainloop(). i just know how to bind events and make a basic layout.So it would be very helpful to do an implementation of this in python tkinter. also in this case someone could maybe use the time module to get the current hue indirectly (not based on the variable on the loop 1)which want really answer my question. and thank you for helping.

winskireo
  • 11
  • 1
  • Use [`root.after`](https://stackoverflow.com/questions/29376970/correct-way-of-using-root-after-and-root-mainloop-in-tkinter). And tkinter has its own event loop, so all you need to do is use `root.after` and check for mouse click events. – Christian Dean Apr 17 '21 at 13:46
  • root.after dosent cause recursion problems? – winskireo Apr 17 '21 at 13:57
  • also the checking for mouse clicks should be where exactly – winskireo Apr 17 '21 at 13:59
  • No, it doesn't @winkiero. I know it might look like infinite recursion at first glance but it's not. Notice we're not calling a function inside of itself, we're just repeatedly passing the function to `root.after`, which calls the function after a certain period of time, and then the function schedules itself to be executed again, and so on. – Christian Dean Apr 17 '21 at 14:01
  • 1
    ok thanks ill try it out – winskireo Apr 17 '21 at 14:03
  • @winskiero The way tkinter works is by binding events to functions. What you need to do is create a function that gets the current color of the screen, and updates some sort of label with the name of the color. Then bind this function to the mouse click event. See [here](https://stackoverflow.com/questions/29211794/how-to-bind-a-click-event-to-a-canvas-in-tkinter) for an example of binding functions to events. – Christian Dean Apr 17 '21 at 14:03
  • No problem @winskiero – Christian Dean Apr 17 '21 at 14:04

1 Answers1

0

Here is an example of a clock application that uses the idea you want to implement:

import tkinter as tk
import time


class MainApplication:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Digital clock")
        self.root.geometry("350x200")
        self.current_time = time.strftime("%H:%M:%S")
        self.label = tk.Label(
            self.root, text=self.current_time, background="Green", font=("Arial", 60)
        )
        self.label.place(x="0", y="0", height="200", width="350")
        self.update_time()
        self.root.mainloop()

    def update_time(self):
        self.new_time = time.strftime("%H:%M:%S")
        self.label.config(text=self.new_time)
        self.root.after(1, self.update_time)


if __name__ == "__main__":
    app = MainApplication()

You need to make use of the after method. If you want something more clear provide your code with what have you attempted to do so far

S.B
  • 13,077
  • 10
  • 22
  • 49