[Tutor] Custom Tkinter widgets part 2
Phil
phillor9 at gmail.com
Fri Nov 10 00:05:30 EST 2023
David answered my first custom widgets question in February this year
and I have created several widgets since then based on his answer. I
discovered an alternative method a few months ago and it also works well
until I decided to add a label.
I have created a dial widget (and multiple dial widgets of varying
sizes) which displays correctly now I want to add a label below the dial
to display the dial's value. So far I'm only able to display the dial or
the label but not both at the same time.
I hope these code snippets will be enough show how I'm attempting to
display both widgets.
First the DIAL class:
import tkinter as tk
import math
class DIAL(tk.Canvas):
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.
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")".
Hopefully, the correction is a simple syntax error rather the a design
flaw.
--
Regards,
Phil
More information about the Tutor
mailing list