I am trying to update the value of a label with a slider. I have a news paper object that has a page number variable.
When I move the slider the label should show the sider value multiplied by the page number (so let's say the slider value is 10 and the news paper has 100 pages the label text should become 1000).
Here's my code so far:
from tkinter import *
class NewsPaper:
def __init__(self, title, pageNumber):
self.title = title
self.pageNumber = pageNumber
newsPapers = []
newsPaper1 = NewsPaper("daily news", 52)
newsPapers.append(newsPaper1)
root = Tk()
var = IntVar()
def setCost(var, newsPaper):
var.set( var.get() * newsPaper.pageNumber)
Label(root, textvariable=var).pack()
Scale(root, from_=0, to=20, variable=var, command=setCost(var, newsPapers[0])).pack()
root.mainloop()
For some reason the label text only shows the slider value, not the slider value*the number of pages...
CodePudding user response:
In case anybody stumbles upon this question because they have the same problem, here's the answer:
from tkinter import *
class NewsPaper:
def __init__(self, title, pageNumber):
self.title = title
self.pageNumber = pageNumber
newsPapers = []
newsPaper1 = NewsPaper("daily news", 52)
newsPapers.append(newsPaper1)
root = Tk()
var1 = IntVar()
var2 = IntVar()
def setCost(var, newsPaper):
var2.set( var1.get() * newsPaper.pageNumber)
Label(root, textvariable=var2).pack()
Scale(root, from_=0, to=20, variable=var1, command=lambda val, var=var1:setCost(var1, newsPapers[0])).pack()
root.mainloop()