Примерно так:
import wx
class MyWin(wx.Frame):
"""
Creates the GUI
"""
def __init__(self):
super().__init__(None, title="My Enigma Machine", style=wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX))
#### Widgets
## Panel
self.panel = wx.Panel(self)
## Buttons
self.buttonEncript = wx.Button(self.panel, label='Encript')
self.buttonDecript = wx.Button(self.panel, label='Decript')
## TextCtrl
self.tcToEncript = wx.TextCtrl(self.panel, value="", size=(515, 22))
self.tcToDecript = wx.TextCtrl(self.panel, value="", size=(515, 22))
#### Sizers
self.sizer = wx.GridBagSizer(1, 1)
self.sizer.Add(self.tcToEncript, pos=(0, 0), border=2, flag=wx.EXPAND|wx.ALL)
self.sizer.Add(self.buttonEncript, pos=(0, 1), border=2, flag=wx.ALIGN_CENTER|wx.ALL)
self.sizer.Add(self.tcToDecript, pos=(1, 0), border=2, flag=wx.EXPAND|wx.ALL)
self.sizer.Add(self.buttonDecript, pos=(1, 1), border=2, flag=wx.ALIGN_CENTER|wx.ALL)
self.panel.SetSizer(self.sizer)
self.sizer.Fit(self)
#### Bind
self.buttonEncript.Bind(wx.EVT_BUTTON, self.OnEncript)
self.buttonDecript.Bind(wx.EVT_BUTTON, self.OnDecript)
#### Methods of the class
def OnEncript(self, event):
"""
Encript the text. It will just change the case of each letter
"""
string = self.tcToEncript.GetValue()
stringEncripted = string.swapcase()
self.tcToDecript.SetValue(stringEncripted)
def OnDecript(self, event):
"""
Same as OnEncript
"""
string = self.tcToDecript.GetValue()
stringDecripted = string.swapcase()
self.tcToEncript.SetValue(stringDecripted)
if __name__ == '__main__':
app = wx.App()
frame = MyWin()
frame.Show()
app.MainLoop()
На основе ответа @Nick я реализовал более простую версию. Этот подкласс обладает свойством BOOL outlined
, которое по функциям аналогично selected
, highlighted
и enabled
.
При выполнении таких вещей, как [customButtton setImage:[UIImage imageNamed:@"MyOutlinedButton.png"] forState:UIControlStateOutlined]
, он автоматически работает при обновлении свойства outlined
.
При необходимости, можно добавить еще состояние + свойство .
UICustomButton.h
extern const UIControlState UIControlStateOutlined;
@interface UICustomButton : UIButton
@property (nonatomic) BOOL outlined;
@end
UICustomButton.m
const UIControlState UIControlStateOutlined = (1 << 16);
@interface OEButton ()
@property UIControlState customState;
@end
@implementation OEButton
- (void)setOutlined:(BOOL)outlined
{
if (outlined)
{
self.customState |= UIControlStateOutlined;
}
else
{
self.customState &= ~UIControlStateOutlined;
}
[self stateWasUpdated];
}
- (BOOL)outlined
{
return ( self.customState & UIControlStateOutlined ) == UIControlStateOutlined;
}
- (UIControlState)state {
return [super state] | self.customState;
}
- (void)stateWasUpdated
{
[self setNeedsLayout];
}
// These are only needed if you have additional code on -(void)stateWasUpdated
// - (void)setSelected:(BOOL)newSelected
// {
// [super setSelected:newSelected];
// [self stateWasUpdated];
// }
//
// - (void)setHighlighted:(BOOL)newHighlighted
// {
// [super setHighlighted:newHighlighted];
// [self stateWasUpdated];
// }
//
// - (void)setEnabled:(BOOL)newEnabled
// {
// [super setEnabled:newEnabled];
// [self stateWasUpdated];
// }
@end