如何创建“数组"? Python使用多个tkinter Scale小部件? [英] How to create an "array" of multiple tkinter Scale widgets with Python?

查看:204
本文介绍了如何创建“数组"? Python使用多个tkinter Scale小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python 3.2.3和tkinter模块创建GUI.我需要一个Scale小部件的数组",但我一生无法解决如何返回值,除非一次创建一个Scale小部件,并为每个由其关键字参数传递var.

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.

我可以循环窗口小部件创建位并根据需要增加行和列参数,但无法弄清楚如何检索Scale小部件的值.

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.

在基本"中,每个小部件都会有一个索引,该索引可用于解决该问题,但是我找不到在Python中如何实现类似的功能.更糟糕的是,我只使用了一个Scale小部件:

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()

得到答复:

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

我认为这是某种版本问题.

I am assuming this is some kind of version issue.

推荐答案

确定要使用Python 3吗?您的示例是Python 2. 这个简单的示例可用于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()

我希望那是你想要的.

JPG

这篇关于如何创建“数组"? Python使用多个tkinter Scale小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆