简体   繁体   English

如何在 Tkinter python 中将字符串转换为整数

[英]How to cast string to integer in Tkinter python

How to cast string from the entry box to integer in Tkinter.如何在 Tkinter 中将输入框中的字符串转换为整数。 I've tried this but it doesn't work.我试过这个,但它不起作用。

import tkinter 
from tkinter import tt

age= tkinter.Label(window, text = "How old are you?")
age.grid(row = 1, column = 0)

entry_age = tkinter.Entry(window)
Entry.grid(row = 1, column = 1)

height = tkinter.Label(window, text = "What is your height(cm): ")
height.grid(row = 2, column = 0)

entry_height = tkinter.Entry(window)
entry_height.grid(row = 2, column = 1)

weight = tkinter.Label(window, text = "your weight (kg): ")
weight.grid(row = 3, column = 0)

entry_weight = tkinter.Entry(window)
entry_weight.grid(row = 3, column = 1) 

entry_age1 = entry_age.get()

entry_age1 = int(entry_age1)


entry_heigh1t = entry_height.get()

entry_height1 = int(entry_height1)

entry_weight1 = entry_weight.get()

entry_weight1 = int(entry_weight1)

Here .get() function always return anything as a string.这里 .get() 函数总是以字符串形式返回任何内容。 You have to convert that into str to int.您必须将其转换为 str 到 int。

Here is the quick example.这是快速示例。

import tkinter as tk

win = tk.Tk()

def get_number():
    text = ent.get()
    try:
        num = int(text)    # <= Focus here
        print(num)
    except:
        print("This is not number")

ent = tk.Entry(win)
ent.grid(row=0, column=0, padx=10, pady=10)

btn = tk.Button(win, text="Get Number", command=get_number)
btn.grid(row=1, column=0)

win.mainloop()

Please post the error message you're getting along with some test cases as input for the Entry boxes.请张贴您遇到的错误消息以及一些测试用例作为输入框的输入。

entry_age.get() returns a string that can be converted into an integer using int() if the input is a number of course. entry_age.get() 返回一个字符串,如果输入当然是数字,则可以使用 int() 将其转换为整数。

Edit after @Milena Pavlovic replied with the error:在@Milena Pavlovic 回复错误后编辑:

The program won't run because as soon as it runs the statement entry_age1 = entry_age.get() which stores the empty string into the variable, the next statement that follows tries to convert this empty string into an integer.该程序将不会运行,因为一旦它运行将空字符串存储到变量中的语句entry_age1 = entry_age.get() ,接下来的语句就会尝试将此空字符串转换为整数。 Empty strings go by "" which cannot be converted to integer type.空字符串以 "" 开头,无法转换为整数类型。 Since this is invalid, it raises the value error.由于这是无效的,它会引发值错误。 To resolve this, you can make the following changes:要解决此问题,您可以进行以下更改:

if entry_age.get().isdecimal():
    entry_age1 = int(entry_age1)

if entry_height.get().isdecimal():
    entry_height1 = int(entry_height1)

if entry_weight.get().isdecimal():
    entry_weight1 = int(entry_weight1)

The isdecimal() function checks if the string returned by get() is a decimal and only then will it get converted into an integer and stored into the respective variables and allow you to do what you want with them. isdecimal() 函数检查 get() 返回的字符串是否为小数,然后才将其转换为整数并存储到相应的变量中,并允许您对它们执行所需的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM