Question

My problem is thatI Have a scale and a spinbox which change each other's values. OFr example if both go from 1 to 100, if I set the scale to 50, the spinbox will also change and vice versa. now I've got this to work pretty well except for one minor problem. I can't get the ttk scale to go up by whole numbers. Everytimer I change the scale, I get a ton of decimals behind my number. Here is my code:

def create_widgets(self):
"""my widgets"""
    spinval = IntVar()

    self.scale = ttk.Scale(self, orient = HORIZONTAL,
                                   length = 200,
                                   from_ = 1, to = 100,
                                   variable = spinval)
    self.scale.grid(row = 3,column = 1,sticky = W)


    self.spinbox = Spinbox(self, from_ = 1, to = 100,
                                   textvariable = spinval,
                                   command = self.update,
                                   width = 10)
    self.spinbox.grid(row = 3,column =3,sticky = W)

def update(self, nothing):
    """Updates the scale and spinbox"""
    self.scale.set(self.spinbox.get())

Now my question is: is it somehow possible to make it increment by whole numbers or change the graphic of the normal Tkinter scale so it looks better. Any help is welcome.

Was it helpful?

Solution

def create_widgets(self):
    """my widgets"""
    spinval = IntVar()

    self.scale = ttk.Scale(self, orient=HORIZONTAL,
                                length=200,
                                from_=1, to=100,
                                variable=spinval,
                                command=self.accept_whole_number_only)
    self.scale.grid(row=3, column=1, sticky=W)


    self.spinbox = Spinbox(self, from_=1, to=100,
                                textvariable=spinval,
                                command=self.update,
                                width=10)
    self.spinbox.grid(row=3,column=3, sticky=W)

def accept_whole_number_only(self, e=None):
    value = self.scale.get()
    if int(value) != value:
        self.scale.set(round(value))

def update(self, e=None):
    """Updates the scale and spinbox"""
    self.scale.set(self.spinbox.get())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top