简体   繁体   English

我想在python中显示输入错误消息,但似乎不起作用

[英]I want to show an entry error message in python but it doesnt seem to work

here is the code which I want to demonstrate an error message when the entry is less than 1000 , everything just looks fine but when I run this I get the error I mentioned at the bottom, here is all you need to see 这是我要在条目小于1000时显示错误消息的代码,一切看起来都很好,但是当我运行此代码时,我得到了我在底部提到的错误,这是您需要查看的所有内容

    import tkinter as tk
    from tkinter import ttk
    from tkinter.constants import *
    from tkinter import messagebox

    root=tk.Tk()
    root.title("Date converter")

    gy=tk.IntVar()
    jy=tk.IntVar()


    def cj():
        if jy.get() <1000 :
            messagebox.showerror("ERROR", "Entry of year must be upper than oneThousand (1000) !")
        else:   
            Answerj.configure(text=jy.get())

    def cg():
        if gy.get()<1000:
            messagebox.showerror("ERROR", "Entry of year must be upper than oneThousand (1000) !")
        else:
            Answerg.configure(text=gy.get())


    def justint(inStr,acttyp):
        if acttyp == '1': 
            if not inStr.isdigit():
                return False
        return True

    entryjy=ttk.Entry(root , width=6 , validate="key", textvariable=jy)
    entryjy['validatecommand'] = (entryjy.register(justint),'%P','%d')
    entryjy.delete(0, END)
    entryjy.grid(row=0 , column=0)

    entrygy=ttk.Entry(root , width=6 , validate="key", textvariable=gy)
    entrygy['validatecommand'] = (entrygy.register(justint),'%P','%d')
    entrygy.delete(0, END)
    entrygy.grid(row=1 , column=0)

    Answerj=ttk.Button(root,text=" ",state=DISABLED,style='my.TButton')
    Answerj.grid(row=0,column=2)

    Answerg=ttk.Button(root,text=" ",state=DISABLED,width=10,style='my.TButton')
    Answerg.grid(row=1,column=2 )

    buttong=ttk.Button(root,text="calculate",command=cg,width=10,style='my.TButton')
    buttong.grid(column=1,row=0)

    buttonj=ttk.Button(root,text="calculate",command=cj,style='my.TButton')
    buttonj.grid(column=1,row=1)


    root.mainloop()

Unfortunately, This is the error I get: 不幸的是,这是我得到的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Hadi\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 505, in get
    return self._tk.getint(value)
_tkinter.TclError: expected integer but got ""

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Hadi\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in call
    return self.func(*args)
  File "D:\python\soal_az_stack.py", line 20, in cg
    if gy.get()<1000:
  File "C:\Users\Hadi\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 507, in get
    return int(self._tk.getdouble(value))
_tkinter.TclError: expected floating-point number but got ""

If you could tell where the problem is I would be much obliged! 如果您能说出问题出在哪里,我将倍感荣幸!

You need to first validate that the field contains a valid number instead of empty sting. 您需要首先验证该字段包含有效数字而不是空字符串。

One way to deal with this is with a try/except statement that will try your function and if it fails let you know some error message. 解决此问题的一种方法是使用try/except语句,该语句将尝试您的函数,如果失败,则让您知道一些错误消息。

Change cj() and cg() to this: cj()cg()更改为此:

def cj():
    try:
        if jy.get() < 1000:
            messagebox.showerror("ERROR", "Entry of year must be upper than oneThousand (1000) !")
        else:   
            Answerj.configure(text=jy.get())
    except:
        messagebox.showerror("ERROR", "Entry field does not contain a valid number!")

def cg():
    try:
        if gy.get() < 1000:
            messagebox.showerror("ERROR", "Entry of year must be upper than oneThousand (1000) !")
        else:
            Answerg.configure(text=gy.get())
    except:
        messagebox.showerror("ERROR", "Entry field does not contain a valid number!")

暂无
暂无

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

相关问题 Python莳萝:Pickle namedtuple似乎不起作用 - Python dill: Pickle namedtuple doesnt seem to work .readlines 在 python 3 中似乎根本不起作用 - .readlines doesnt seem to work at all in python 3 我的python程序中的逻辑似乎不起作用 - the logic in my python program doesnt seem to work 转换为 python 的 Pyqt5 Ui 文件似乎不起作用 - Pyqt5 Ui file converted to python doesnt seem to work python pygame animation 可以在没有 class 的情况下工作,但是一旦我将它放入 class 中,它就不想工作了? - python pygame animation works without class but as soon as i put it into a class it doesnt want to work? 似乎无法按照我希望的方式使用“sched”模块 - Can't seem to use the 'sched' module the way I want it to work 在 python Django 中,我想对 if else 执行两条语句,如果 email 被采用,则显示消息如果员工 ID 被采用,则显示消息 - In python Django I want to execute two statements on if else that if email is taken show message if employee id is taken show message 我想做一个 discord 机器人,当会员上线时会发送一条消息,但我似乎无法让它工作? (Python) - I would like to make a discord bot that sends a message when a member goes online but I can't seem to make it work ? (python) SQLite升序似乎不适用于bisect - Sqlite ascending order doesnt seem to work with bisect QtCore.Qt.Key_ 似乎不起作用 - QtCore.Qt.Key_ doesnt seem to work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM