【问题标题】:TkInter sending String to IntVarTkInter 发送字符串到 IntVar
【发布时间】: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 的值”?当它抛出错误时,错误是什么?

标签: python tkinter


【解决方案1】:

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

当您有问题时,可以通过这样的实验来确定事情的实际运作方式。

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2016-11-13
    相关资源
    最近更新 更多