2014-11-14 36 views
-2

下面是我的Tkinter窗口之一作为入口表单设计。它有许多行(通常由另一个函数定义)和一组由len(self.names)定义的列。TkInter发送字符串到IntVar

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

你应该能够运行下面的代码,它是一个(主要)工作班级。

任何有识之士将不胜感激。

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

这段代码是如何解释这个问题的?你在哪里设置“相关IntVar的价值”?当它抛出错误时,错误是什么? – 2014-11-14 13:33:39

回答

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]' 

如果您有任何疑问,实验是这样确定的事情是如何工作的。