0

私はコードを持っています

class Button(object):
'''A simple Button class to represent a UI Button element'''

def __init__(self, text = "button"):
    '''Create a Button and assign it a label'''
    self.label = text

def press(self):
    '''Simply print that the button was pressed'''
    print("{0} was pressed".format(self.label))

class ToggleButton(Button):
def __init__(self, text, state=True):
    super(ToggleButton, self).__init__(text)
    self.state = state

def press(self):
    super(ToggleButton, self).press()
    self.state = not self.state
    print('{0} is now'.format(self.label), 'ON' if self.state else 'OFF')

入力すると

tb = ToggleButton("Test", False) 
tb.press()
tb.press() 

それは正常に動作し、戻ります

Test was pressed
Test is now ON
Test was pressed
Test is now OFF

しかし、私が望むのは、テキストパラメータをオプションにすることです。

b = ToggleButton()
b.press()

それは戻ってきます

ToggleButton was pressed
ToggleButton is now OFF

どんな助けでも大歓迎です!

4

2 に答える 2

0

stateパラメータの例に従って、textデフォルト値も指定します。

class ToggleButton(Button):
    def __init__(self, text="ToggleButton", state=True):
        super(ToggleButton, self).__init__(text)
        self.state = state
于 2013-05-24T02:51:30.487 に答える