Josh Josh - 25 days ago 10
Python Question

How do I get tkinter scale widget to set it's upper limit to an updated variable?

I'm trying to set the 'to=' configuration option on a tkinter scale widget. I want to take the user input from an entry widget and use it as the upper limit on the scale.

The way I have it set up now, the scale just stays stuck at 0 after the user enters a value in the entry box and clicks the button. The 'to=' configuration option doesn't seem to update after the user changes the variable.

How do I get the scale widget to set it's upper limit to the new value?

Here's the code:

import tkinter

class HomePage:
def __init__(self):
self.root = tkinter.Tk()
self.root.geometry('300x300')

self.entry_value = tkinter.IntVar()

self.entry_box = tkinter.Entry(master=self.root)
self.entry_box.pack(pady=10)

self.button = tkinter.Button(master=self.root, text='get value', command=self.set_value)

self.button.pack(pady=10)

self.scale = tkinter.Scale(master=self.root, from_=0, to=self.entry_value.get(), orient='horizontal')
self.scale.pack(pady=10)

self.show_entry_value = tkinter.Button(self.root, text='Updated Value',
command=lambda: print(self.entry_value.get()))
self.show_entry_value.pack()

def set_value(self):
self.entry_value.set(self.entry_box.get())


app = HomePage()
app.root.mainloop()

Answer

Use the configure method to change the value:

self.scale.configure(to=the_new_value)