python entry get_Python 3:Tkinter:如何将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

I get this error:

Traceback (most recent call last):

File "C:/Users/Bill/converter.py", line 18, in

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.

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.

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

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.

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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值