2016-04-21 20 views
2

我正在爲一個項目在學校編程,而我陷入了非常簡單的事情,我無法解決。該程序是遊戲BULLS AND COWS的GUI。 我使用數字按鈕做到這一點,即每次點擊它們時,都會將選定的數字添加到文本框中。由於遊戲規則,在轉彎結束前必須只有4個數字,所以我想要做的就是讓按鈕禁用,如果在文本框中已經有4個數字。 下面是代碼:wxpython - 如何使程序中的某些事情發生時禁用按鈕?

import wx 
from random import randint 

OPTIONS = [1, 2, 3, 4, 5, 6, 7, 8, 9, "DEL", 0, "SEND"] 
# these are the events' IDs sent to a function when you click a button. 
# the OPTIONS_ID is in the same order of OPTIONS. 

OPTIONS_ID = [-31990,-31989,-31988,-31987,-31986,-31985, -31984, -31983, -31982, -31981, -31980, -31979] # the built in wxpython IDs for the buttons 


GAME_POSITION = (400, 100) 
GAME_SIZE = [900, 600] 

class Frame(wx.Frame): # class for all the frames in our game. 

    def __init__(self, parent, id, title, pos, size): 
     wx.Frame.__init__(self, parent, id, title, pos, size) 
     self.panel = wx.Panel(self) 
     self.fdf = wx.TextCtrl(self.panel, size=(275, 75), pos=(520, 20)) 
     self.count = 0 
     self.check = False 
    # this function creates a textbox at a specific position with a specific size. 
    def write(self, panel, txt, pos, size=20, font_family=wx.SWISS, font_style = wx.NORMAL,font_weight = wx.BOLD, underline = False): 
     # create a textbox at a specific position with a specific size. 
     your_txt = wx.StaticText(panel, -1, txt, pos) 
     your_txt.SetFont(wx.Font(size,font_family,font_style,font_weight,underline)) 
    # same as above, just for a button. 
    def create_button(self, panel, txt, position, width, height): 
     Size = wx.Size(width, height) 
     self.button = wx.Button(panel, -1, txt, position, Size) 
     self.border = wx.BoxSizer(wx.VERTICAL) 
     self.border.Add(self.button) 
     self.Bind(wx.EVT_BUTTON, lambda evt: self.OnButton(evt), self.button) 
    def create_disable(self, panel, txt, position, width, height): 
     Size = wx.Size(width, height) 
     self.button = wx.Button(panel, -1, txt, position, Size) 
     self.border = wx.BoxSizer(wx.VERTICAL) 
     self.border.Add(self.button) 
     self.button.Disable() 
    def OnButton(self, event): 
     print repr(event.Id) + "," 
     if event.Id in OPTIONS_ID: # if indeed an option button was pressed 
      exited = -1 # exited is 5100 if the user exited his dialog box 
      # assigning the events to the button. 
      if event.Id == OPTIONS_ID[0]: # 1 
       self.fdf.AppendText(str(OPTIONS[0])) 
       self.count += 1 
      elif event.Id == OPTIONS_ID[1]: # 2 
       self.fdf.AppendText(str(OPTIONS[1])) 
       self.count += 1 
      elif event.Id == OPTIONS_ID[2]: # 3 
       self.fdf.AppendText(str(OPTIONS[2])) 
       self.count += 1 
      elif event.Id == OPTIONS_ID[3]: # 4 
       self.fdf.AppendText(str(OPTIONS[3])) 
       self.count += 1 
      elif event.Id == OPTIONS_ID[4]: # 5 
       self.fdf.AppendText(str(OPTIONS[4])) 
       self.count += 1 
      elif event.Id == OPTIONS_ID[5]: # 6 
       self.fdf.AppendText(str(OPTIONS[5])) 
       self.count += 1 
      elif event.Id == OPTIONS_ID[6]: # 7 
       self.fdf.AppendText(str(OPTIONS[6])) 
       self.count += 1 
      elif event.Id == OPTIONS_ID[7]: # 8 
       self.fdf.AppendText(str(OPTIONS[7])) 
       self.count += 1 
      elif event.Id == OPTIONS_ID[8]: # 9 
       self.fdf.AppendText(str(OPTIONS[8])) 
       self.count += 1 
      elif event.Id == OPTIONS_ID[9]: # del 
       if self.count > 0: 
        self.count -= 1 
       self.fdf.SetValue(self.fdf.GetValue()[:-1]) 
      elif event.Id == OPTIONS_ID[10]: # 0 
       self.fdf.AppendText(str(OPTIONS[10])) 
       self.count += 1 
      elif event.Id == OPTIONS_ID[11]: # send 
       self.fdf.AppendText(str(OPTIONS[11])) 
      if self.count == 4: 
       print "ddd" 


class Game(wx.App): 
    def OnInit(self): # upon game opening 
     # I would like the options window to be the first window's parent 
     # so I will first set up our options window: 
     window = Frame(None, -1, "Good Luck!", GAME_POSITION, GAME_SIZE) 
     first_panel = window.panel 
     window.write(first_panel, "BULLS AND COWS!", (50, 50), size=(35)) 
     countX = 500 
     countY = 100 
     for option in OPTIONS: 
      window.create_button(first_panel,str(option), (countX, countY), 100, 100) 
      countX += 110 
      if str(option) == "3" or str(option) == "6" or str(option) == "9": 
       countY += 110 
       countX = 500 



     window.Show(True) 
     return True 


def main(): 
    camel = Game() 
    camel.MainLoop() 


if __name__ == '__main__': 
    main() 

我想那些重要的一個按鈕的每次點擊計數器變量要做到這一點,當他等於4(這意味着有4個數字在文本框中) ,然後發生了一些事情(在我告訴他沒有理由打印字符串「ddd」的代碼中)。問題是:如何在4個數字(而不是打印「ddd」)時使按鈕禁用?我知道命令button.Disable(),但我不知道爲了使它工作而放在哪裏,而且我的所有嘗試都失敗了。

我希望你幫我.. :)

+0

只是爲了澄清:一旦計數器達到4,你想禁用*所有*按鈕? – dpwilson

+0

所有的數字按鈕(當然,「刪除」和「發送」按鈕將能夠被按下)。 – omer

回答

0

要禁用所有的按鈕,一旦計數器達到4,使用GetChildren()方法在容器上。在這種情況下,即self.panel,這是所有按鈕的父項。只要撥打self.panel.GetChildren()就會給你所有的孩子,但不僅僅是按鈕。

幸運的是,Python具有內置函數isinstance()來檢查對象是否是給定的類。爲了確保我們只禁用按鈕,我們檢查isinstance(button, wx.Button)是否返回true,表明該對象確實是wx.Button的實例。

將此限制爲僅編號按鈕使其稍微複雜化,但不會太多。只需檢查按鈕的標籤!你可以這樣做:

... 
if self.count == 4: 
    for child in self.panel.GetChildren(): 
     if isinstance(child, wx.Button): 
      try: 
       int(child.GetLabel()) 
      except ValueError: 
       pass 
      else: 
       child.Disable() 
... 

首先,我們遍歷你面板的所有孩子。在下一行中,我們檢查確定孩子實際上是一個按鈕,而不是靜態文本或類似的東西。最後,我們檢查標籤是否是一個整數。如果不是的話(它會拋出一個ValueError),我們忽略它;如果是,我們將其禁用。

希望這會有所幫助!

+0

非常感謝!它是完美的工作。我怎樣才能更多地瞭解這個話題(什麼是兒童和家長,以及如何使用它們)?我想這對我很有用.. – omer

+0

現在我注意到一個小錯誤.. 如果我把4個數字它是完全像我想要的(禁用所有的數字按鈕),但如果在此之後我刪除一個數字,比計數變爲3 - 但數字按鈕仍然禁用。 我該如何解決這個問題? 在這裏我想說的是,英語不是我的母語,所以我可能有一些語法錯誤.. sory;) – omer

+0

我自己修復它!抱歉的不必要的問題..哈哈 再一次,泰克你的幫助,我真的很感激它。 – omer

相關問題