"if"用于显示错误 tkinter.messagebox 的语句不起作用



我正在尝试创建一个函数,如果我输入的值为null,则会出现一个ttk.messagebox,通知用户必须输入正确的值。当我为prce或uprce输入空值时,它只会给我错误消息,而不会显示消息框。我试过用一个按钮来显示同一个消息框,它很有效,但现在我试着用一个";如果";声明,它不显示

这是我的样本代码

import tkinter
from tkinter import*
from tkinter import ttk, LabelFrame
import tkinter.messagebox
import sqlite3
conn = sqlite3.connect('newtest.db')
c = conn.cursor()
top = tkinter.Tk()
def update(show):
trv.delete(*trv.get_children())
for i in show:
trv.insert("", 'end', values=i)
return show

def submitprod():
database_insert()
current_db_data = query_database()
update(current_db_data)
# reset
pdesc.delete(0, END)
qty.delete(0, END)
prce.delete(0, END)
uprce.delete(0, END)
conn.commit()

def query_database():
query = "SELECT oid, pdesc, qty, prce, uprce, markup from products"
conn = sqlite3.connect('newtest.db')
c = conn.cursor()
c.execute(query)
show = c.fetchall()
return show
def error():
tkinter.messagebox.showinfo("Error! Enter Value!")
def database_insert():
conn = sqlite3.connect('newtest.db')
c = conn.cursor()
if prce.get == None:
error()
if uprce.get == None:
error()
pprce1 = int(prce.get())
uprce1 = int(uprce.get())
mup = (pprce1 - uprce1 / uprce1 * 100)
print(mup)
c.execute("INSERT INTO products VALUES (:pdesc, :qty, :prce, :uprce, :mup1)",{
'pdesc': pdesc.get(),
'qty': qty.get(),
'prce': prce.get(),
'uprce': uprce.get(),
'mup1': mup})
conn.commit()
box2 = LabelFrame(top)
box2.pack (fill="both", expand="yes", padx=20, pady=10)
box1 = LabelFrame(top, text="Entry")
box1.pack (fill="both", expand="yes", padx=20, pady=10)
pdesc = Entry(box1, width=30)
pdesc.grid(row=1, column=1, padx=20)
qty = Entry(box1, width=30)
qty.grid(row=2, column=1, padx=20)
prce = Entry(box1, width=30)
prce.grid(row=3, column=1, padx=20)
uprce = Entry(box1, width=30)
uprce.grid(row=4, column=1, padx=20)
pdesc_label = Label(box1, text='Product')
pdesc_label.grid(row=1, column=2)
qty_label = Label(box1, text='Quantity')
qty_label.grid(row=2, column=2)
prce_label = Label(box1, text='Price')
prce_label.grid(row=3, column=2)
uprce_label = Label(box1, text='Unit Price')
uprce_label.grid(row=4, column=2)
trv = ttk.Treeview(box2, column=(1,2,3,4,5,6), show="headings", height="20")
style=ttk.Style(trv)
style.configure('Treeview', rowheight=20)
trv.pack(side=LEFT)
trv.heading(1, text="Product ID")
trv.heading(2, text="Product Description")
trv.heading(3, text="Quantity")
trv.heading(4, text="Price")
trv.heading(5, text="Unit Price")
trv.heading(6, text="Return Percentage")
#data for products//show1
conn = sqlite3.connect('new1.db')
query = "SELECT oid, pdesc, qty, prce, uprce, markup from products"
c.execute(query)
show = c.fetchall()
update(show)
#submit product button
btn2 = ttk.Button(box1, text='Enter', command=submitprod)
btn2.grid(row=6, column=1, columnspan=1, pady=10, padx=10, ipadx=10)
top.title("error test")
top.geometry("1500x1200")
top.mainloop()

这是我得到的错误信息

line 22, in submitprod
database_insert()
line 54, in database_insert
pprce1 = int(prce.get())
ValueError: invalid literal for int() with base 10: ''

您有几个错误。1( 你需要在get()方法的末尾添加((2(你需要与空字符串""进行比较,而不是None3(如果发生错误,你需要添加return来停止函数,4(你需要为错误弹出窗口提供标题。

if prce.get() == "":
error()
return

也就是说,使用try块而不是if块会更像蟒蛇,比如

def error():
tkinter.messagebox.showinfo("Error", "Invalid Value!")
def database_insert():
try:
pprce1 = int(prce.get())
uprce1 = int(uprce.get())
except ValueError:
error()
return # stop this function
mup = (pprce1 - uprce1 / uprce1 * 100)
print(mup)
conn = sqlite3.connect('newtest.db')
c = conn.cursor()
c.execute("INSERT INTO products VALUES (:pdesc, :qty, :prce, :uprce, :mup1)",{
'pdesc': pdesc.get(),
'qty': qty.get(),
'prce': prce.get(),
'uprce': uprce.get(),
'mup1': mup})
conn.commit()

如果用户键入";香蕉;或者什么的。

相关内容

  • 没有找到相关文章

最新更新