我如何从 python tkinter 条目中获取整数值?

发布于 2025-01-09 13:45:38 字数 108 浏览 2 评论 0原文

所以我知道 python 中的条目是如何工作的,并且我在一些项目中对其进行了编码。但在一个项目中,需要从用户输入的数字(number)值。但它给出了条目仅支持字符串数据的错误。你能解决这个问题吗?谢谢。

so I know how entry in python works and i coded it in some projects. but in one project , there was need to get numeric ( number ) value from user in entry. but it gives the error that entry only supports string data. can you solve this problem ? thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天煞孤星 2025-01-16 13:45:38

您只需将其类型转换为整数即可。例如。这段代码对我有用:

from tkinter import *

win=Tk()

win.geometry("700x350")

def cal_sum():
   t1=int(a.get()) # Casting to integer
   t2=int(b.get()) # Casting to integer
   sum=t1+t2
   label.config(text=sum)

Label(win, text="Enter First Number", font=('Calibri 10')).pack()
a=Entry(win, width=35)
a.pack()
Label(win, text="Enter Second Number", font=('Calibri 10')).pack()
b=Entry(win, width=35)
b.pack()

label=Label(win, text="Total Sum : ", font=('Calibri 15'))
label.pack(pady=20)
Button(win, text="Calculate Sum", command=cal_sum).pack()

win.mainloop()

You just need to typecast it into an integer. Eg. This code worked for me:

from tkinter import *

win=Tk()

win.geometry("700x350")

def cal_sum():
   t1=int(a.get()) # Casting to integer
   t2=int(b.get()) # Casting to integer
   sum=t1+t2
   label.config(text=sum)

Label(win, text="Enter First Number", font=('Calibri 10')).pack()
a=Entry(win, width=35)
a.pack()
Label(win, text="Enter Second Number", font=('Calibri 10')).pack()
b=Entry(win, width=35)
b.pack()

label=Label(win, text="Total Sum : ", font=('Calibri 15'))
label.pack(pady=20)
Button(win, text="Calculate Sum", command=cal_sum).pack()

win.mainloop()

等待圉鍢 2025-01-16 13:45:38

除此之外,您可以使用验证来确保您只能在条目中写入计算结果为整数的值:

import tkinter as tk

root = tk.Tk()
root.geometry("200x100")

# Function to validate the Entry text
def validate(entry_value_if_allowed, action):
    if int(action) == 0: # User tried to delete a character/s, allow
        return True
    try:
        int(entry_value_if_allowed) # Entry input will be an integer, allow
        return True
    except ValueError: # Entry input won't be an integer, don't allow
        return False

# Registering the validation function, passing the correct callback substitution codes
foo = (root.register(validate), '%P', '%d')
my_entry = tk.Entry(root, width=20, validate="key", validatecommand=foo)

# Misc
text = tk.Label(root, text="This Entry ^ will only allow integers")
my_entry.pack()
text.pack()
root.mainloop()

可以找到有关条目验证的更多信息此处

To add further to this, you can use validation to ensure that you can only write values that evaluate to an integer in your entry:

import tkinter as tk

root = tk.Tk()
root.geometry("200x100")

# Function to validate the Entry text
def validate(entry_value_if_allowed, action):
    if int(action) == 0: # User tried to delete a character/s, allow
        return True
    try:
        int(entry_value_if_allowed) # Entry input will be an integer, allow
        return True
    except ValueError: # Entry input won't be an integer, don't allow
        return False

# Registering the validation function, passing the correct callback substitution codes
foo = (root.register(validate), '%P', '%d')
my_entry = tk.Entry(root, width=20, validate="key", validatecommand=foo)

# Misc
text = tk.Label(root, text="This Entry ^ will only allow integers")
my_entry.pack()
text.pack()
root.mainloop()

Some more info on Entry validation can be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文