2011-07-21 97 views
0

这是我第一次使用这个论坛,希望我能得到一个快速的回应与详细的解释。 运行: 的Python 3.2 Tkinter的8.5 & Windows7的64tkinter中的循环 - 帮助!

我的代码:

from tkinter import * 
import tkinter.messagebox 
class Application(Frame): 
    def __init__(self, master): 
     super(Application, self).__init__(master) 
     self.grid() 
     self.lvl_one_att = 4 
     self.bttn_clicks = 2 
     self.bttn_clicks2 = 2 
     self.bttn_clicks3 = 1 
     self.bttn_clicks4 = 2 
     self.bttn_clicks5 = 1 
     self.bttn_clicks6 = 2 
     self.bttn_clicks7 = 1 
     self.bttn_clicks8 = 2 
     self.bttn_clicks9 = 2 
     self.level_one() 
    def level_one(self): 
     self.lbl1 = Label(self, text = "You must flip all the boxes to show the same colour!") 
     self.lbl1.grid(row = 0, column = 0, columnspan = 3) 
     self.levellbl = Label(self, text = "Level 1") 
     self.levellbl.grid(row = 4, column = 1, sticky = W, columnspan = 2) 
     self.rulesbttn = Button(self, text = "Instructions", command = self.rules) 
     self.rulesbttn.grid(row = 4, column = 0, sticky = W, columnspan = 1) 
     self.tlbttn = Button(self, bg = "red", width = 20, height = 10, command = self.callback1) 
     self.tlbttn.grid(row = 1, column = 0, columnspan = 1) 
     self.tmbttn = Button(self, bg = "red", width = 20, height = 10, command = self.callback2) 
     self.tmbttn.grid(row = 1, column = 1, columnspan = 1) 
     self.trbttn = Button(self, bg = "blue", width = 20, height = 10, command = self.callback3) 
     self.trbttn.grid(row = 1, column = 2, columnspan = 1) 
     self.mlbttn = Button(self, bg = "red", width = 20, height = 10, command = self.callback4) 
     self.mlbttn.grid(row = 2, column = 0, columnspan = 1) 
     self.mmbttn = Button(self, bg = "blue", width = 20, height = 10, command = self.callback5) 
     self.mmbttn.grid(row = 2, column = 1, columnspan = 1) 
     self.mrbttn = Button(self, bg = "red", width = 20, height = 10, command = self.callback6) 
     self.mrbttn.grid(row = 2, column = 2, columnspan = 1) 
     self.blbttn = Button(self, bg = "blue", width = 20, height = 10, command = self.callback7) 
     self.blbttn.grid(row = 3, column = 0, columnspan = 1) 
     self.bmbttn = Button(self, bg = "red",width = 20, height = 10, command = self.callback8) 
     self.bmbttn.grid(row = 3, column = 1, columnspan = 1) 
     self.brbttn = Button(self, bg = "red", width = 20, height = 10, command = self.callback9) 
     self.brbttn.grid(row = 3, column = 2, columnspan = 1) 
    def callback1(self): 
     self.lvl_one_att -= 1 
     self.color_change1() 
     self.color_change2() 
     self.color_change4() 
     self.color_change5() 
    def callback2(self): 
     self.lvl_one_att -= 1 
     self.color_change1() 
     self.color_change2() 
     self.color_change3() 
     self.color_change5() 
     self.color_change4() 
     self.color_change6() 
    def callback3(self): 
     self.lvl_one_att -= 1 
     self.color_change2() 
     self.color_change3() 
     self.color_change6() 
     self.color_change5() 
    def callback4(self): 
     self.lvl_one_att -= 1 
     self.color_change1() 
     self.color_change2() 
     self.color_change4() 
     self.color_change5() 
     self.color_change7() 
     self.color_change8() 
    def callback5(self): 
     self.lvl_one_att -= 1 
     self.color_change1() 
     self.color_change2() 
     self.color_change3() 
     self.color_change6() 
     self.color_change9() 
     self.color_change4() 
     self.color_change5() 
     self.color_change7() 
     self.color_change8() 
    def callback6(self): 
     self.lvl_one_att -= 1 
     self.color_change3() 
     self.color_change2() 
     self.color_change6() 
     self.color_change5() 
     self.color_change9() 
     self.color_change8() 
    def callback7(self): 
     self.lvl_one_att -= 1 
     self.color_change4() 
     self.color_change5() 
     self.color_change7() 
     self.color_change8() 
    def callback8(self): 
     self.lvl_one_att -= 1 
     self.color_change4() 
     self.color_change5() 
     self.color_change6() 
     self.color_change7() 
     self.color_change8() 
     self.color_change9() 
    def callback9(self): 
     self.lvl_one_att -= 1 
     self.color_change5() 
     self.color_change6() 
     self.color_change9() 
     self.color_change8() 
    def color_change1(self): 
     self.bttn_clicks += 1 
     if self.bttn_clicks == 3: 
      self.bttn_clicks = 1 
     if self.bttn_clicks == 1: 
      self.tlbttn.configure(bg = "blue") 
     else: 
      self.tlbttn.configure(bg = "red") 
    def color_change2(self): 
     self.bttn_clicks2 += 1 
     if self.bttn_clicks2 == 3: 
      self.bttn_clicks2 = 1 
     if self.bttn_clicks2 == 1: 
      self.tmbttn.configure(bg = "blue") 
     else: 
      self.tmbttn.configure(bg = "red") 
    def color_change3(self): 
     self.bttn_clicks3 += 1 
     if self.bttn_clicks3 == 3: 
      self.bttn_clicks3 = 1 
     if self.bttn_clicks3 == 1: 
      self.trbttn.configure(bg = "blue") 
     else: 
      self.trbttn.configure(bg = "red") 
    def color_change4(self): 
     self.bttn_clicks4 += 1 
     if self.bttn_clicks4 == 3: 
      self.bttn_clicks4 = 1 
     if self.bttn_clicks4 == 1: 
      self.mlbttn.configure(bg = "blue") 
     else: 
      self.mlbttn.configure(bg = "red") 
    def color_change5(self): 
     self.bttn_clicks5 += 1 
     if self.bttn_clicks5 == 3: 
      self.bttn_clicks5 = 1 
     if self.bttn_clicks5 == 1: 
      self.mmbttn.configure(bg = "blue") 
     else: 
      self.mmbttn.configure(bg = "red") 
    def color_change6(self): 
     self.bttn_clicks6 += 1 
     if self.bttn_clicks6 == 3: 
      self.bttn_clicks6 = 1 
     if self.bttn_clicks6 == 1: 
      self.mrbttn.configure(bg = "blue") 
     else: 
      self.mrbttn.configure(bg = "red") 
    def color_change7(self): 
     self.bttn_clicks7 += 1 
     if self.bttn_clicks7 == 3: 
      self.bttn_clicks7 = 1 
     if self.bttn_clicks7 == 1: 
      self.blbttn.configure(bg = "blue") 
     else: 
      self.blbttn.configure(bg = "red") 
    def color_change8(self): 
     self.bttn_clicks8 += 1 
     if self.bttn_clicks8 == 3: 
      self.bttn_clicks8 = 1 
     if self.bttn_clicks8 == 1: 
      self.bmbttn.configure(bg = "blue") 
     else: 
      self.bmbttn.configure(bg = "red") 
    def color_change9(self): 
     self.bttn_clicks9 += 1 
     if self.bttn_clicks9 == 3: 
      self.bttn_clicks9 = 1 
     if self.bttn_clicks9 == 1: 
      self.brbttn.configure(bg = "blue") 
     else: 
      self.brbttn.configure(bg = "red") 
    def rules(self): 
     tkinter.messagebox.showinfo("Instructions", "The idea of the game, is to make sure all the boxes on the screen are the same colour. By pressing a button it will flip, all colours touching it, including diagonally. ") 
    def round_win(self): 
     self.lbl1.grid_forget() 
     self.rulesbttn.grid_forget() 
     self.tlbttn.grid_forget() 
     self.tmbttn.grid_forget() 
     self.trbttn.grid_forget() 
     self.mlbttn.grid_forget() 
     self.mmbttn.grid_forget() 
     self.mrbttn.grid_forget() 
     self.blbttn.grid_forget() 
     self.bmbttn.grid_forget() 
     self.brbttn.grid_forget() 
     self.win() 
    def win(self): 
     self.lbl2 = Label(self, text = "CONGRATULATIONS!", font=("Helvetica", 40)) 
     self.lbl2.grid(row = 0, column = 0) 
root = Tk() 
root.title("Program") 
app = Application(root) 
root.mainloop() 

什么我可以计算出如何做的,就是以某种方式使用一个循环,这样,当全部9个是红是它运行round_win()函数。 IM不确定哪里放环,以及如何编写它(for循环,while循环,如果,但是,其他等) 感谢为满口任何帮助球员则可以提供:)

+0

我从来没有说过这是功课。我只是问,有人可以帮助循环部分。我做了一堆看,但没有什么是明确的,或者我不能将它分解成我自己的代码(从伪代码或其他人的例子)。 – James

+1

作业与否,你需要重写这个。你有没有想过,因为你在一行中复制和粘贴完全相同的功能___,可能有更好的方法来做到这一点? – senderle

+0

是的,我做到了。但我想不出一个办法,没有人问我帮助过我,我自己的方式也是如此。 – James

回答

3

(这是我在stackoverflow的第一篇文章) 我认为这样的事情会工作。 您可以将原始按钮放入列表中。 ...

def level_one(self): 
    ... 
    self.game_buttons = [] 

    for btn_index in range(9): 
     bg = "red" # change this to something based on the index 
     next_button = Button(self, bg = bg, width = 20, height = 10) 
     next_button['command'] = (lambda index=btn_index: self.callback(index)) 

     row = index/3 + 1 
     column = index % 3 
     next_button.grid(row = row, column = column, columnspan = 1) 
     self.game_buttons.append(next_button) 

def callback1(self, index): 
    button = self.game_buttons[index] 
    # add the appropriate code here 

def color_change(self, index): 
    # add the appropriate code here 

此外,您还可以更改下面的代码...

self.bttn_clicks2 += 1 
    if self.bttn_clicks2 == 3: 
     self.bttn_clicks2 = 1 

到...

self.bttn_clicks2 = self.bttn_clicks2 % 3 + 1 
+0

你在正确的轨道上,但你需要阅读:http://stackoverflow.com/questions/6737112/have-python-2-7-functions-remember-value-and-not-reference-closure-奇怪 – phkahler

+0

更改为... (lambda index = btn_index:self.callback(index)) – jcfollower