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 13:45, Alan Gauld via Tutor wrote:

> OK, I took your code and tweaked it to do what I think you want.
> Note that you now set the initial angle by passing it to the
> constructor call.

I was sufficiently intrigued by this that I took another pass
and added a label to demonstrate how it could be used and
did the display conversion to degrees too. I hope that it's
helpful. I also tidied up the code some more.

import tkinter as tk
import math


class KNOB(tk.Canvas):
     def __init__(
         self,
         parent,
         size=100,
         angle=0,
         string_var = None
     ):
         super().__init__(parent, width=size, height=size)

         self.size = size
         self.color1 = "#007DC8"
         self.color2 = "#98D2D2"
         self.angle = angle % (2 * math.pi) # keep within 0-2pi
         self.str_var = string_var

         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 = None   # make a "dot" for first draw_dot to remove!
         self.draw_dot()

         self.bind("<Button-3>", self.turn_clockwise)
         self.bind("<Button-1>", self.turn_counterwise)

     def draw_dot(self):
         self.delete(self.dot)
         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,
         )
         if self.str_var:
             self.str_var.set("%5.2f deg." % math.degrees(self.angle))

     def turn_clockwise(self, event):
         self.set_angle(self.angle + 0.1)
         self.draw_dot()


     def turn_counterwise(self, event):
         self.set_angle(self.angle - 0.1)
         self.draw_dot()

     def set_angle(self,angle):
         self.angle = angle % (2*math.pi)  # keep within 0-2pi range

if __name__ == "__main__":
     root = tk.Tk()
     root.title("Knob test")
     knob_angle = tk.StringVar()
     knob_angle.set("0")

     knob = KNOB(root, angle=15, string_var = knob_angle)
     knob.pack(padx=20, pady=20)
     lbl = tk.Label(root, textvariable=knob_angle)
     lbl.pack()

     root.mainloop()

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