【发布时间】:2014-11-14 13:14:32
【问题描述】:
下面是我设计为输入表单的 Tkinter 窗口之一。它有许多行(通常由另一个函数定义)和一组由 len(self.names) 定义的列。
Tkinter Int 和 String 变量正在正确创建,但是当根据条目小部件中的值设置 Int 的值时,它会抛出一个错误,指出 int() 以 10 为底的无效文字:''。我知道您不能将字符串设置为 int,但即使在我尝试设置值之前,很明显 textvariable 没有正确“链接”到 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()
编辑:
它现在以这种格式工作。似乎尝试将 string/intvar 附加到列表同时尝试将其分配给 textvariable 是行不通的,我必须在将 string/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
【问题讨论】:
-
这段代码如何说明问题?您在哪里尝试“设置相关 IntVar 的值”?当它抛出错误时,错误是什么?