wxPython 中的单选按钮显示和选择问题 [英] Radio button display and selection issue in wxPython

查看:30
本文介绍了wxPython 中的单选按钮显示和选择问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建长度相等的多列列表,并生成与列表长度相等的单选按钮数量.我有几个问题:

I am creating multi-column lists which are all equal in length and also generating number of radio buttons equal to the length of list. I have couple of issues:

1] 显示问题:在下图中,我得到单选按钮.

1] Display issue: In following fig., I get radio buttons.

但是当我向下滚动时,就会发生这种情况.

But when I scroll down this is what happens.

以下是我生成它的代码片段,请帮助我解决此问题,以便我正确显示完整的单选按钮列表.

Following is a snippet of my code to generate it Please assist me to fix this issue so that I get full list of radio buttons displayed properly.

w = 0
for i in range(1,len(lut_code)):
    w += 30
    rb_G = wx.RadioButton(scroll1, -1, "G", (500,w), style=wx.RB_GROUP)
    rb_F = wx.RadioButton(scroll1, -1, "F", (540,w))
    rb_P = wx.RadioButton(scroll1, -1, "P", (580,w))

2] 单选按钮的选择:当我想从一行中选择一个单选按钮时,会选择一个完整的行,它会变成蓝色,如下图所示.

2] Selection of radio button: When I want to select a single radio button from a row, a complete row is selected instead and it gets blue in color like in following fig.

是不是因为我用wx.ListCtrl来显示这些列?仅选择选择的单选按钮而不是选择整行的修复或替代方法是什么?

Is it because of my use of wx.ListCtrl to display these columns? What would be the fix or alternative method to only select radio button of choice instead of selecting whole row?

推荐答案

我花了一些时间来解决这个问题.我认为您实际上是通过指定位置在框架上绘制单选按钮!在这种情况下这显然不起作用,因为当您尝试选择单选按钮时,您的 listCtrl 会出现在前台.

Took me some time to figure this out. I think you are actually drawing the radio buttons on the frame by specifying the position! This will obviously not work in this case because your listCtrl comes to the foreground when you try to select a radio button.

如果您发现单选按钮未根据列表项正确对齐.按钮之间的间隙大于列表行之间的间隙.

If you notice the radio buttons are not correctly aligned according to the list items. The gap between the buttons is greater than the gap between rows of the list.

使用 list_ctrl 的问题是您不能将单选按钮之类的小部件放入其中.

The problem with using list_ctrl is you cannot put widgets like radio buttons in them.

我建议您为此使用 wx.ScrolledWindow 之类的东西.将每一行放入一个 sizer 并垂直堆叠这些 sizer.

I would suggest you to use something like wx.ScrolledWindow for this. Put each row into a sizer and stack these sizers vertically.

更新:

import wx

class Frame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Test", pos = wx.DefaultPosition, size = wx.Size( -1,-1 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        sizer0 = wx.BoxSizer( wx.VERTICAL )

        self.scrolledWindow = wx.ScrolledWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
        self.scrolledWindow.SetScrollRate( 5, 5 )
        grid_sizer = wx.GridSizer( 0, 8, 0, 0 )

        self.head1 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Code", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.head1.Wrap( -1 )
        grid_sizer.Add( self.head1, 0, wx.ALL, 5 )

        self.head2 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Classification", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.head2.Wrap( -1 )
        grid_sizer.Add( self.head2, 0, wx.ALL, 5 )

        self.head3 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"A", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.head3.Wrap( -1 )
        grid_sizer.Add( self.head3, 0, wx.ALL, 5 )

        self.head4 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"B", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.head4.Wrap( -1 )
        grid_sizer.Add( self.head4, 0, wx.ALL, 5 )

        self.head5 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"C", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.head5.Wrap( -1 )
        grid_sizer.Add( self.head5, 0, wx.ALL, 5 )

        self.head6 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"D", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.head6.Wrap( -1 )
        grid_sizer.Add( self.head6, 0, wx.ALL, 5 )

        self.head7 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Cond.", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.head7.Wrap( -1 )
        grid_sizer.Add( self.head7, 0, wx.ALL, 5 )

        self.head8 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Update", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.head8.Wrap( -1 )
        grid_sizer.Add( self.head8, 0, wx.ALL, 5 )

        self.column11 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"86", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.column11.Wrap( -1 )
        grid_sizer.Add( self.column11, 0, wx.ALL, 5 )

        self.column12 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"Urban", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.column12.Wrap( -1 )
        grid_sizer.Add( self.column12, 0, wx.ALL, 5 )

        self.column13 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"68", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.column13.Wrap( -1 )
        grid_sizer.Add( self.column13, 0, wx.ALL, 5 )

        self.column14 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"80", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.column14.Wrap( -1 )
        grid_sizer.Add( self.column14, 0, wx.ALL, 5 )

        self.column15 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"86", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.column15.Wrap( -1 )
        grid_sizer.Add( self.column15, 0, wx.ALL, 5 )

        self.column16 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, u"89", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.column16.Wrap( -1 )
        grid_sizer.Add( self.column16, 0, wx.ALL, 5 )

        self.column17 = wx.StaticText( self.scrolledWindow, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.column17.Wrap( -1 )
        grid_sizer.Add( self.column17, 0, wx.ALL, 5 )

        radio1Choices = [ u"G", u"P", u"F" ]
        self.radio1 = wx.RadioBox( self.scrolledWindow, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, radio1Choices, 3, wx.RA_SPECIFY_COLS )
        self.radio1.SetSelection( 0 )
        grid_sizer.Add( self.radio1, 0, wx.ALL, 5 )


        self.scrolledWindow.SetSizer( grid_sizer )
        self.scrolledWindow.Layout()
        grid_sizer.Fit( self.scrolledWindow )
        sizer0.Add( self.scrolledWindow, 1, wx.EXPAND |wx.ALL, 5 )

        # Bind the radio box select event to a function
        self.radio1.Bind( wx.EVT_RADIOBOX, self.on_selected )


        self.SetSizer( sizer0 )
        self.Layout()
        self.Maximize()
        self.Centre( wx.BOTH )
        self.Show()

    def on_selected(self, event):
        # Depending upon the option selected the values of A,B,C,D are changed
        if  self.radio1.GetStringSelection() == 'P': 
            self.column13.SetLabel('25')
            self.column14.SetLabel('27')
            self.column15.SetLabel('34')
            self.column16.SetLabel('12')
        elif self.radio1.GetStringSelection() == 'F':
            self.column13.SetLabel('56')
            self.column14.SetLabel('70')
            self.column15.SetLabel('49')
            self.column16.SetLabel('54')
        else:
            self.column13.SetLabel('78')
            self.column14.SetLabel('83')
            self.column15.SetLabel('69')
            self.column16.SetLabel('100')


if __name__ == "__main__":
        app = wx.App()
        Frame(None)
        app.MainLoop()

我编写此示例代码是为了向您展示如何做到这一点.这有点粗糙,但我希望你能看懂.

I wrote this sample code to show you how you can do it. It's a bit crude but I hope you get the picture.

您可能还想查看这篇文章中的答案:为ListCtrl WxPython 中的每一行添加一个按钮.这可能是更好的选择.

You might also want to check the answer in this post:Adding a button to every row in ListCtrl WxPython. It might be a better choice.

这篇关于wxPython 中的单选按钮显示和选择问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆