how can a value be retrieved from an event loop?

Phil <[email protected]>
Newsgroups gmane.comp.python.tutor
Message-ID <[email protected]>
Thank for reading this.

This knob class does what it's supposed to do but I cannot see how to 
retrieve the angle or set the angle from an instance of the class. The 
angle can be retrieved and printed from within the event loop but not 
outside the loop.

I've found a similar class that does return a value to an instance of 
the class but it's vastly more complicated than what I've presented here 
and even though it's similar I cannot fathom how it works.

Is there a simple fix or is there a major flaw in the design?

import tkinter as tk
import math


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,
         )

         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

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

     root.mainloop()

-- 

Regards,
Phil

_______________________________________________
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.