python - A loop within tkinter - HELP! -
this first time using forum, hopefully, can quick response detailed explanation. running: python 3.2 tkinter 8.5 & windows7 x64
my code:
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 boxes show 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 game, make sure boxes on screen same colour. pressing button flip, 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()
what can work out how do, somehow use loop when 9 squares red runs round_win() function. im unsure put loop, , how write (for loop, while loop, if, but, else, etc) heaps guys may able provide :)
(this first post on stackoverflow) think work. can put original buttons list. ...
def level_one(self): ... self.game_buttons = [] btn_index in range(9): bg = "red" # change based on 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 appropriate code here def color_change(self, index): # add appropriate code here
also, can change following code ...
self.bttn_clicks2 += 1 if self.bttn_clicks2 == 3: self.bttn_clicks2 = 1
to ...
self.bttn_clicks2 = self.bttn_clicks2 % 3 + 1
Comments
Post a Comment