Re: how can a value be retrieved from an event loop?

Alan Gauld via Tutor <[email protected]>
Newsgroups gmane.comp.python.tutor
Message-ID <[email protected]>
On 16/11/2023 08:38, Phil wrote:

> angle can be retrieved and printed from within the event loop but not 
> outside the loop.

That would be how you normally do things.
You should be ale to access the values outside the
mainloop(as you do in the code below) but changes
won't show up on the GUI until the mainloop runs
(unless who do a whole lot of extra stiff that
the mainloop normally does for you)


> class KNOB(tk.Canvas):
>      def __init__(
>          self,
>          parent,
>          size=100
>      ):
>          super().__init__(parent, width=size, height=size)
> 
>          self.size = size
>          self.color1 = "#007DC8"
>          self.color2 = "#98D2D2"
>          self.angle = 0
> 
>          self.border = self.create_oval(
>              0.1 * size,
>              0.1 * size,
>              0.9 * size,
>              0.9 * size,
>              outline=self.color1,
>              width=3,
>              fill=self.color2,
>          )
> 
>          self.dot = self.create_oval(
>              0.45 * size,
>              0.20 * size,
>              0.55 * size,
>              0.30 * size,
>              outline=self.color1,
>              width=3,
>          )

You could simplify your code by creating a draw_dot()
method which you use here and in the two event handlers below.


> 
>          self.bind("<Button-3>", self.turn_clockwise)
>          self.bind("<Button-1>", self.turn_counterwise)
> 
>      def turn_clockwise(self, event):
>          self.delete(self.dot)
>          self.angle = self.angle + 0.1
>          centerX = 0.5 * self.size + (0.25 * self.size * 
> math.sin(self.angle))
>          centerY = 0.5 * self.size - (0.25 * self.size * 
> math.cos(self.angle))
>          self.dot = self.create_oval(
>              (centerX - 0.05 * self.size),
>              (centerY - 0.05 * self.size),
>              (centerX + 0.05 * self.size),
>              (centerY + 0.05 * self.size),
>              outline=self.color1,
>              width=3,
>          )
>          #print(self.angle)
>          #return self.angle
> 
>      def turn_counterwise(self, event):
>          self.delete(self.dot)
>          self.angle = self.angle - 0.1
>          centerX = 0.5 * self.size + (0.25 * self.size * 
> math.sin(self.angle))
>          centerY = 0.5 * self.size - (0.25 * self.size * 
> math.cos(self.angle))
>          self.dot = self.create_oval(
>              (centerX - 0.05 * self.size),
>              (centerY - 0.05 * self.size),
>              (centerX + 0.05 * self.size),
>              (centerY + 0.05 * self.size),
>              outline=self.color1,
>              width=3,
>          )
>          #print(self.get_angle())
> 
>      def get_angle(self):
>          print("get_angle called")
>          return self.angle
> 
>      def set_angle(self, angle):
>          self.angle = angle
> 
> if __name__ == "__main__":
>      root = tk.Tk()
>      root.title("Knob test")
> 
>      knob = KNOB(root)
>      knob.pack(padx=20, pady=20)
> 
>      print(knob.get_angle()) # this returns 0  once but the angle value 
> is not updated here

Which is as expected since you only call this once and the method
only prints the value not change it..

>      knob.set_angle(15) # this sets the angle but only once the mouse 
> button is pressed

You are changing the value in the object but you are not
updating the GUI. Even calling the turnXXX handlers will
not do that, you need to get into processing the widgets
and painting them on screen.

That's the extra sauce that the event loop does for you.

>      root.mainloop()

If you called the turnXXX handlers before mainloop() the
changes should become visible once the loop runs.

I'm not quite sure what you are trying to achieve by
accessing/changing the value before the loop starts, it's
not something I've ever needed to do in a GUI program,
but it should certainly be possible. And from your description
is working as expected.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.