How to disable a button control in wxPython?

eriggs eriggs at kollmorgen.com
Sat Feb 16 10:12:24 EST 2002


Robert Amesz <sheershion at mailexpire.com> wrote in message news:<Xns91B6DE74F368Crcamesz at amesz.demon.nl>...
> eriggs wrote:
> 
> > I'm somewhat new to Python, and I'm trying to figure out how to
> > disable a button control.
> 
> Use use button.Enable(true) instead of button.Enable(), and use 
> button.Enable(false) instead of button.Disable(). That should do the 
> trick. The documentation seems to be wrong concerning that, at least 
> for wxPython.
> 
> Robert Amesz


That did it... Thanks!!  BTW, I'm reposting my modified source in case
anyone else can benefit.

---  Earl

######################################################################
# [ Contents of PYWRUN.bat... ]
#@echo off
#set PYTHONPATH=c:\python21\lib;c:\python21\libcust;c:\pythprgs\lib
#c:\python21\pythonw %1
#
# Usage:  pywrun my_file.py
######################################################################
from wxPython.wx import *

class ButtonFrame(wxFrame):
    'Creates a frame with a single button in the center'
    def __init__(self):
        wxFrame.__init__(self, NULL, -1, 'wxPython',
                         wxDefaultPosition, (330, 210))

        panel = wxPanel(self, -1) 

        #wxTextCtrl(wxWindow* parent, wxWindowID id, 
        #           const wxString& value = "", 
        #           const wxPoint& pos,
        #           const wxSize& size = wxDefaultSize, 
        #           long style = 0, const wxValidator& validator, 
        #           const wxString& name = "text")
        tc = wxTextCtrl(panel, -1, "", wxPoint(10, 10), \
                    wxSize(300, 100), wxTE_MULTILINE|wxTE_READONLY)
        self.msgtext = tc

        button1 = wxButton(panel, 111, 'Start', wxPoint(10, 130), \
                           wxSize(100, 30))
        EVT_BUTTON(self, 111, self.onButtonStart)

        button1.SetDefault()
        self.StartButton = button1
 

        button2 = wxButton(panel, 112, 'Quit', wxPoint(210, 130), \
                           wxSize(100, 30))
        EVT_BUTTON(self, 112, self.onButtonQuit)
        EVT_CLOSE(self, self.OnCloseWindow)

        self.QuitButton = button2

        # Disable Quit button
        self.QuitButton.Enable(false)
        # Enable Start button
        self.StartButton.Enable(true)


    def onButtonStart(self, event):
        'Create a message dialog when the button is clicked'
        dlg = wxMessageDialog(self, 'Starting...',\
                              'Start', wxOK)
        dlg.ShowModal()
        dlg.Destroy()

        self.setText("Starting program...")
        # Enable Quit button
        self.QuitButton.Enable(true)
        # Disable Start button
        self.StartButton.Enable(false)
        # Start program loop


    def onButtonQuit(self, event):
        'Create a message dialog when the button is clicked'
        dlg = wxMessageDialog(self, 'Quitting...',\
                              'Stop', wxOK)
        dlg.ShowModal()
        dlg.Destroy()

        self.setText("Stopping program...")

        self.Close(true)


    def OnCloseWindow(self, event):
        self.Destroy()


    def setText(self, msg):
        'Create a message dialog when the button is clicked'
        self.msgtext.Clear()
        self.msgtext.AppendText(msg)


class App(wxApp):
    def OnInit(self):
        'Create the main window and insert the custom frame'
        frame = ButtonFrame()
        frame.Show(true)
        return true # Yes, continue processing

# Create the app and start processing messages
app = App(0)
app.MainLoop()



More information about the Python-list mailing list