0
我正在爲我的軟件工程類構建一個桌面遊戲的GUI。我正在Python 2.7(windows)上使用TKinter工具包。我現在被卡住了,因爲我似乎找不到一種方法來忽略/忘記按鈕的某種順序。基本上,我試圖創建一個代表我的遊戲板的按鈕網格。而現在,我有一個遊戲板,在7x7網格上總共有49個按鈕。如何使用grid_forget消除特定的按鈕實例化後的排序
到目前爲止,這是我能夠做到:
- 實例化我所有的按鈕對象,其中,列= x和行= Y。這很容易地建立X * Y的網格
- 然後我把每個按鈕到一個列表(可以稱之爲列表1)
我想用我的按鈕對象列表忽略/忘記/刪除(因爲缺乏的更好的描述)某些按鈕。我想我可以創建我想要使用grid_forget的按鈕對象的索引的第二個列表(list2),然後比較我的兩個列表,並只保留那些不在list2中的列表。不幸的是,這並不符合我的要求。下面是代碼:
gameboard = ttk.Labelframe(root, padding = (8,8,8,8), text = "Gameboard", relief = "sunken") #forgetButtons will not be displayed on the game board b/c they do not have a #label (they are not a: room, hallway, starting space) forgetButtons = [0,1,3,5,6,7,13,14,16,18,21,30,32,41,42,43,45,46,47,48] #this list tracks all the buttons on the gameboard myButtons=[] count = 0 for x in range(7): #build a 7x7 grid of buttons (49 buttons total) for y in range(7): btn = Button(gameboard, width=7, height=4) myButtons.append(btn) btn.grid(column=x, row=y, padx = 3, pady = 3) #do some comparison here between the two lists #to weed out the buttons found in forgetButtons #**or maybe it should not be done here?** btn.config(text="Room%d\none\ntwo\nfour\nfive" % x)
@http://stackoverflow.com/users/1019227/a-rodas非常感謝。這很好。我需要研究itertools。甚至沒有意識到它們的存在。這就是我喜歡Python的原因。我只需要將forgetButtons列表重新排序一下,但我想我可以處理該部分。 – Mash