Grid sizer静态文本编辑

时间:2014-07-23 07:02:35

标签: wxpython

我在分割窗口中有7列超过10行的grid_sizer。所有行值都以静态文本的形式通过列表插入。我想更改我单击其单选按钮的行的静态文本。我能够从这个thread的单行完成它。现在我想编辑例如第7行的静态文本。有关如何使用grid sizer设置特定行标签的任何建议吗?

import wx
# I have these lists from a text file: lut_code, lut_type, 
#  lut_A, lut_B, lut_C, lut_D, condition2
lut_code = ['10', '11', '12', '13']
lut_type = ['Urban', 'Low Density Residential', 'Medium Density Residential', 'High Density Residential']
lut_A = ['61', '54', '61', '77']
lut_B = ['75', '70', '75', '85']
lut_C = ['83', '80', '83', '90']
lut_D = ['87', '85', '87', '92']

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, "test", wx.DefaultPosition, wx.Size(900, 600))
        splitter = wx.SplitterWindow(self, -1)
        scroll1 = wx.ScrolledWindow(splitter, -1)
        scroll1.SetBackgroundColour(wx.WHITE)
        scroll1.SetScrollRate(5,5)
        self.grid_sizer = wx.GridSizer( 0, 8, 0, 0 )

        self.head1 = wx.StaticText(scroll1, wx.ID_ANY, u"Code", (15,2))
        self.head1.Wrap( -1 )
        self.grid_sizer.Add( self.head1, 0, wx.ALL, 5 )

        self.head2 = wx.StaticText(scroll1, wx.ID_ANY, u"Classification", (80,2))
        self.head2.Wrap( -1 )
        self.grid_sizer.Add( self.head2, 0, wx.ALL, 5 )

        self.head3 = wx.StaticText(scroll1, wx.ID_ANY, u"A", (270,2))
        self.head3.Wrap( -1 )
        self.grid_sizer.Add( self.head3, 0, wx.ALL, 5 )

        self.head4 = wx.StaticText(scroll1, wx.ID_ANY, u"B",(320,2))
        self.head4.Wrap( -1 )
        self.grid_sizer.Add( self.head4, 0, wx.ALL, 5 )

        self.head5 = wx.StaticText(scroll1, wx.ID_ANY, u"C", (370,2))
        self.head5.Wrap( -1 )
        self.grid_sizer.Add( self.head5, 0, wx.ALL, 5 )

        self.head6 = wx.StaticText(scroll1, wx.ID_ANY, u"D", (420,2))
        self.head6.Wrap( -1 )
        self.grid_sizer.Add( self.head6, 0, wx.ALL, 5 )

        self.head7 = wx.StaticText(scroll1, wx.ID_ANY, u"Cond.", (470,2))
        self.head7.Wrap( -1 )
        self.grid_sizer.Add( self.head7, 0, wx.ALL, 5 )

        self.head8 = wx.StaticText(scroll1, wx.ID_ANY, u"Update", (565,2))
        self.head8.Wrap( -1 )
        self.grid_sizer.Add( self.head8, 0, wx.ALL, 5 )

        # specified headers (code, class, A, B, C, D, cond., update, condition2) using static text and grid sizer

        v = 0
        for lu,t,a,b,c,d,cond in zip(lut_code,lut_type,lut_A,lut_B,lut_C,lut_D,condition2):
            v += 39
            self.column11 = wx.StaticText(scroll1, wx.ID_ANY, str(lu), (15,v))
            self.column11.Wrap( -1 )
            self.grid_sizer.Add( self.column11, 0, wx.ALL, 5 )

            self.column12 = wx.StaticText(scroll1, wx.ID_ANY, str(t), (80,v))
            self.column12.Wrap( -1 )
            self.grid_sizer.Add( self.column12, 0, wx.ALL, 5 )

            self.column13 = wx.StaticText(scroll1, wx.ID_ANY, str(a), (270,v))
            self.column13.Wrap( -1 )
            self.grid_sizer.Add( self.column13, 0, wx.ALL, 5 )

            self.column14 = wx.StaticText(scroll1, wx.ID_ANY, str(b), (320,v))
            self.column14.Wrap( -1 )
            self.grid_sizer.Add( self.column14, 0, wx.ALL, 5 )

            self.column15 = wx.StaticText(scroll1, wx.ID_ANY, str(c), (370,v))
            self.column15.Wrap( -1 )
            self.grid_sizer.Add( self.column15, 0, wx.ALL, 5 )

            self.column16 = wx.StaticText(scroll1, wx.ID_ANY, str(d), (420,v))
            self.column16.Wrap( -1 )
            self.grid_sizer.Add( self.column16, 0, wx.ALL, 5 )

            self.column17 = wx.StaticText(scroll1, wx.ID_ANY, str(cond), (480,v))
            self.column17.Wrap( -1 )
            self.grid_sizer.Add( self.column17, 0, wx.ALL, 5 )

        # I have added radiobuttons as follows using "rb_list" (which I created using length of "lut_code")

        rb_list = [20]
        r = 18
        for i in range(1,len(lut_code)):
            r += 39
            rb_list.append(r)

        radio1Choices = ['G','F','P']     
        self.radiobuttonlist = []
        for i in rb_list:
            self.radiobuttonlist.append(wx.RadioBox(scroll1, wx.ID_ANY, wx.EmptyString, (550,i), (30,5), radio1Choices, 3, wx.RA_SPECIFY_COLS | wx.NO_BORDER))
            self.radiobuttonlist[-1].SetSelection(2)
            self.radiobuttonlist[-1].my_identity = i                                                                                        
            self.Bind(wx.EVT_RADIOBOX,self.Getvalues,id=self.radiobuttonlist[-1].GetId())
            self.grid_sizer.Add(self.radiobuttonlist[-1], 0, wx.ALL, 5)

        # added buttons in scroll2
        wx.StaticText(scroll2, -1,message, (15,150))
        splitter.SplitVertically(scroll1,scroll2,-220)

        self.btnCancel = wx.Button(scroll2, label="Cancel and Close", pos=(50,30))
        self.Bind(wx.EVT_BUTTON, self.OnClose, id = self.btnCancel.GetId())

        self.btnCancel = wx.Button(scroll2, label="Update and Close", pos=(50,80))
        self.Bind(wx.EVT_BUTTON, self.OnClose, id = self.btnCancel.GetId())

        self.btnCancel = wx.Button(scroll2, label="Set All to "'Good'"", pos=(50,300))
        self.Bind(wx.EVT_BUTTON, self.OnClose, id = self.btnCancel.GetId())

        self.btnCancel = wx.Button(scroll2, label="Set All to "'Fair'"", pos=(50,350))
        self.Bind(wx.EVT_BUTTON, self.OnClose, id = self.btnCancel.GetId())

        self.btnCancel = wx.Button(scroll2, label="Set All to "'Poor'"", pos=(50,400))
        self.Bind(wx.EVT_BUTTON, self.OnClose, id = self.btnCancel.GetId())

        self.btnCancel = wx.Button(scroll2, label="Write LookUp Table to File", pos=(10,450))
        self.Bind(wx.EVT_BUTTON, self.OnClose, id = self.btnCancel.GetId())
        scroll1.SetVirtualSize(scroll1.GetBestSize())

    def OnClose(self, event):
        self.Show(False)

    def Getvalues(self,event):
        r = 7 #row number
        c = 4 # number of columns in the grid
        texts = self.grid_sizer.GetChildren()[c*(r-1) : c*r - 1]  # returns a list of widgets inside the sizer
        for statictext in texts:
            if isinstance(statictext.GetWindow(), wx.StaticText):  # checks if it is a statictext
                statictext.GetWindow.SetLabel('data')              # sets the new data  

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'splitterwindow.py')
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

app = MyApp(0)
app.MainLoop()

1 个答案:

答案 0 :(得分:0)

由于每行有4列,因此您可以确定第7行中的wx.staticTexts将从第24个wx.StaticText个对象(从0开始)到27。

您可以使用类似于以下内容的方式设置StaticTexts的标签:

r = #row number
c = 4 # number of columns in the grid
texts = gridsizer.GetChildren()[c*(r-1) : c*r - 1]  # returns a list of widgets inside the sizer
for statictext in texts:
    if isinstance(statictext.GetWindow(), wx.StaticText):  # checks if it is a statictext
        statictext.GetWindow.SetLabel('data')              # sets the new data

一些解释here