我正在爲一個項目在學校編程,而我陷入了非常簡單的事情,我無法解決。該程序是遊戲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(),但我不知道爲了使它工作而放在哪裏,而且我的所有嘗試都失敗了。
我希望你幫我.. :)
只是爲了澄清:一旦計數器達到4,你想禁用*所有*按鈕? – dpwilson
所有的數字按鈕(當然,「刪除」和「發送」按鈕將能夠被按下)。 – omer