Accessing wx.TextCtrl after refactoring

alex alecla at bluewin.ch
Fri Mar 27 22:51:19 CET 2009


Hi all
I am working on a Dialog window for a gui in wxPython and started
refactoring it, below code is a simplified version of it.
"def createInput1" should create a static text, a button and a
textcontrol using the information in "def box1Labels".
"def makeStaticBox1" then arranges all widgets in staticbox1.

My problem is that it works fine for the static text and the button
label but I cant "rename" self.txtctrl that it
becomes self.txtctrl_inpath or self.txtctrl_outpath for getting the
path from either "def BrowseInDlg" or "def BrowseOutDlg".

I must admit that I am still a beginner but searching through my books
does not get me further. I like the idea of refactoring for
"automatising" widgets creation instead of hardcoding each widgetID.
But I am completely stuck here. Maybe can somebody help?

Coming from the Fortran world my programming style may be quite
"fortranic" instead of being "pythonic" but I am trying to improve and
enjoy what I learned so far...

Thanks Alex



#
#
#!/usr/bin/env python
#
#
"""Add Python docs string"""
import wx
import os
#
#
class Dialog1(wx.Dialog):
    def __init__(self):
        wx.Dialog.__init__(self, None, -1, "Test", size=(500, 600))
        self.makeStaticBox1()
        self.makeSizer()


    def box1Labels(self):
        return (("Label1:", "Browse", self.BrowseInDlg,
txtctrl_inpath),
                ("Label2:", "Browse", self.BrowseOutDlg,
txtctrl_outpath))


    def makeStaticBox1(self):
        box1 = wx.StaticBox(self, -1, "Box1")
        pathFlexGridSizer = wx.FlexGridSizer(4, 0, 0, 0)
        pathFlexGridSizer.AddGrowableCol(1)

        for label, pth_btn_label, btn_funct, txtctrl_path in
self.box1Labels():
            self.createInput1(label, pth_btn_label, txtctrl_path)

            pathFlexGridSizer.Add(self.stattxt, 0,
wx.ALIGN_CENTER_VERTICAL|wx.TOP|wx.BOTTOM, 2)
            pathFlexGridSizer.Add((10, 10))
            pathFlexGridSizer.Add(self.pth_Btn, 0, wx.ALIGN_LEFT)
            pathFlexGridSizer.Add(self.txtctrl, 0, wx.ALIGN_RIGHT|
wx.TOP|wx.BOTTOM, 2)
            self.Bind(wx.EVT_BUTTON, btn_funct, self.pth_Btn)


        self.path_sizer = wx.StaticBoxSizer(box1, wx.VERTICAL)
        self.path_sizer.Add(pathFlexGridSizer, 2, wx.ALL|wx.EXPAND, 0)


    def createInput1(self, label, pth_btn_label, txtctrl_path):
        self.stattxt=wx.StaticText(self, -1, label)
        self.pth_Btn = wx.Button(self, -1, pth_btn_label)
        self.txtctrl=wx.TextCtrl(self, -1, "", size=(300, -1))




    def makeSizer(self):
        GridSizer = wx.BoxSizer(wx.VERTICAL)
        GridSizer.Add(self.path_sizer, 1, wx.ALL|wx.EXPAND, 2)


        self.SetSizer(GridSizer)
        self.Fit()
        self.Centre()
        self.Show(True)


    def BrowseInDlg(self, event):
#
        dialog = wx.DirDialog(None, "Choose directory:",
style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dialog.ShowModal() == wx.ID_OK:
            inpath=dialog.GetPath()
            self.txtctrl_inpath.SetValue(inpath)
            print inpath
        dialog.Destroy()


    def BrowseOutDlg(self, event):
#
        dialog = wx.DirDialog(None, "Choose directory:",
style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dialog.ShowModal() == wx.ID_OK:
            outpath=dialog.GetPath()
            self.txtctrl_outpath.SetValue(outpath)
            print outpath
        dialog.Destroy()







More information about the Python-list mailing list