Re: Custom Tkinter widgets part 2

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

> First the DIAL class:

> class DIAL(tk.Canvas):

My first thought is that when creating any kind of custom
widget I start by inheriting from Frame. Frames can contain
anything and are self contained in terms of geometry
management. Canvases can contain most things so it should
work but just a thought.

>      def __init__(self, master, dial_type="semi", bg="white", *args, 
> **kwargs):
>          super().__init__(master, *args, **kwargs)
>          self.configure(width=100, height=100, bd=0, highlightthickness=0)
> 
>          self.min_value = 0
>          self.max_value = 100
>          self.value = 50
>          self.dial_type = dial_type
>          self.bg = bg
> 
>          self.draw_dial()
> 
>          # Create a label to display the dial value
> 
>          self.label = tk.Label(self, text=f"{self.value}")
>          #self.label.pack(side="bottom") # I think I can see why this 
> won't display the label on the canvas
> 
>      def draw_dial(self):
> 
> And here's part of a dial demo:
> 
>      root = tk.Tk()
>      root.title("Dial Widget Example")
> 
>      full_dial = DIAL(root, width=150, height=150, dial_type="full", 
> bg="red")
>      full_dial.pack(padx=20, pady=20) # dial or dials all display correctly
>      full_dial.label.pack(side="bottom") # the label needs to be 
> displayed on the canvas but this method is wrong.

You shouldn't have to pack the label twice. I don't know
what draw_dial does, but the fact you pack the label into
the Canvas means you should only need to pack the Canvas
in the client code.

> This is one of several attempts that I've tried. As it stands the label 
> is displayed where the dial would have been if I'd left out the line 
> "full_dial.label.pack(side="bottom")".

I'm not sure what that means. Can you post the entire DIAL code?
Hopefully it's not dependant on anything else? That way we can
run it rather than guess what is happening.

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