好吧,我已经更新了我的代码,现在,我唯一遇到的问题是,当用户单击"911"并尝试使用 while 循环时,我想关闭窗口,但问题是我想不出让 911 按钮到 = win.close 的好方法,另一个问题是, 我想更改用户单击的最后一个按钮的按钮颜色,当我单击它时,我更改了其中一个按钮的颜色,但是如果我单击另一个按钮,则上一个按钮不会变回主颜色:有什么可以帮助解决我的问题吗?
# button.py
from graphics import *
from dieview import DieView
from button import Button
win = GraphWin("Phone", 160,160)
win.setBackground("gray")
m = Button (win, Point(30,20),10,10,"1")
quitButton= Button(win, Point(60,20), 10, 10, "2")
quitButton.label.setFill('black')
quitButton.rect.setWidth(1)
quitButton.active=True
quitButton1 = Button(win, Point(30,20), 10, 10, "1")
quitButton1.label.setFill('black')
quitButton1.rect.setWidth(1)
quitButton1.active=True
quitButton2 = Button(win, Point(90,20), 10, 10, "3")
quitButton2.label.setFill('black')
quitButton2.rect.setWidth(1)
quitButton2.active=True
quitButton3 = Button(win, Point(30,50), 10, 10, "4")
quitButton3.label.setFill('black')
quitButton3.rect.setWidth(1)
quitButton3.active = True
quitButton4 = Button(win, Point(60,50), 10, 10, "5")
quitButton4.label.setFill('black')
quitButton4.rect.setWidth(1)
quitButton4.active=True
quitButton5 = Button(win, Point(90,50), 10, 10, "6")
quitButton5.label.setFill('black')
quitButton5.rect.setWidth(1)
quitButton5.active=True
quitButton6 = Button(win, Point(30,80), 10, 10, "7")
quitButton6.label.setFill('black')
quitButton6.rect.setWidth(1)
quitButton6.active=True
quitButton7 = Button(win, Point(60,80), 10, 10, "8")
quitButton7.label.setFill('black')
quitButton7.rect.setWidth(1)
quitButton7.active=True
quitButton8 = Button(win, Point(90,80), 10, 10, "9")
quitButton8.label.setFill('black')
quitButton8.rect.setWidth(1)
quitButton8.active=True
quitButton0 = Button(win,Point(90,110),20,10,"quit")
quitButton0.label.setFill("red")
quitButton0.rect.setWidth(1)
quitButton0.active=True
q=Circle(Point(90,110),10)
q.setFill("gray")
q.draw(win)
Text(Point(90,110),"quit").draw(win)
value=DieView(win,Point(30,20),10)
value8=DieView(win,Point(90,80),10)
c = Circle(Point(30,20),10)
c.setFill("lightgray")
c.draw(win)
Text(Point(30,20), "1").draw(win)
c1 = Circle(Point(60,20),10)
c1.setFill("lightgray")
c1.draw(win)
Text(Point(60,20), "2").draw(win)
c2 = Circle(Point(90,20),10)
c2.setFill("lightgray")
c2.draw(win)
Text(Point(90,20),"3").draw(win)
c3 = Circle(Point(30,50),10)
c3.setFill("lightgray")
c3.draw(win)
c4 = Circle(Point(60,50),10)
c4.setFill("lightgray")
c4.draw(win)
Text(Point(30,50), "4").draw(win)
c5 = Circle(Point(90,50),10)
c5.setFill("lightgray")
c5.draw(win)
Text(Point(60,50), "5").draw(win)
c6 = Circle(Point(30,80),10)
c6.setFill("lightgray")
c6.draw(win)
Text(Point(90,50), "6").draw(win)
c7 = Circle(Point(60,80),10)
c7.setFill("lightgray")
c7.draw(win)
Text(Point(30,80), "7").draw(win)
c8 = Circle(Point(90,80),10)
c8.setFill("lightgray")
c8.draw(win)
Text(Point(60,80), "8").draw(win)
Text(Point(90,80), "9").draw(win)
listclick=[quitButton,quitButton1]
pt = win.getMouse()
while not quitButton0.clicked(pt):
if quitButton.clicked(pt):
print("2")
c1.setFill("red")
elif quitButton1.clicked(pt):
print("1")
elif quitButton2.clicked(pt):
quitButton.active=True
print("3")
elif quitButton3.clicked(pt):
quitButton3.active = True
print("4")
elif quitButton4.clicked(pt):
quitButton4.active = True
print("5")
elif quitButton5.clicked(pt):
quitButton5.active = True
print("6")
elif quitButton6.clicked(pt):
quitButton6.active = True
print("7")
elif quitButton7.clicked(pt):
quitButton7.active = True
print("8")
elif quitButton8.clicked(pt):
quitButton8.active=True
print("9")
pt = win.getMouse()
quitButton0=win.close()
:s 花了这么长时间,没有人回答我的问题,所以我已经想通了# button.py从图形导入 *
from button import Button
win = GraphWin("Phone", 140,160)
win.setBackground("lightgray")
quitButton0 =Button(win,Point(60,110),18,10,"0")
quitButton0.label.setFill("black")
quitButton0.rect.setWidth(1)
quitButton0.active=True
quitButton= Button(win, Point(60,20), 18, 10, "2")
quitButton.label.setFill('black')
quitButton.rect.setWidth(1)
quitButton.active=True
quitButton1 = Button(win, Point(30,20), 18, 10, "1")
quitButton1.label.setFill('black')
quitButton1.rect.setWidth(1)
quitButton1.active=True
quitButton2 = Button(win, Point(90,20), 18, 10, "3")
quitButton2.label.setFill('black')
quitButton2.rect.setWidth(1)
quitButton2.active=True
quitButton3 = Button(win, Point(30,50), 18, 10, "4")
quitButton3.label.setFill('black')
quitButton3.rect.setWidth(1)
quitButton3.active = True
quitButton4 = Button(win, Point(60,50), 18, 10, "5")
quitButton4.label.setFill('black')
quitButton4.rect.setWidth(1)
quitButton4.active=True
quitButton5 = Button(win, Point(90,50), 18, 10, "6")
quitButton5.label.setFill('black')
quitButton5.rect.setWidth(1)
quitButton5.active=True
quitButton6 = Button(win, Point(30,80), 18, 10, "7")
quitButton6.label.setFill('black')
quitButton6.rect.setWidth(1)
quitButton6.active=True
quitButton7 = Button(win, Point(60,80), 18, 10, "8")
quitButton7.label.setFill('black')
quitButton7.rect.setWidth(1)
quitButton7.active=True
quitButton8 = Button(win, Point(90,80), 18, 10, "9")
quitButton8.label.setFill('black')
quitButton8.rect.setWidth(1)
quitButton8.active=True
quitButton13 = Button(win, Point(90,200), 0, 0, "")
quitButton13.label.setFill('gray')
quitButton13.rect.setWidth(0)
quitButton13.active=False
c0=Circle(Point(60,110),10)
c0.setFill("gray")
c0.draw(win)
Text(Point(60,110),"0").draw(win)
c1 = Circle(Point(60,20),10)
c1.setFill("gray")
c1.draw(win)
Text(Point(60,20), "2").draw(win)
c2 = Circle(Point(90,20),10)
c2.setFill("gray")
c2.draw(win)
Text(Point(90,20),"3").draw(win)
c3 = Circle(Point(30,50),10)
c3.setFill("gray")
c3.draw(win)
c4 = Circle(Point(60,50),10)
c4.setFill("gray")
c4.draw(win)
Text(Point(30,50), "4").draw(win)
c5 = Circle(Point(90,50),10)
c5.setFill("gray")
c5.draw(win)
Text(Point(60,50), "5").draw(win)
c6 = Circle(Point(30,80),10)
c6.setFill("gray")
c6.draw(win)
Text(Point(90,50), "6").draw(win)
c7 = Circle(Point(60,80),10)
c7.setFill("gray")
c7.draw(win)
Text(Point(30,80), "7").draw(win)
c8 = Circle(Point(90,80),10)
c8.setFill("gray")
c8.draw(win)
Text(Point(60,80), "8").draw(win)
Text(Point(90,80), "9").draw(win)
close = Button(win, Point(30,20), 10, 10, "1")
close.label.setFill("black")
close.rect.setWidth(0)
close.active=False
Text(Point(30,20), "1").draw(win)
but=Button(win,Point(30,20),10,10,"1")
but.label.setFill("black")
but.rect.setWidth(0)
#but.active = False
h = Circle(Point(30,20),10)
h.setFill("gray")
h.draw(win)
Text(Point(30,20), "1").draw(win)
pt = win.getMouse()
while not but.clicked(pt):
if quitButton8.clicked(pt):
quitButton8.active=True
print("9")
c8.setFill("red")
close.active=True
c0.setFill("gray")
quitButton1.active=False
c1.setFill("gray")
c2.setFill("gray")
c3.setFill("gray")
c4.setFill("gray")
c5.setFill("gray")
c6.setFill("gray")
c7.setFill("gray")
h.setFill("gray")
elif close.clicked(pt):
close.active=False
h.setFill("red")
print("1")
but.active=True
c1.setFill("gray")
c2.setFill("gray")
c3.setFill("gray")
c4.setFill("gray")
c5.setFill("gray")
c6.setFill("gray")
c7.setFill("gray")
c8.setFill("gray")
elif quitButton0.clicked(pt):
print("0")
c0.setFill("red")
close.active=False
c1.setFill("gray")
c2.setFill("gray")
c3.setFill("gray")
c4.setFill("gray")
c5.setFill("gray")
c6.setFill("gray")
c7.setFill("gray")
h.setFill("gray")
quitButton1.active=True
c8.setFill("gray")
elif quitButton.clicked(pt):
print("2")
c1.setFill("red")
quitButton1.active=True
close.active=False
c8.setFill("gray")
c0.setFill("gray")
c3.setFill("gray")
c4.setFill("gray")
c5.setFill("gray")
c6.setFill("gray")
c7.setFill("gray")
h.setFill("gray")
elif quitButton1.clicked(pt):
print("1")
h.setFill("red")
quitButton1.active=True
but.active=False
close.active = False
c8.setFill("gray")
c0.setFill("gray")
c1.setFill("gray")
c2.setFill("gray")
c3.setFill("gray")
c4.setFill("gray")
c5.setFill("gray")
c6.setFill("gray")
c7.setFill("gray")
elif quitButton2.clicked(pt):
quitButton.active=True
print("3")
c2.setFill("red")
quitButton1.active=True
close.active=False
c8.setFill("gray")
c1.setFill("gray")
c0.setFill("gray")
c3.setFill("gray")
c4.setFill("gray")
c5.setFill("gray")
c6.setFill("gray")
c7.setFill("gray")
h.setFill("gray")
elif quitButton3.clicked(pt):
quitButton3.active = True
print("4")
c3.setFill("red")
quitButton1.active=True
close.active=False
c8.setFill("gray")
c0.setFill("gray")
c1.setFill("gray")
c2.setFill("gray")
c4.setFill("gray")
c5.setFill("gray")
c6.setFill("gray")
c7.setFill("gray")
h.setFill("gray")
elif quitButton4.clicked(pt):
quitButton4.active = True
print("5")
c4.setFill("red")
close.active=False
c8.setFill("gray")
c0.setFill("gray")
c1.setFill("gray")
c2.setFill("gray")
c3.setFill("gray")
c5.setFill("gray")
c6.setFill("gray")
c7.setFill("gray")
h.setFill("gray")
close.active=False
quitButton1.active=True
elif quitButton5.clicked(pt):
quitButton5.active = True
print("6")
c5.setFill("red")
close.active=False
c8.setFill("gray")
c0.setFill("gray")
c1.setFill("gray")
c2.setFill("gray")
c3.setFill("gray")
c4.setFill("gray")
c6.setFill("gray")
c7.setFill("gray")
h.setFill("gray")
quitButton1.active=True
elif quitButton6.clicked(pt):
quitButton6.active = True
print("7")
c6.setFill("red")
close.active=False
quitButton1.active=True
c8.setFill("gray")
c0.setFill("gray")
c1.setFill("gray")
c2.setFill("gray")
c3.setFill("gray")
c4.setFill("gray")
c5.setFill("gray")
c7.setFill("gray")
h.setFill("gray")
elif quitButton7.clicked(pt):
quitButton7.active = True
print("8")
c7.setFill("red")
quitButton1.active=True
close.active=False
c8.setFill("gray")
c0.setFill("gray")
c1.setFill("gray")
c2.setFill("gray")
c3.setFill("gray")
c4.setFill("gray")
c5.setFill("gray")
c6.setFill("gray")
h.setFill("gray")
pt = win.getMouse()
win.close()
现在,如果用户单击 9-1-1,我所要做的就是关闭窗口,那就是创建另一个按钮,直到用户在 9>1 之后立即单击时才激活,然后这将激活另一个按钮,如果您单击该按钮,窗口将关闭,所以我让它好像我正在制作一个退出按钮,直到用户玩游戏才会被激活, 我知道代码看起来很想念,但它有效,你需要分析我的代码才能理解它,但我在这里给你一个想法,现在我已经完成了"如果用户点击 9-1-1 窗口关闭",另一件事是,如果用户例如单击按钮编号"9",按钮颜色将从"灰色"变为"红色",如果后跟另一个单击另一个按钮,按钮编号 9 将从"红色"颜色"灰色"开始,这是我按钮的主要颜色,我在 while 循环中这样做的方式是一个示例" 如果用户单击按钮 1,按钮 1 将变为红色,如果用户单击另一个按钮,在 if 语句中, 在包括
if button 1.clicked(pt):
button1.setFill("red")
button2.setFill("gray")
elif button2.clicked(pt):
button2.setFill("red")
button1.setFill("gray") # that way if you click button 1, button 1 will turn red, when you click button 2, button 2 will turn red, and will turn button 1 to gray color