如何在UltimateListCtrl中对齐单选按钮,以及如何将选中的按钮放入字典中?

时间:2018-08-29 12:11:04

标签: wxpython

我是wxpython的新手,并且在Windows 10上使用Python 2.7.10,wxpython 4.0.3和Ultimatelistctrl 0.8。
我目前正在尝试创建一个显示项目列表的GUI。用户必须为每个项目选择方法A或方法B。完成此操作后,用户按下按钮,将具有所选方法的项目名称写入字典。我遇到两个问题:
问题1-我希望单选按钮位于列的中心,而不是右对齐。我试过了InsertColumn和InsertColumnInfo,但它仅对齐标题,而不对齐单选按钮。这比主要问题更令人烦恼,但对此我将不胜感激。
问题2-我完全迷住了如何将用户做出的选择放入词典中。
这是我当前的代码:  

import wx
from wx.lib.agw import ultimatelistctrl as ULC

class TestPanel(wx.Panel):
""""""
def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent)

    self.ultimateList = ULC.UltimateListCtrl(self, agwStyle = ULC.ULC_HAS_VARIABLE_ROW_HEIGHT | wx.LC_REPORT| wx.LC_VRULES | wx.LC_HRULES)

    # At first I used InsertColumn. It works, but the radio buttons are right aligned.
    # self.ultimateList.InsertColumn(0,"Category name",format=0)
    # self.ultimateList.InsertColumn(1,"Method A",format=2)
    # self.ultimateList.InsertColumn(2,"Method B",format=2)

    # Then I tried UltimateListItem instead, but the radio buttons are still right aligned.
    info = ULC.UltimateListItem()
    info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_FORMAT
    info._text = "Category Name"
    info._format = wx.LIST_FORMAT_LEFT
    self.ultimateList.InsertColumnInfo(0, info)

    info = ULC.UltimateListItem()
    info._mask = wx.LIST_MASK_TEXT |wx.LIST_MASK_FORMAT
    info._text = "Method A"
    info._format = wx.LIST_FORMAT_CENTRE
    self.ultimateList.InsertColumnInfo(1, info)

    info = ULC.UltimateListItem()
    info._mask = wx.LIST_MASK_TEXT |wx.LIST_MASK_FORMAT
    info._text = "Method B"
    # info._format = wx.LIST_FORMAT_CENTRE
    info.SetAlign(ULC.ULC_FORMAT_CENTRE) # I thought this might work instead of _format
    self.ultimateList.InsertColumnInfo(2, info)

    self.ultimateList.SetColumnWidth(0,ULC.ULC_AUTOSIZE_FILL)
    self.ultimateList.SetColumnWidth(1,ULC.ULC_AUTOSIZE_USEHEADER)
    self.ultimateList.SetColumnWidth(2,ULC.ULC_AUTOSIZE_USEHEADER)

    getsel_btn = wx.Button(self, label='Get Selection')
    getsel_btn.Bind(wx.EVT_BUTTON, self.onGetSel)

    # the items in myCategories will vary depending on another user input
    # For the purpose of this example I've hardcoded a list
    myCategories= ["Red","Blue","Green","Yellow","Purple","Something Else"]

    for item in range(len(myCategories)):
        self.ultimateList.InsertStringItem(item, str(myCategories[item]))
        self.radBt1 = wx.RadioButton(self.ultimateList, wx.ID_ANY, "", style=wx.RB_GROUP)
        self.ultimateList.SetItemWindow(item,1,self.radBt1)
        self.radBt2 = wx.RadioButton(self.ultimateList, wx.ID_ANY, "", style=0)
        self.ultimateList.SetItemWindow(item,2,self.radBt2)

    self.ultimateList.Bind(ULC.EVT_LIST_ITEM_CHECKED,self.onSel)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self.ultimateList, 1, wx.EXPAND)
    sizer.Add(getsel_btn, 0, wx.ALL|wx.CENTER, 5)
    self.SetSizer(sizer)

def onSel(self, event):
    """selected method"""
    # I can't work out how to get this information into here

def onGetSel(self, event):
    """save category name and method to dictionary"""
    results={}
    # in results the keys should be set to the category name and the values
    # should be set to A or B, depending on the radio button selected
    print results

#-------------------------------------------------------------------------
class TestFrame(wx.Frame):
""""""

def __init__(self):
    """Constructor"""
    wx.Frame.__init__(self, None, title="Example")
    panel = TestPanel(self)
    self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = TestFrame()
    app.MainLoop()

0 个答案:

没有答案