Python 从条目中获取整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4199278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get an Integer from Entry
提问by Paris
How do I get an integer type from the Tkinter Entrywidget?
如何从 TkinterEntry小部件获取整数类型?
If I try to get the value using variable_name.get(), it says it is a str. If I try to change the type using int(variable_name.get()), it says int can only accept a string or number.
如果我尝试使用 获取值variable_name.get(),它会说它是一个str. 如果我尝试使用 更改类型int(variable_name.get()),它会说 int 只能接受字符串或数字。
Any help would be welcome!
欢迎任何帮助!
回答by pyfunc
See the following example from Tkinter:
请参阅以下来自 Tkinter 的示例:
v = StringVar()
e = Entry(master, textvariable=v)
e.pack()
v.set("a default value")
s = v.get()
v is a StringVar class. try doing - int(str(variable.get()))
v 是一个 StringVar 类。尝试做 - int(str(variable.get()))
But the class says that variable.get should return you a string.
但是课程说 variable.get 应该返回一个字符串。
Tkinter Entry has a default text value, that might be an issue if it is not integer data.
Tkinter Entry 有一个默认文本值,如果它不是整数数据,这可能是一个问题。
回答by Bryan Oakley
When you say "variable" in your question, are you referring to an instance of a StringVaror a reference to the widget itself?
当您在问题中说“变量”时,您是指 a 的实例StringVar还是对小部件本身的引用?
If you have a reference to the widget you can use the getmethod of the entry. This will return a string that you can then pass to the intfunction:
如果您有对小部件的引用,则可以使用get条目的方法。这将返回一个字符串,然后您可以将其传递给int函数:
s = e.get()
i = int(s)
If this doesn't solve your problem can you provide some working code that shows exactly the problem you are having?
如果这不能解决您的问题,您能否提供一些工作代码来准确显示您遇到的问题?
回答by Brandon
I ran into this same problem recently. I had a small method running :
我最近遇到了同样的问题。我有一个小方法正在运行:
newSave = self.EntryDict[key].get
try:
int(newSave)
bool = True
except ValueError:
bool = False
And I got the error you are describing that It can only accept a string or a number. And my problem was that my .get didn't have the parenthesis. After I put .get() everything worked. I think this might be your problem too. But not seeing any of your code I can't know for sure.
我得到了你描述的错误,它只能接受一个字符串或一个数字。我的问题是我的 .get 没有括号。在我放置 .get() 之后一切正常。我想这也可能是你的问题。但是没有看到您的任何代码,我不确定。
回答by Tymric
I am new to Tkinter as well, and I also faced the same problem. I think the issue is that the default value of the Entry field is an empty string, which cannot be converted to integer.
我也是 Tkinter 的新手,我也遇到了同样的问题。我认为问题是 Entry 字段的默认值是一个空字符串,无法转换为整数。
I suggest you try adding an if statement to check for that case:
我建议您尝试添加一个 if 语句来检查这种情况:
if variable.get() != '':
other_variable = int(variable.get())
I understand that the question is old, but this worked for me and I thought I'd share.
我知道这个问题很老,但这对我有用,我想我会分享。
回答by user3429497
In the end I just initialized the variable as tk.IntVar()instead of tk.StringVar()
最后,我只是将变量初始化tk.IntVar()为tk.StringVar()
That way you don't have to cast it as an int, it will always be one, and the default value from the Entry element will now be 0instead of ''
这样你就不必将它转换为 int,它将始终为 1,并且 Entry 元素的默认值现在将0改为''
That's how I approached it anyway, seems the simplest way and avoid the need for a lot of the validation you'd need to do otherwise...
无论如何,这就是我接近它的方式,似乎是最简单的方法,并且避免了需要进行大量验证的需要,否则...
回答by Taavi Kreem
int(str(variable.get()))
I try this and it works now.
我试试这个,现在可以用了。

