Search code examples
pythonwxpythonwxtextctrl

wxPython wx.TextCtrl is uneditable


In my wxPython Application I have a few wx.TextCtrl input fields for a form. My problem is, some of my wx.TextCtrl input fields are uneditable, meaning I can not type in the field nor can I select text that is in the field. My parent wx.Panel in my main.py file has two children a login wx.Panel and a wx.Notebook. The wx.TextCtrls in my login work great, but the wx.TextCtrls in my wx.Notebook do not work. The following code is from my main.py file where I create my wx.Notebook and add a page:

main.py

import wx
from LoginPanel import LoginPanel
from UserView import UserView

class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title=title, size=(850,725))

        # Creating Panels
        self.main = wx.Panel(self)
        self.mainLogin = LoginPanel(self.main,-1,addSpacers=1)

        #start off showing the login panel
        self.mainLogin.Show()


        # Create a notebook on the panel
        self.nb = wx.Notebook(self.main, 1)
        #Start off hiding the Notebook until successfully logged in
        self.nb.Hide()

        # create the page windows as children of the notebook
        self.userAdmin = UserView(parent=self.nb, ID=-1)

        # add the pages to the notebook with the label to show on the tab
        self.nb.AddPage(self.userAdmin, "Users")

        # Bindings
        self.Bind(wx.EVT_BUTTON, self.EvtLoginBtn, self.mainLogin.loginBtns.LoginBtn)
        self.Bind(wx.EVT_BUTTON, self.EvtLogoutBtn, self.mainLogin.loginBtns.LogoutBtn)

        # Create sizers
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        # Adding Objects to mainSizer
        self.mainSizer.AddSpacer(10)
        self.mainSizer.Add(self.mainLogin, 1, wx.ALL|wx.EXPAND)
        self.mainSizer.Add(self.nb, 1, wx.ALL|wx.EXPAND)

        # Set main sizer
        self.main.SetAutoLayout(True)
        self.main.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self.main)
        self.Layout()
        self.Centre(wx.BOTH)
        self.Show()

    def EvtLoginBtn(self,e):
        self.nb.Show()
        self.mainLogin.Hide()
        print("Login clicked")
        self.mainSizer.Layout()

    def EvtLogoutBtn(self,e):
        print("Logout clicked")

app = wx.App(False)
frame = MyFrame(None, -1, 'App UI')
app.MainLoop()

LoginPanel.py

import wx
from OneLblOneSingleTxt_HorzBoxSizer_Panel import OneLblOneSingleTxt_HorzBoxSizer_Panel
from LoginBtnsPanel import LoginBtnsPanel

class LoginPanel(wx.Panel):
    def __init__(self, parent, ID, addSpacers):
        wx.Panel.__init__(self, parent, ID)

        sizer = wx.BoxSizer(wx.VERTICAL)

        self.userNamePnl = OneLblOneSingleTxt_HorzBoxSizer_Panel(self,-1,
                       name="loginUser", lbl="Username: ", addSpacers=1)
        self.passwordPnl = OneLblOneSingleTxt_HorzBoxSizer_Panel(self,-1,
                       name="loginPass", lbl="Password: ", addSpacers=1)
        self.loginBtns = LoginBtnsPanel(self,-1)


        if addSpacers == 1:
            sizer.AddStretchSpacer()

        sizer.Add(self.userNamePnl,0,wx.EXPAND)
        sizer.AddSpacer(10)
        sizer.Add(self.passwordPnl,0,wx.EXPAND)
        sizer.AddSpacer(10)
        sizer.Add(self.loginBtns,0,wx.EXPAND)


        if addSpacers == 1:
            sizer.AddStretchSpacer()

        self.SetAutoLayout(True)
        self.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()

OneLblOneSingleTxt_HorzBoxSizer_Panel.py

import wx

class OneLblOneSingleTxt_HorzBoxSizer_Panel(wx.Panel):
    def __init__(self, parent, ID, name, lbl, addSpacers):
        wx.Panel.__init__(self, parent, ID)
        sizer = wx.BoxSizer(wx.HORIZONTAL)

        lbl = wx.StaticText(self, label=lbl)
        self.singleTxt = wx.TextCtrl(self, size=(140,-1))

        if addSpacers==1:
            sizer.AddStretchSpacer()

        sizer.Add(lbl,0,wx.EXPAND)
        sizer.AddSpacer(10)
        sizer.Add(self.singleTxt,1,wx.EXPAND)
        sizer.AddStretchSpacer()

        if addSpacers==1:
            sizer.AddStretchSpacer()


        self.SetAutoLayout(True)
        self.SetSizer(sizer)
        sizer.Fit(self)

LoginBtnsPanel.py

import wx

class LoginBtnsPanel(wx.Panel):
    def __init__(self, parent, ID):
        wx.Panel.__init__(self, parent, ID)

        sizer = wx.BoxSizer(wx.HORIZONTAL)

        self.LoginBtn =wx.Button(self, label='Login')
        self.LogoutBtn =wx.Button(self, label='Logout')

        sizer.AddStretchSpacer()
        sizer.Add(self.LoginBtn,1,wx.EXPAND)
        sizer.AddSpacer(10)
        sizer.Add(self.LogoutBtn,1,wx.EXPAND)
        sizer.AddStretchSpacer()

        self.SetAutoLayout(True)
        self.SetSizer(sizer)
        sizer.Fit(self)

UserView.py

import wx
from UserTable import UserGridTable

class UserView(wx.Panel):
    def __init__(self, parent, ID):
        wx.Panel.__init__(self, parent, ID)
        self.sizer = wx.BoxSizer(wx.VERTICAL)

        self.gridPnl = wx.Panel(self,-1)
        self.gridPnlSizer = wx.BoxSizer(wx.HORIZONTAL)

        #Grid that show current users
        self.UserGrid = UserGridTable(self.gridPnl)

        #Start out showing this Grid until the Add button or Edit button is clicked
        self.UserGrid.Show()


        self.userEntryPnl = wx.Panel(self,0)
        self.userEntryPnlSizer = wx.BoxSizer(wx.VERTICAL)

        self.userlbl = wx.StaticText(self.userEntryPnl, label='Username')
        self.userTxt = wx.TextCtrl(self.userEntryPnl, size=(140,-1))
        self.userTxt.SetModified(True)
        self.userTxt.SetEditable(True)
        self.passlbl = wx.StaticText(self.userEntryPnl, label='Password')
        self.passTxt = wx.TextCtrl(self.userEntryPnl, size=(140,-1))
        self.passTxt.SetModified(True)
        self.passTxt.SetEditable(True)

        self.userEntryPnlSizer.AddStretchSpacer()
        self.userEntryPnlSizer.Add(self.userlbl,0,wx.EXPAND)
        self.userEntryPnlSizer.AddSpacer(10)
        self.userEntryPnlSizer.Add(self.userTxt,0,wx.EXPAND)
        self.userEntryPnlSizer.AddSpacer(10)
        self.userEntryPnlSizer.Add(self.passlbl,0,wx.EXPAND)
        self.userEntryPnlSizer.AddSpacer(10)
        self.userEntryPnlSizer.Add(self.passTxt,0,wx.EXPAND)
        self.userEntryPnlSizer.AddStretchSpacer()

        self.userEntryPnl.SetAutoLayout(True)
        self.userEntryPnl.SetSizer(self.userEntryPnlSizer)
        self.userEntryPnlSizer.Fit(self.userEntryPnl)

        #start out hiding this panel until Add button or Edit button is clicked
        self.userEntryPnl.Hide()

        self.gridPnlSizer.AddStretchSpacer()
        self.gridPnlSizer.Add(self.UserGrid,0,wx.EXPAND)
        self.gridPnlSizer.Add(self.userEntryPnl,0,wx.EXPAND)
        self.gridPnlSizer.AddStretchSpacer()

        self.gridPnl.SetAutoLayout(True)
        self.gridPnl.SetSizer(self.gridPnlSizer)
        self.gridPnlSizer.Fit(self.gridPnl)


        self.bottomBtnsPnl = wx.Panel(self,-1)
        self.bottomSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.AddBtn = wx.Button(self.bottomBtnsPnl, label='Add')
        self.EditBtn = wx.Button(self.bottomBtnsPnl, label='Edit')
        self.DeleteBtn = wx.Button(self.bottomBtnsPnl, label='Delete')
        self.SaveBtn = wx.Button(self.bottomBtnsPnl, label='Save')

        self.AddBtn.Show()
        self.EditBtn.Show()
        self.DeleteBtn.Show()
        self.SaveBtn.Hide()

        self.Bind(wx.EVT_BUTTON, self.addBtnEnt, self.AddBtn)
        self.Bind(wx.EVT_BUTTON, self.editBtnEnt, self.EditBtn)
        self.Bind(wx.EVT_BUTTON, self.deleteBtnEnt, self.DeleteBtn)
        self.Bind(wx.EVT_BUTTON, self.saveBtnEnt, self.SaveBtn)


        self.bottomSizer.AddStretchSpacer()
        self.bottomSizer.Add(self.AddBtn,0,wx.EXPAND)
        self.bottomSizer.AddSpacer(10)
        self.bottomSizer.Add(self.EditBtn,0,wx.EXPAND)
        self.bottomSizer.AddSpacer(10)
        self.bottomSizer.Add(self.DeleteBtn,0,wx.EXPAND)
        self.bottomSizer.AddSpacer(10)
        self.bottomSizer.Add(self.SaveBtn,0,wx.EXPAND)
        self.bottomSizer.AddSpacer(10)
        self.bottomSizer.AddStretchSpacer()

        self.bottomBtnsPnl.SetAutoLayout(True)
        self.bottomBtnsPnl.SetSizer(self.bottomSizer)
        self.bottomSizer.Fit(self.bottomBtnsPnl)

        self.sizer.AddSpacer(10)
        self.sizer.Add(self.gridPnl,0,wx.EXPAND)
        self.sizer.AddSpacer(10)
        self.sizer.Add(self.bottomBtnsPnl,0,wx.EXPAND)
        self.sizer.AddSpacer(10)

        self.SetAutoLayout(True)
        self.SetSizer(self.sizer)
        self.sizer.Fit(self)

    def addBtnEnt(self,e):
        self.UserGrid.Hide()
        self.AddBtn.Hide()
        self.EditBtn.Hide()
        self.DeleteBtn.Hide()
        self.SaveBtn.Show()
        self.userEntryPnl.Show()
        self.userEntryPnl.Layout()

    def editBtnEnt(self,e):
        self.UserGrid.Hide()
        self.AddBtn.Hide()
        self.EditBtn.Hide()
        self.DeleteBtn.Hide()
        self.SaveBtn.Show()
        self.userEntryPnl.Show()
        self.userEntryPnl.Layout()

    def deleteBtnEnt(self,e):
        print("Delete clicked")

    def saveBtnEnt(self,e):
        self.userEntryPnl.Hide()
        self.SaveBtn.Hide()
        self.UserGrid.Show()
        self.AddBtn.Show()
        self.EditBtn.Show()
        self.DeleteBtn.Show()
        self.userEntryPnl.Layout()

UserTable.py

import wx
import wx.grid

class UserGridTable(wx.grid.Grid):
    def __init__(self, parent):
        wx.grid.Grid.__init__(self, parent)
        self.CreateGrid(6,3)
        self.SetColLabelValue(0, "ID")
        self.SetColLabelValue(1, "Username")
        self.SetColLabelValue(2, "Password")

        self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.EmpCellLeftClick, self)

    def EmpCellLeftClick(self,e):
        self.SelectRow(e.GetRow())

How do I get self.userTxt and self.passTxt to be editable where I can enter text in and select it? Thank you for any and all help!


UPDATE:


The above code is a working on Python 2.7.8


Solution

  • As a quick fix, change

    self.userEntryPnl = wx.Panel(self, 0)
    

    To

    self.userEntryPnl = wx.Panel(self.gridPnl, -1)
    

    gridPnl was on top of userEntryPnl although userEntryPnl was visible. Because of that you couldn't edit what was underneath gridPnl.