tkinter 条目小部件和变量 [英] tkinter Entry widget and variables

查看:24
本文介绍了tkinter 条目小部件和变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处于一种无法弄清楚自己的情况.

Im sitting in a situation i can't figure out myself.

当我使用 Entry 小部件进行用户交互时,我很难找到验证数据的正确方法.

When im using the Entry widget to get user interaction i am having a hard time finding the right way to validate the data.

情况:

我有两个 Entry 小部件,用户必须在其中输入两个必须是浮点数的变量.

I have two Entry widgets in which the user must enter two variables which has to be floats.

虽然我可以运行一个只有在输入的值是浮点数时才能正常工作的程序,但如果我将它留空或输入一个字母会关闭 - 因此我想验证输入是一个浮点数:

While i can run a program that only works properly if the entered value is a float, if i then leave it blank or enter a letter shuts down - therefor i want to validate the entry to be a float:

variableentry = Entry(root and so on)
variableentry.grid()

我正在使用:

variablename = float(variableentry.get()) 

当我:

print(type(variablename) 

我收到消息:

<class 'float'> 

因此我无法使用

#...
try:
    if(variablename is not float):
    messagebox.showerror("Error", "Incorrect parameter")
    return

这显然不起作用,因为变量名属于float"类而不是 float,我尝试了不同的方式在 if 语句中输入而不是 float - 没有任何运气.

This obviously isnt working since the variablename is of class 'float' and not float, i have tried different ways of entering instead of float in the if statement - without any luck.

有什么想法吗?

提前,谢谢!

最好的问候,

卡斯帕

我找到了:

from Tkinter import *

class ValidatingEntry(Entry):
    # base class for validating entry widgets

    def __init__(self, master, value="", **kw):
        apply(Entry.__init__, (self, master), kw)
        self.__value = value
        self.__variable = StringVar()
        self.__variable.set(value)
        self.__variable.trace("w", self.__callback)
        self.config(textvariable=self.__variable)

    def __callback(self, *dummy):
        value = self.__variable.get()
        newvalue = self.validate(value)
        if newvalue is None:
            self.__variable.set(self.__value)
        elif newvalue != value:
            self.__value = newvalue
            self.__variable.set(self.newvalue)
        else:
            self.__value = value

    def validate(self, value):
        # override: return value, new value, or None if invalid
        return value

来自 http://effbot.org/zone/tkinter-entry-validate.htm

然而,其余的代码不是在课堂上编写的(我知道这不是最优的,但这是老师要求的)这会影响上面的例子吗?我如何使它适合我的需求?

However the rest of the code is not written in classes (i know this is not optimal but it is demanded by the teacher) will that effect the above example? And how would i make it fit my needs?

推荐答案

你要做的是尝试将输入框的内容转换为浮点数,如果不能转换则报错.执行 variablename = float(variableentry.get()) 很好,但是如果给定的字符串无法转换,要捕获 float 引发的错误,您必须换行在 try 块中,并捕获由 float 引发的 ValueError.如果没有异常,就可以继续代码了:

What you want to do is to try converting the contents of the entry box to a float, and report an error message if the conversion is not possible. Doing variablename = float(variableentry.get()) is fine, but to catch errors raised by float if the string it is given cannot be converted, you must wrap the line in a try block, and catch the ValueError raised by float. If there is no exception, you can proceed with the code:

try:
    variablename = float(variableentry.get())
except ValueError:
    # error messagebox, etc
else:
    # do stuff with variablename

这篇关于tkinter 条目小部件和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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