Python 3:Tkinter:如何将Entry.get()更改为整数 [英] Python 3: Tkinter: How to change Entry.get() into an integer

查看:1817
本文介绍了Python 3:Tkinter:如何将Entry.get()更改为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的程序,它将英制单位转换为公制单位。但是,当我使用 Entry.get()命令时,我需要将其转换为整数,当我尝试这样做时

I am coding a simple program which converts imperial units into metric units. However, when I use an Entry.get() command, I need to convert it to an integer and when I try to do that

我收到此错误:

Traceback (most recent call last):
  File "C:/Users/Bill/converter.py", line 18, in <module>
    conversion=int(e.get())
ValueError: invalid literal for int() with base 10: ''

我尝试运行我正在做的非常基本的版本(如下所示),但仍然导致同样的错误。

I tried running an extremely basic version of what I was doing (which is written below), but that still caused the same error.

import tkinter
root= tkinter.Tk()

e=tkinter.Entry(root)
e.pack()

b=tkinter.Button(root, command= lambda: print(e.get()))
b.pack()

conversion=int(e.get())
conversion= conversion* 1.8 +32
l = tkinter.Label(root, text=conversion)

top.mainloop()

我最近才重新开始编码,所以答案可能是我错过的非常简单,但感谢您的回答。

I only recently started coding again, so the answer is probably something really simple that I missed, but thanks for any answers.

推荐答案

问题以及错误说明的是空字符串''无法转换为整数。

The problem, and what the error states, is that the empty string '' cannot be converted to an integer.

事实上,很多字符串都无法转换为整数。
在你的情况下, int(e.get())引发错误,因为条目为空,但 int('') 引发错误。
因此,您需要在转换之前验证您的输入,以便仅在它包含整数的字符串表示时才处理它。

In fact, a lot of strings cannot be converted to an integer. In your case, int(e.get()) raises an error because the entry is empty, but int('') raises an error. Therefore, you need to validate your input before converting it, so as to process it only when it contains the string representation of an integer.

您可以换行a get_value 函数中尝试 - 除了

You can wrap a try-except in a get_value function:

def get_value(entryWidget):
    value = entryWidget.get()
    try:
        return int(value)
    except ValueError:
        return None

然后,而不是设置 lambda:print(e.get())作为按钮的回调,传递 lambda:print(get_value(e))
如果值可以解析为整数,则会打印 int(e.get())的结果。
如果不能,则打印

Then, instead of setting lambda: print(e.get()) as a callback to your button, pass lambda: print(get_value(e)). If the value could be parsed as an integer, this will print the result of int(e.get()). If it couldn't, this will print None.

以下是您的代码的修改版本:

Here is the modified version of your code:

import tkinter
root= tkinter.Tk()

def get_value(entryWidget):
    value = entryWidget.get()
    try:
        return int(value)
    except ValueError:
        return None


e = tkinter.Entry(root)
e.pack()

b = tkinter.Button(root, command=lambda: print(e.get()))
b.pack()

conversion = get_value(e)
if conversion is not None:
    conversion *= 1.8 + 32
l = tkinter.Label(root, text=conversion)

top.mainloop()

然而,这有点尴尬。
由于条目的内容是在主循环之前捕获的,后者将始终为空。

This, however, is a bit awkward. Since the content of the entry is caught before the main loop, the latter will always be empty.

在处理GUI时,你无法顺序思考通常这样做。
您应该在按下按钮时更新标签内容,以便显示转换结果:

When dealing with GUIs, you cannot think sequencially as you usually do. You should rather ask your button to update the content of your label when pressed, so as to have it display the result of the conversion:

import tkinter


def get_value(entryWidget):
    value = entryWidget.get()
    try:
        return int(value)
    except ValueError:
        return None

def convert(value):
    if value is None:
        return None
    else:
        return 1.8*value + 32

def set_label_text(label, entry):
    value = convert(get_value(entry))
    if value is None:
        label['text'] = "Enter an integer"
    else:
        label['text'] = value


root = tkinter.Tk()

e = tkinter.Entry(root)    
l = tkinter.Label(root, text="")
b = tkinter.Button(root, text="Convert", command=lambda: set_label_text(l, e))

e.pack()
l.pack()
b.pack()

root.mainloop()

这篇关于Python 3:Tkinter:如何将Entry.get()更改为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆