wxPython wx.TextCtrl不可编辑



在我的wxPython应用程序中,我有几个wx。TextCtrl为表单输入字段。我的问题是,一些wx。TextCtrl输入字段是不可编辑的,这意味着我不能在字段中键入,也不能选择字段中的文本。我的父母。面板在我的main.py文件有两个子登录wx。面板和笔记本。的天气。textctrl在我的登录工作很好,但wx。textctrl在我的wx。笔记本不工作。下面的代码来自我创建wx的main.py文件。笔记本并添加一个页面:

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())

如何得到self。userTxt和self。passstext是可编辑的,我可以在其中输入文本并选择它?谢谢你的任何和所有的帮助!


更新:


以上代码是在Python 2.7.8上运行的

作为快速修复,更改

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

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

gridPnl位于userEntryPnl之上,尽管userEntryPnl是可见的。因为你不能编辑gridPnl下面的内容

相关内容

  • 没有找到相关文章

最新更新