2011-07-21 128 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