处理关闭窗口和更改按钮颜色

时间:2013-11-09 02:13:50

标签: python button graphics click drawing

好吧我已经更新了我的代码,现在,我唯一遇到的问题是,我想在用户点击“911”时关闭窗口并尝试使用while循环,但事情是我想不到的获取911按钮= win.close的好方法,另一个问题是,我想更改用户点击的最后一个按钮的按钮颜色,当我点击它时,我改变了其中一个按钮的颜色,但如果我点击另一个按钮,前一个按钮将不会变回主色:S什么可以帮我解决问题?

   # 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()

1 个答案:

答案 0 :(得分:0)

:花了这么长时间没有人回答我的问题,所以我已经弄明白了,你走了,你们可以看看我是怎么做的,有什么问题吗?我在这里帮忙!!      #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,button1将变为红色,如果用户点击另一个按钮,在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