tkinter中的循环 - 帮助!

时间:2011-07-21 00:53:15

标签: python tkinter

这是我第一次使用这个论坛,希望我可以通过详细解释得到快速回复。 运行: Python 3.2 Tkinter 8.5& Windows7 x64

我的代码:

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()函数。 我不确定在哪里放置循环,以及如何编写循环(for循环,while循环,if,但是,等等) 感谢大家帮忙,你可以提供任何帮助:)

1 个答案:

答案 0 :(得分: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