Python 错误弹出窗口不起作用
Python error pop up not working
编写代码时,我不知何故弄坏了它,不记得以前的工作原理,只使用了 python 几个星期,在字面意思上工作了一整天之后,我有点沮丧...
# A program to calculate delivery order totals
from graphics import *
#Error Window
def errorWindow():
errwin = GraphWin("Error", 200, 200)
errwin.setCoords(0.0, 0.0, 4.0, 4.0)
errwin.setBackground(color_rgb(33,158,87))
Text(Point(2, 3), "This is not a valid order").draw(errwin)
Text(Point(2, 2.5), "Please try again").draw(errwin)
outline = Rectangle(Point(1.5,1), Point(2.5,2))
outline.setFill('white')
outline.draw(errwin)
okbutton = Text(Point(2,1.5),"OK")
okbutton.draw(errwin)
done=errwin.getMouse()
if (done.getX() > 1.5 and done.getX() < 2.5):
errwin.close()
def main():
win = GraphWin("Brandi's Bagel House", 500, 500)
win.setCoords(0.0, 0.0, 10.0, 10.0)
win.setBackground(color_rgb(33,158,87))
#Banner
banner = Rectangle(Point(0, 8), Point(10, 10))
banner.setFill(color_rgb(97,76,26))
banner.setOutline(color_rgb(97,76,26))
banner.draw(win)
#Logo Image
circ = Circle(Point(1,9), .65)
circ.setFill('white')
circ.setOutline('white')
circ.draw(win)
circ2 = Circle(Point(1,9), .6)
circ2.setFill(color_rgb(33,158,87))
circ2.setOutline(color_rgb(33,158,87))
circ2.draw(win)
bigb = Text(Point(1, 9),"B")
bigb.setStyle('bold italic')
bigb.setTextColor('white')
bigb.setFace('courier')
bigb.setSize(28)
bigb.draw(win)
#Horizontal Lines
Line(Point(0,2.25), Point(10,2.25)).draw(win)
Line(Point(0,4.25), Point(10,4.25)).draw(win)
Line(Point(0,6.25), Point(10,6.25)).draw(win)
#Text
title = Text(Point(4,9), "Delivery Orders:")
title.setSize(20)
title.setTextColor('white')
title.draw(win)
Text(Point(1.5,7), "Bagels:").draw(win)
whitetext = Text(Point(3,7.5), "White")
whitetext.setSize(10)
whitetext.draw(win)
wheattext = Text(Point(5,7.5), "Wheat")
wheattext.setSize(10)
wheattext.draw(win)
Text(Point(1.5,5), "Toppings:").draw(win)
creamtext = Text(Point(3,5.8), "Cream")
creamtext.setSize(10)
creamtext.draw(win)
cheesetext = Text(Point(3,5.5), "Cheese")
cheesetext.setSize(10)
cheesetext.draw(win)
buttertext = Text(Point(5,5.5), "Butter")
buttertext.setSize(10)
buttertext.draw(win)
jellytext = Text(Point(7,5.5), "Jelly")
jellytext.setSize(10)
jellytext.draw(win)
Text(Point(1.5,3), "Coffee:").draw(win)
regtext = Text(Point(3,3.5), "Regular")
regtext.setSize(10)
regtext.draw(win)
decaftext = Text(Point(5,3.5), "Decaf")
decaftext.setSize(10)
decaftext.draw(win)
Text(Point(7,1.5), "Subtotal:").draw(win)
Text(Point(7,1), "Tax:").draw(win)
Text(Point(7,0.5), "Total:").draw(win)
#input bagel
bagel1 = Entry(Point(3,7), 3)
bagel1.setText("0")
bagel1.draw(win)
bagel2 = Entry(Point(5,7), 3)
bagel2.setText("0")
bagel2.draw(win)
#input topping
top1 = Entry(Point(3,5), 3)
top1.setText("0")
top1.draw(win)
top2 = Entry(Point(5,5), 3)
top2.setText("0")
top2.draw(win)
top3 = Entry(Point(7,5), 3)
top3.setText("0")
top3.draw(win)
#input coffee
coff1 = Entry(Point(3,3), 3)
coff1.setText("0")
coff1.draw(win)
coff2 = Entry(Point(5,3), 3)
coff2.setText("0")
coff2.draw(win)
#Calculate Button
outer1 = Rectangle(Point(4.2,0.6), Point(5.8,1.4))
outer1.setFill('white')
outer1.draw(win)
button = Text(Point(5,1),"Calculate")
button.draw(win)
#Quit Buttons
outer2 = Rectangle(Point(9,9.2), Point(9.95,9.95))
outer2.setFill('white')
outer2.draw(win)
button = Text(Point(9.5,9.6),"Quit")
button.draw(win)
#Output
subtotaloutput = Text(Point(9,1.5), "[=11=].00")
subtotaloutput.draw(win)
taxoutput = Text(Point(9,1), "[=11=].00")
taxoutput.draw(win)
totaloutput = Text(Point(9,0.5), "[=11=].00")
totaloutput.draw(win)
#calculations
whitebagel = eval(bagel1.getText())
whitetotal = whitebagel* 1.25
wheatbagel = eval(bagel2.getText())
wheattotal = wheatbagel*1.50
creamcheese = eval(top1.getText())
creamtotal = creamcheese* 0.50
butter = eval(top2.getText())
buttertotal = butter*0.25
jelly = eval(top3.getText())
jellytotal = jelly*0.75
regularcoff = eval(coff1.getText())
regulartotal = regularcoff*1.25
decafcoff = eval(coff2.getText())
decaftotal = decafcoff*1.25
click = win.getMouse()
if (click.getX() > 4.2 and click.getX() < 5.8 and (whitebagel >0 or wheatbagel >0)):
subtotal = whitetotal+wheattotal+creamtotal+buttertotal+jellytotal+regulartotal+decaftotal
tax = .06
taxes = subtotal*tax
total = taxes+subtotal
subtotal = "$%0.2f" % (subtotal)
taxes = "$%0.2f" % (taxes)
total = "$%0.2f" % (total)
subtotaloutput.setText(subtotal)
taxoutput.setText(taxes)
totaloutput.setText(total)
#Coffee Cup Image
bottomoval = Oval(Point(0.7,0.4), Point(1.4, 0.7))
bottomoval.setFill(color_rgb(97,76,26))
bottomoval.setOutline(color_rgb(97,76,26))
bottomoval.draw(win)
cup = Rectangle(Point(0.7,0.5), Point(1.4,1.3))
cup.setFill(color_rgb(97,76,26))
cup.setOutline(color_rgb(97,76,26))
cup.draw(win)
topoval = Oval(Point(0.6,1.2), Point(1.5, 1.5))
topoval.setFill('white')
topoval.setOutline(color_rgb(97,76,26))
topoval.draw(win)
coffee = Oval(Point(0.7,1.26), Point(1.4, 1.45))
coffee.setFill(color_rgb(71,54,14))
coffee.setOutline(color_rgb(71,54,14))
coffee.draw(win)
tinycirc = Circle(Point(1.05,0.8), .3)
tinycirc.setFill(color_rgb(33,158,87))
tinycirc.setOutline('white')
tinycirc.draw(win)
letter = Text(Point(1.05,0.8),"B")
letter.setStyle('bold italic')
letter.setTextColor('white')
letter.setFace('courier')
letter.setSize(17)
letter.draw(win)
#Bagel
outerbagel = Oval(Point(1.8, 0.4), Point(2.7, 1))
outerbagel.setFill(color_rgb(191,157,61))
outerbagel.draw(win)
shadow = Oval(Point(2.1, 0.7), Point(2.4, 0.8))
shadow.setFill(color_rgb(130,102,26))
shadow.draw(win)
elif(click.getX() > 4.2 and click.getX() < 5.8 and whitebagel <1 and wheatbagel <1):
errorWindow()
#End
point = win.getMouse()
if (point.getX() > 9 and point.getX() < 9.95):
win.close()
main()
如果未在订单中选择百吉饼,我需要弹出一个错误 window,因为在这个例子中,如果没有百吉饼,咖啡和配料将无法配送。我想我以前在 try/except 中有这个,但现在我也无法让它工作。要么它不会计算总数并弹出,要么它计算但仍然弹出错误,或者没有任何反应!我似乎也无法在关闭弹出窗口后找到任何方向,返回到正常运行的主要 window。当我关闭错误 window 时,主要的 window 不再有效。我希望能够做到这一点。最后我不能使用 tkinter,只能使用 graphics.py。如果有任何粗暴的行为,我也深表歉意,到目前为止,这是我所知道的唯一方法。在此先感谢您的任何建议。
我仍然希望有人会可怜我,并给我一些指导,告诉我这段代码哪里出了问题...我已经更改了我遇到问题的部分,但它仍然无法正常工作:
try:
click = win.getMouse()
if click.getX() > 4.2 and click.getX() < 5.8 and whitebagel >0 and wheatbagel >0:
subtotal = whitetotal+wheattotal+creamtotal+buttertotal+jellytotal+regulartotal+decaftotal
tax = .06
taxes = subtotal*tax
total = taxes+subtotal
subtotal = "$%0.2f" % (subtotal)
taxes = "$%0.2f" % (taxes)
total = "$%0.2f" % (total)
subtotaloutput.setText(subtotal)
taxoutput.setText(taxes)
totaloutput.setText(total)
#Coffee Cup Image
bottomoval = Oval(Point(0.7,0.4), Point(1.4, 0.7))
bottomoval.setFill(color_rgb(97,76,26))
bottomoval.setOutline(color_rgb(97,76,26))
bottomoval.draw(win)
cup = Rectangle(Point(0.7,0.5), Point(1.4,1.3))
cup.setFill(color_rgb(97,76,26))
cup.setOutline(color_rgb(97,76,26))
cup.draw(win)
topoval = Oval(Point(0.6,1.2), Point(1.5, 1.5))
topoval.setFill('white')
topoval.setOutline(color_rgb(97,76,26))
topoval.draw(win)
coffee = Oval(Point(0.7,1.26), Point(1.4, 1.45))
coffee.setFill(color_rgb(71,54,14))
coffee.setOutline(color_rgb(71,54,14))
coffee.draw(win)
tinycirc = Circle(Point(1.05,0.8), .3)
tinycirc.setFill(color_rgb(33,158,87))
tinycirc.setOutline('white')
tinycirc.draw(win)
letter = Text(Point(1.05,0.8),"B")
letter.setStyle('bold italic')
letter.setTextColor('white')
letter.setFace('courier')
letter.setSize(17)
letter.draw(win)
#Bagel
outerbagel = Oval(Point(1.8, 0.4), Point(2.7, 1))
outerbagel.setFill(color_rgb(191,157,61))
outerbagel.draw(win)
shadow = Oval(Point(2.1, 0.7), Point(2.4, 0.8))
shadow.setFill(color_rgb(130,102,26))
shadow.draw(win)
except:
errorWindow()
我想我已经失去理智了,因为我一直在努力让它发挥作用……
首先,tkinter 不是处理图形的方式。它有点陈旧,其中的许多概念将无法移植。再一次,如果没有其他选择,请使用它...
其次,ErrorWindow 的代码缩进不正确。当它出现时,我至少会收到一个错误弹出窗口。可能不在正确的错误条件下。
第三,我还会对点击的 Y 坐标进行某种检查,因为您可以通过点击其垂直列中的任意位置来触发计算按钮,例如小麦文本框。
通常在 GUI 中会有某种事件循环来处理多次点击。类似于:
while(True):
click = win.getMouse()
if calculate button:
get text values
if not bagels:
errorWindow()
continue
calculate
elif quit button:
break
我做了一个修改版本,它有一个以这种方式构造的事件循环,我让它给出了多个总计,并且在我没有百吉饼订单然后继续前进时提出了一个错误。
编写代码时,我不知何故弄坏了它,不记得以前的工作原理,只使用了 python 几个星期,在字面意思上工作了一整天之后,我有点沮丧...
# A program to calculate delivery order totals
from graphics import *
#Error Window
def errorWindow():
errwin = GraphWin("Error", 200, 200)
errwin.setCoords(0.0, 0.0, 4.0, 4.0)
errwin.setBackground(color_rgb(33,158,87))
Text(Point(2, 3), "This is not a valid order").draw(errwin)
Text(Point(2, 2.5), "Please try again").draw(errwin)
outline = Rectangle(Point(1.5,1), Point(2.5,2))
outline.setFill('white')
outline.draw(errwin)
okbutton = Text(Point(2,1.5),"OK")
okbutton.draw(errwin)
done=errwin.getMouse()
if (done.getX() > 1.5 and done.getX() < 2.5):
errwin.close()
def main():
win = GraphWin("Brandi's Bagel House", 500, 500)
win.setCoords(0.0, 0.0, 10.0, 10.0)
win.setBackground(color_rgb(33,158,87))
#Banner
banner = Rectangle(Point(0, 8), Point(10, 10))
banner.setFill(color_rgb(97,76,26))
banner.setOutline(color_rgb(97,76,26))
banner.draw(win)
#Logo Image
circ = Circle(Point(1,9), .65)
circ.setFill('white')
circ.setOutline('white')
circ.draw(win)
circ2 = Circle(Point(1,9), .6)
circ2.setFill(color_rgb(33,158,87))
circ2.setOutline(color_rgb(33,158,87))
circ2.draw(win)
bigb = Text(Point(1, 9),"B")
bigb.setStyle('bold italic')
bigb.setTextColor('white')
bigb.setFace('courier')
bigb.setSize(28)
bigb.draw(win)
#Horizontal Lines
Line(Point(0,2.25), Point(10,2.25)).draw(win)
Line(Point(0,4.25), Point(10,4.25)).draw(win)
Line(Point(0,6.25), Point(10,6.25)).draw(win)
#Text
title = Text(Point(4,9), "Delivery Orders:")
title.setSize(20)
title.setTextColor('white')
title.draw(win)
Text(Point(1.5,7), "Bagels:").draw(win)
whitetext = Text(Point(3,7.5), "White")
whitetext.setSize(10)
whitetext.draw(win)
wheattext = Text(Point(5,7.5), "Wheat")
wheattext.setSize(10)
wheattext.draw(win)
Text(Point(1.5,5), "Toppings:").draw(win)
creamtext = Text(Point(3,5.8), "Cream")
creamtext.setSize(10)
creamtext.draw(win)
cheesetext = Text(Point(3,5.5), "Cheese")
cheesetext.setSize(10)
cheesetext.draw(win)
buttertext = Text(Point(5,5.5), "Butter")
buttertext.setSize(10)
buttertext.draw(win)
jellytext = Text(Point(7,5.5), "Jelly")
jellytext.setSize(10)
jellytext.draw(win)
Text(Point(1.5,3), "Coffee:").draw(win)
regtext = Text(Point(3,3.5), "Regular")
regtext.setSize(10)
regtext.draw(win)
decaftext = Text(Point(5,3.5), "Decaf")
decaftext.setSize(10)
decaftext.draw(win)
Text(Point(7,1.5), "Subtotal:").draw(win)
Text(Point(7,1), "Tax:").draw(win)
Text(Point(7,0.5), "Total:").draw(win)
#input bagel
bagel1 = Entry(Point(3,7), 3)
bagel1.setText("0")
bagel1.draw(win)
bagel2 = Entry(Point(5,7), 3)
bagel2.setText("0")
bagel2.draw(win)
#input topping
top1 = Entry(Point(3,5), 3)
top1.setText("0")
top1.draw(win)
top2 = Entry(Point(5,5), 3)
top2.setText("0")
top2.draw(win)
top3 = Entry(Point(7,5), 3)
top3.setText("0")
top3.draw(win)
#input coffee
coff1 = Entry(Point(3,3), 3)
coff1.setText("0")
coff1.draw(win)
coff2 = Entry(Point(5,3), 3)
coff2.setText("0")
coff2.draw(win)
#Calculate Button
outer1 = Rectangle(Point(4.2,0.6), Point(5.8,1.4))
outer1.setFill('white')
outer1.draw(win)
button = Text(Point(5,1),"Calculate")
button.draw(win)
#Quit Buttons
outer2 = Rectangle(Point(9,9.2), Point(9.95,9.95))
outer2.setFill('white')
outer2.draw(win)
button = Text(Point(9.5,9.6),"Quit")
button.draw(win)
#Output
subtotaloutput = Text(Point(9,1.5), "[=11=].00")
subtotaloutput.draw(win)
taxoutput = Text(Point(9,1), "[=11=].00")
taxoutput.draw(win)
totaloutput = Text(Point(9,0.5), "[=11=].00")
totaloutput.draw(win)
#calculations
whitebagel = eval(bagel1.getText())
whitetotal = whitebagel* 1.25
wheatbagel = eval(bagel2.getText())
wheattotal = wheatbagel*1.50
creamcheese = eval(top1.getText())
creamtotal = creamcheese* 0.50
butter = eval(top2.getText())
buttertotal = butter*0.25
jelly = eval(top3.getText())
jellytotal = jelly*0.75
regularcoff = eval(coff1.getText())
regulartotal = regularcoff*1.25
decafcoff = eval(coff2.getText())
decaftotal = decafcoff*1.25
click = win.getMouse()
if (click.getX() > 4.2 and click.getX() < 5.8 and (whitebagel >0 or wheatbagel >0)):
subtotal = whitetotal+wheattotal+creamtotal+buttertotal+jellytotal+regulartotal+decaftotal
tax = .06
taxes = subtotal*tax
total = taxes+subtotal
subtotal = "$%0.2f" % (subtotal)
taxes = "$%0.2f" % (taxes)
total = "$%0.2f" % (total)
subtotaloutput.setText(subtotal)
taxoutput.setText(taxes)
totaloutput.setText(total)
#Coffee Cup Image
bottomoval = Oval(Point(0.7,0.4), Point(1.4, 0.7))
bottomoval.setFill(color_rgb(97,76,26))
bottomoval.setOutline(color_rgb(97,76,26))
bottomoval.draw(win)
cup = Rectangle(Point(0.7,0.5), Point(1.4,1.3))
cup.setFill(color_rgb(97,76,26))
cup.setOutline(color_rgb(97,76,26))
cup.draw(win)
topoval = Oval(Point(0.6,1.2), Point(1.5, 1.5))
topoval.setFill('white')
topoval.setOutline(color_rgb(97,76,26))
topoval.draw(win)
coffee = Oval(Point(0.7,1.26), Point(1.4, 1.45))
coffee.setFill(color_rgb(71,54,14))
coffee.setOutline(color_rgb(71,54,14))
coffee.draw(win)
tinycirc = Circle(Point(1.05,0.8), .3)
tinycirc.setFill(color_rgb(33,158,87))
tinycirc.setOutline('white')
tinycirc.draw(win)
letter = Text(Point(1.05,0.8),"B")
letter.setStyle('bold italic')
letter.setTextColor('white')
letter.setFace('courier')
letter.setSize(17)
letter.draw(win)
#Bagel
outerbagel = Oval(Point(1.8, 0.4), Point(2.7, 1))
outerbagel.setFill(color_rgb(191,157,61))
outerbagel.draw(win)
shadow = Oval(Point(2.1, 0.7), Point(2.4, 0.8))
shadow.setFill(color_rgb(130,102,26))
shadow.draw(win)
elif(click.getX() > 4.2 and click.getX() < 5.8 and whitebagel <1 and wheatbagel <1):
errorWindow()
#End
point = win.getMouse()
if (point.getX() > 9 and point.getX() < 9.95):
win.close()
main()
如果未在订单中选择百吉饼,我需要弹出一个错误 window,因为在这个例子中,如果没有百吉饼,咖啡和配料将无法配送。我想我以前在 try/except 中有这个,但现在我也无法让它工作。要么它不会计算总数并弹出,要么它计算但仍然弹出错误,或者没有任何反应!我似乎也无法在关闭弹出窗口后找到任何方向,返回到正常运行的主要 window。当我关闭错误 window 时,主要的 window 不再有效。我希望能够做到这一点。最后我不能使用 tkinter,只能使用 graphics.py。如果有任何粗暴的行为,我也深表歉意,到目前为止,这是我所知道的唯一方法。在此先感谢您的任何建议。
我仍然希望有人会可怜我,并给我一些指导,告诉我这段代码哪里出了问题...我已经更改了我遇到问题的部分,但它仍然无法正常工作:
try:
click = win.getMouse()
if click.getX() > 4.2 and click.getX() < 5.8 and whitebagel >0 and wheatbagel >0:
subtotal = whitetotal+wheattotal+creamtotal+buttertotal+jellytotal+regulartotal+decaftotal
tax = .06
taxes = subtotal*tax
total = taxes+subtotal
subtotal = "$%0.2f" % (subtotal)
taxes = "$%0.2f" % (taxes)
total = "$%0.2f" % (total)
subtotaloutput.setText(subtotal)
taxoutput.setText(taxes)
totaloutput.setText(total)
#Coffee Cup Image
bottomoval = Oval(Point(0.7,0.4), Point(1.4, 0.7))
bottomoval.setFill(color_rgb(97,76,26))
bottomoval.setOutline(color_rgb(97,76,26))
bottomoval.draw(win)
cup = Rectangle(Point(0.7,0.5), Point(1.4,1.3))
cup.setFill(color_rgb(97,76,26))
cup.setOutline(color_rgb(97,76,26))
cup.draw(win)
topoval = Oval(Point(0.6,1.2), Point(1.5, 1.5))
topoval.setFill('white')
topoval.setOutline(color_rgb(97,76,26))
topoval.draw(win)
coffee = Oval(Point(0.7,1.26), Point(1.4, 1.45))
coffee.setFill(color_rgb(71,54,14))
coffee.setOutline(color_rgb(71,54,14))
coffee.draw(win)
tinycirc = Circle(Point(1.05,0.8), .3)
tinycirc.setFill(color_rgb(33,158,87))
tinycirc.setOutline('white')
tinycirc.draw(win)
letter = Text(Point(1.05,0.8),"B")
letter.setStyle('bold italic')
letter.setTextColor('white')
letter.setFace('courier')
letter.setSize(17)
letter.draw(win)
#Bagel
outerbagel = Oval(Point(1.8, 0.4), Point(2.7, 1))
outerbagel.setFill(color_rgb(191,157,61))
outerbagel.draw(win)
shadow = Oval(Point(2.1, 0.7), Point(2.4, 0.8))
shadow.setFill(color_rgb(130,102,26))
shadow.draw(win)
except:
errorWindow()
我想我已经失去理智了,因为我一直在努力让它发挥作用……
首先,tkinter 不是处理图形的方式。它有点陈旧,其中的许多概念将无法移植。再一次,如果没有其他选择,请使用它...
其次,ErrorWindow 的代码缩进不正确。当它出现时,我至少会收到一个错误弹出窗口。可能不在正确的错误条件下。
第三,我还会对点击的 Y 坐标进行某种检查,因为您可以通过点击其垂直列中的任意位置来触发计算按钮,例如小麦文本框。
通常在 GUI 中会有某种事件循环来处理多次点击。类似于:
while(True):
click = win.getMouse()
if calculate button:
get text values
if not bagels:
errorWindow()
continue
calculate
elif quit button:
break
我做了一个修改版本,它有一个以这种方式构造的事件循环,我让它给出了多个总计,并且在我没有百吉饼订单然后继续前进时提出了一个错误。