How to convert a StringVar to an Integer
本问题已经有最佳答案,请猛点这里访问。
我正在写一个基本的猜数字游戏,并且试图将程序放到一个窗口中。 一切正常,除非我尝试访问在
现在,从
1 2 3 4 5 | # Creates the entry box and variable that will be used in it # answer = StringVar() entry_box = Entry(window, textvariable = answer, width = 25, bg = 'white') .place(x = 350, y = 0) num1 = answer.get() |
因此,现在的答案是StringVar,而我正在尝试将其转换为Integer。 任何帮助,将不胜感激。
您只需要使用int():
1 | num1 = int(answer.get()) |
最简单的方法与在Python中将任何字符串转换为整数的方法相同:
1 2 | num1 = answer.get() # now we have a str num2 = int(num1) # now we have an int |
当然,如果用户在Entry中键入类似
(对于更高级的项目,您可能要考虑使用