带有 HTML 的 wxPython CheckListBox [英] wxPython CheckListBox with HTML

查看:29
本文介绍了带有 HTML 的 wxPython CheckListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 wxPython 创建一个 GUI 应用程序.现在我使用 wx.CheckListBox 来显示带有复选框的选项,但我希望 CheckListBox 中的文本使用 HTML 进行格式化.解决此问题的最佳方法是什么?

I'm using wxPython to create a GUI app. Right now I'm using a wx.CheckListBox to display options with check boxes, but I'd like the text in the CheckListBox to be formatted using HTML. What's the best way to go about this?

推荐答案

wxCheckListBox 替换为 wxHtmlWindow 并使用 wxpTag 作为复选框.

Replace wxCheckListBox with wxHtmlWindow and use wxpTag for the check boxes.

这是一些帮助您入门的代码.

Here is some code to get you started.

import wx
import wx.lib.wxpTag


class HtmlCheckListBox(wx.html.HtmlWindow):
    def __init__(self, parent, choices=None):
        wx.html.HtmlWindow.__init__(self, parent)

        check_box = """
        <wxp module="wx" class="CheckBox">
            <param name="id" value="%d">
        </wxp>
        """

        self._ids = dict()

        if choices:
            items = list()
            for c, choice in enumerate(choices):
                i = wx.NewId()
                self._ids[i] = c
                items.append((check_box % i) + choice)
            self.SetPage("<hr>".join(items))

        self.Bind(wx.EVT_CHECKBOX, self.OnCheck)

    def OnCheck(self, event):
        print "item:", self._ids[event.Id], "checked:", event.Checked()


class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.options = HtmlCheckListBox(
            self,
            [
                "<i>one</i>",
                "<b>two</b>",
                "<u>three</u>"
            ]
        )


app = wx.PySimpleApp()
app.TopWindow = TestFrame()
app.TopWindow.Show()
app.MainLoop()

这篇关于带有 HTML 的 wxPython CheckListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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