0

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...

Gobhniu
  • 265
  • 3
  • 11
  • 1
    You called `setCost()` *once*, in the process of creating the Scale, and passed its *return value* (which is None) as the `command=` option of the Scale. I'll link to a similar problem involving Buttons; the solutions are exactly the same. – jasonharper Sep 27 '21 at 14:25
  • Does this answer your question? [tkinter creating buttons in for loop passing command arguments](https://stackoverflow.com/questions/10865116/tkinter-creating-buttons-in-for-loop-passing-command-arguments) – jasonharper Sep 27 '21 at 14:25
  • Well I changed 'command=setCost(var, newsPapers[0])' for 'command=lambda var=var:setCost(var, newsPapers[0])' but now I get this error: var.set( var.get() * newsPaper.pageNumber) AttributeError: 'str' object has no attribute 'set' I guess it's because I pass my var as a string on my Label? – Gobhniu Sep 27 '21 at 14:44
  • 1
    Oops, the `command=` of a Scale does have one difference compared to a Button - it gets called with a parameter, which is the new value of the Scale, which is overwriting your `var` parameter. You need to provide an extra parameter to receive this value: `lambda val, var=var: ...`. Also, you need separate `IntVar`s for the Label and the Scale, since the whole point of your code is that they don't have the same value. – jasonharper Sep 27 '21 at 15:13
  • Thanks, I posted the answer I found with your help. – Gobhniu Sep 27 '21 at 15:30

1 Answers1

0

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()
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Gobhniu
  • 265
  • 3
  • 11