如何解决Python tkinter中的这个msg.showerror问题?

huangapple go评论66阅读模式
英文:

How to solve this msg.showerror problem in python tkinter?

问题

I want to make an error message if the input of an entry is defined IntVar and we input not an IntVar such as string to the entry box.

here is the code that I made:

nobad_sign = tk.IntVar()
nohp_sign = tk.IntVar()

nobad_entry_sign =  tk.Entry(frm2, textvariable=nobad_sign, width=20, 
                            font=('verdana', 13))
nobad_entry_sign.pack(pady=(10,20), anchor='w')
nohp_entry_sign =  tk.Entry(frm2, textvariable=nohp_sign, width=20, 
                            font=('verdana', 13))
nohp_entry_sign.pack(pady=(10,20), anchor='w')

def submit():
        if nobad_sign.get() == "" or noph_sign.get() == "":
            msg.showerror('Visitor Data', 'please input with number')

submit_btn = tk.Button(frm2, text='Submit', bg='gray25', fg='alice blue',
                            font=('Britannic Bold', 12), width=20, relief=tk.GROOVE, 
                            borderwidth=4, command=submit)
submit_btn.pack(pady=(10,10),  anchor='w')

and the error syntax that comes out is:

Traceback (most recent call last):
  File "C:\Users\HP-PC\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 570, in get
    return self._tk.getint(value)
_tkinter.TclError: expected integer but got ""

Can you help me how to solve this?

I want to make that error message pops up when the entry is just blank or inputted stringVar.

英文:

I want to make an error message if the input of an entry is defined IntVar and we input not an IntVar such as string to the entry box.

here is the code that I made:

nobad_sign = tk.IntVar()
nohp_sign = tk.IntVar()

nobad_entry_sign =  tk.Entry(frm2, textvariable=nobad_sign,width= 20, 
                            font=('verdana',13))
nobad_entry_sign.pack(pady=(10,20), anchor='w')
nohp_entry_sign =  tk.Entry(frm2, textvariable=nohp_sign,width= 20, 
                            font=('verdana',13))
nohp_entry_sign.pack(pady=(10,20), anchor='w')

def submit():
        if nobad_sign.get() == "" or noph_sign.get() == "":
            msg.showerror('Visitor Data', 'please input with number')

submit_btn = tk.Button(frm2, text='Submit', bg='gray25', fg='alice blue',
                            font=('Britannic Bold',12), width=20, relief=tk.GROOVE, 
                            borderwidth=4, command=submit)
submit_btn.pack(pady=(10,10),  anchor='w')

and the error syntax that comes out is:

Traceback (most recent call last):
  File "C:\Users\HP-PC\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 570, in get
    return self._tk.getint(value)
_tkinter.TclError: expected integer but got ""

Can you help me how to solve this?

I want to make that error message pops up when the entry is just blank "" or inputed stringVar

答案1

得分: 1

如评论中提到的,您可以在输入小部件的textvariables中使用StringVar,而不是IntVar。您可以将它们的初始值设置为0。然后在您的submit函数中,您可以使用字符串方法来确保输入是一个数字。

例如:

from tkinter import *
import tkinter as tk
import tkinter.messagebox as msg

frm2 = Tk()

nobad_sign = tk.StringVar()
noph_sign = tk.StringVar()
nobad_sign.set("0")
noph_sign.set("0")

nobad_entry_sign = tk.Entry(frm2, textvariable=nobad_sign, width=20, font=('verdana', 13))
nobad_entry_sign.pack(pady=(10, 20), anchor='w')
nohp_entry_sign = tk.Entry(frm2, textvariable=noph_sign, width=20, font=('verdana', 13))
nohp_entry_sign.pack(pady=(10, 20), anchor='w')

def submit():
    values = [nobad_sign.get(), noph_sign.get()]
    if not all(val.isdigit() for val in values):
        msg.showerror('Visitor Data', 'please input with number')

submit_btn = tk.Button(frm2, text='Submit', bg='gray25', fg='alice blue',
                      font=('Britannic Bold', 12), width=20, relief=tk.GROOVE,
                      borderwidth=4, command=submit)
submit_btn.pack(pady=(10, 10), anchor='w')

frm2.mainloop()
英文:

As mentioned in the comments, you can use a StringVar instead of an IntVar for each of the textvariables of the Entry widgets. You can set their initial values to 0. Then in your submit function instead of just checking for an empty string "", you can instead use the string methods to ensure that the input is a number.

For example:

from tkinter import *
import tkinter as tk
import tkinter.messagebox as msg

frm2 = Tk()

nobad_sign = tk.StringVar()
noph_sign = tk.StringVar()
nobad_sign.set("0")
noph_sign.set("0")


nobad_entry_sign =  tk.Entry(frm2, textvariable=nobad_sign,width= 20,
                            font=('verdana',13))
nobad_entry_sign.pack(pady=(10,20), anchor='w')
nohp_entry_sign =  tk.Entry(frm2, textvariable=noph_sign,width= 20,
                            font=('verdana',13))
nohp_entry_sign.pack(pady=(10,20), anchor='w')

def submit():
    values = [nobad_sign.get(), noph_sign.get()]
    if not all(val.isdigit() for val in values):
        msg.showerror('Visitor Data', 'please input with number')

submit_btn = tk.Button(frm2, text='Submit', bg='gray25', fg='alice blue',
                            font=('Britannic Bold',12), width=20, relief=tk.GROOVE,
                            borderwidth=4, command=submit)
submit_btn.pack(pady=(10,10),  anchor='w')

frm2.mainloop()

huangapple
  • 本文由 发表于 2023年4月11日 13:17:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定