1

I am trying to create a GUI using Python 3.2.3 and the tkinter module. I need an "array" of Scale widgets but cannot for the life of me figure out how to return the values except by creating the Scale widgets one at a time and having a separate function for each one called by its command keyword argument passing the var.

I can loop the widget creation bit and increment the row and column parameters as necessary but can't figure out how to retrieve the values of the Scale widgets.

In "Basic" each widget would have an index which could be used to address it but I cannot find how anything similar is implemented in Python. Even worse - just with a single Scale widget, I used:

from Tkinter import *

master = Tk()

w = Scale(master, from_=0, to=100)
w.pack()

w = Scale(master, from_=0, to=200, orient=HORIZONTAL)
w.pack()

mainloop()


#To query the widget, call the get method:

w = Scale(master, from_=0, to=100)
w.pack()

print w.get()

And got the response:

AttributeError: 'NoneType' object has no attribute 'get'

I am assuming this is some kind of version issue.

finefoot
  • 9,914
  • 7
  • 59
  • 102
Simon G
  • 11
  • 1
  • 2

1 Answers1

1

Are you sure you are using Python 3? Your example is Python 2. This simple example works with 1 widget:

from tkinter import *
master = Tk()
w = Scale(master, from_=0, to=100,command=lambda event: print(w.get())) 
w.pack()
mainloop()

With an array of widgets, you put them in a list

from tkinter import *
master = Tk()
scales=list()
Nscales=10
for i in range(Nscales):
    w=Scale(master, from_=0, to=100) # creates widget
    w.pack(side=RIGHT) # packs widget
    scales.append(w) # stores widget in scales list
def read_scales():
    for i in range(Nscales):
        print("Scale %d has value %d" %(i,scales[i].get()))
b=Button(master,text="Read",command=read_scales) # button to read values
b.pack(side=RIGHT)
mainloop()

I hope that's what you want.

JPG

JPG
  • 2,224
  • 2
  • 15
  • 15
  • Thank you very much - that is precisely what I wanted. One supplementary question is could you recommend a book or other reference/tutorial source ? I have been plagued by information relating to the wrong version of Python or Tkinter or tkinter. – Simon G Aug 27 '13 at 19:27
  • I began with [http://www.python-course.eu/tkinter_labels.php](http://www.python-course.eu/tkinter_labels.php) but it's very simple. I don't know any comprehensive book or tutorial. Perhaps someone else can give a better tip. – JPG Aug 27 '13 at 19:41
  • Thanks again, you have been very helpful. That site looks good to me - "simple" is what I need at the moment. – Simon G Aug 28 '13 at 07:08