TkInter将字符串发送到IntVar

时间:2014-11-14 13:14:32

标签: python tkinter

以下是我作为报名表格设计的Tkinter窗口之一。它有许多行(通常由另一个函数定义)和一组列数,由len(self.names)定义。

正确地创建了Tkinter Int和String变量,但是当根据条目小部件中的值设置Int的值时,它会抛出一个错误,指出int()的基数为10的无效文字:'& #39 ;.我知道你不能将字符串设置为int,但是在我尝试设置值之前,很明显文本变量并没有被链接到'正确到IntVar,因为在其他窗口中,条目小部件的默认值为0,其中IntVar支持它,在这种情况下,这不会发生。

你应该能够运行下面的代码,它是一个(大多数)工人阶级。

任何见解都将受到赞赏。

from Tkinter import *

class app:

    def __init__(self):

        self.getFiles()

    def getFiles(self):
        self.numHelidays = 2
        root=Tk()
        self.master=root

        self.files = []
        self.names = ("HELI", "DAY", "DATE (yyyymmdd)", "type or browse for group shapefiles", "type or browse for route shapefiles")

        i = 0
        for f in self.names:
            self.fileLabel=Label(self.master,text=f)
            self.fileLabel.grid(column=i, row=0)
            i = i+1

        for heliday in range(self.numHelidays):
            self.files.append([])
            col = 0
            for column in self.names:
                if col == 3 or col == 4:
                    self.fileEntry=Entry(self.master,width=60, textvariable = self.files[heliday].append(StringVar()))
                else:
                    self.fileEntry=Entry(self.master,width=60, textvariable = self.files[heliday].append(IntVar()))
                #self.files[heliday][col].set(self.fileEntry)
                self.fileEntry.grid(column = col, row=heliday+1)

                col = col+1

#now for 'next' button
        self.submit = Button(self.master, text="Finish...", command=self.fileManager, fg="red")
        self.submit.grid(row=(self.numHelidays)+2, column=0)

      #  self.quit = Button(self.master, text="Quit...", command=self.deleteData, fg="red")
      #  self.quit.grid(row=(self.numHelidays)+2, column=1)

    def fileManager(self):
        print self.files
        for i in self.files:
            for l in i:
                print l.get()

app()

编辑:

它现在以这种格式运行。似乎尝试将字符串/ intvar附加到列表中同时也尝试将其分配给textvariable并不起作用,我必须先创建字符串/ intvar,然后再将其分配给textvariable,然后再附加它。我不明白为什么这应该有用,但上面没有,因为它肯定会做同样的事情。我猜Python在上面的脚本中只是按错误的顺序做事。

            if col == 3 or col == 5:
                txt = StringVar()
                self.fileEntry=Entry(self.master,width=60, textvariable = txt)
                self.fileEntry.grid(column = col, row=heliday+1)
            else col == 2:
                txt = IntVar()
                self.fileEntry=Entry(self.master,width=20, textvariable = txt)
                self.fileEntry.grid(column = col, row=heliday+1)

            self.files[heliday].append(txt)
            self.files[heliday][col].set(txt.get())
            col = col+1

1 个答案:

答案 0 :(得分:0)

顾名思义,textvariable应该是StringVar实例。你必须将你得到的字符串转换为任何其他类,就像使用int一样。

因为tcl适用于字符串,即使将非字符串对象传递给.set(),Vars也会设置为字符串。但是,Intvar.get()在字符串上调用int,因此如果无法将其转换为int,则会引发ValueError。

>>> root=tk.Tk()
>>> i = tk.IntVar(root)
>>> i.set(11)
>>> i.get()
11
>>> i.set('11')
>>> i.get()
11
>>> i.set(11.3)
>>> i.get()
11
>>> i.set('abc')
>>> i.get()
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    i.get()
  File "C:\Programs\Python34\lib\tkinter\__init__.py", line 358, in get
    return getint(self._tk.globalgetvar(self._name))
ValueError: invalid literal for int() with base 10: 'abc'
>>> i.set([1])
>>> i.get()
...
ValueError: invalid literal for int() with base 10: '[1]'

如果您有任何疑问,可以通过此类实验来确定实际工作方式。