首页 文章

TkInter将字符串发送到IntVar

提问于
浏览
-2

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

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

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

任何见解将不胜感激 .

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不起作用,我必须在将其分配给textvariable之前创建字符串/ intvar,然后在之后附加它 . 我不明白为什么这应该有用,但上面没有,因为它确实在做同样的事情 . 我猜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

    顾名思义,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]'
    

    当您有疑问时,请尝试这样来确定实际工作方式 .

相关问题