Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment by 1 for ttk scale

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.

like image 275
Tobias Mielke Avatar asked Dec 14 '25 05:12

Tobias Mielke


1 Answers

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())
like image 170
falsetru Avatar answered Dec 15 '25 18:12

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!