wxpython复选框标签未完全显示,某些字母被其他字母隐藏

时间:2018-12-19 16:26:17

标签: python user-interface wxpython wxwidgets wxpython-phoenix

我有很多复选框,它们的标签长度​​不同,当我使用网格化程序显示它们时,标签没有完全显示出来。我发现,随着复选框数量的增加,对话框的大小保持不变。 如何显示所有复选框和标签?下面是代码和图像:

# -*- coding: utf-8 -*-
import wx
import math


class Example(wx.Dialog):
    def __init__(self, cb_titles, title):
        super().__init__(None, title=title)
        self.cb_labels = cb_titles
        self.panel = wx.Panel(self)
        self.initUI()
        self.Centre()
        self.ShowModal()

    def initUI(self):
        vbox = wx.BoxSizer(wx.VERTICAL)  # I use vbox here because I need to add other items below this grid sizer, not shown in the example.
        self.set_grid(vbox)
        border = wx.BoxSizer()
        border.Add(vbox, 1, wx.ALL | wx.EXPAND, 10)
        self.panel.SetSizer(border)
        self.panel.Fit()

    def set_grid(self, vbox):
        n_cbs = len(self.cb_labels)
        n_col = int(math.sqrt(n_cbs))
        n_row = n_col
        while n_row * n_col < n_cbs:
            n_row += 1
        sb = wx.StaticBox(self.panel, -1, 'CheckBoxes:')
        sb_sizer = wx.StaticBoxSizer(sb, wx.VERTICAL)
        grid_sizer = wx.GridSizer(n_row, n_col, 30)

        for i in range(n_cbs):
            cb_label = self.cb_labels[i]
            cb = wx.CheckBox(self.panel, label=cb_label)
            grid_sizer.Add(cb, 1, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)
        sb_sizer.Add(grid_sizer, 1, wx.ALL|wx.CENTER, 10)
        vbox.Add(sb_sizer, 1, wx.CENTER, 0)





app = wx.App()
cb_titles = ["aaaaaaaaaaa", "bbbbbbb", "cccccccccc", "ddddddd", "eeeeeeee", "ffffffffff", "DDDDDDDDDD", 'FFFFFFFFFFFFFF', "GGGGGGGGGG"] * 3  # you can change this number
Example(cb_titles, title ='Question Example')
app.MainLoop()

man_with_phone

我想使用对话框小部件,您可以在图片中看到标签没有完全显示出来。 此外,当我单击十字按钮时,对话框关闭,但程序没有结束,如何正确结束它?

1 个答案:

答案 0 :(得分:2)

我认为您将不得不根据最长的标签长度​​来计算和设置对话框的大小(我可能错了)。

列表中最长的字符串可以找到:

max(a_list,key=len)

您还必须找到所使用字符的磅数。

这是我对代码的第一次尝试,希望您会发现它不是您想要的那样有用。

# -*- coding: utf-8 -*-
import wx
import math


class Example(wx.Dialog):
    def __init__(self, cb_titles, title):
        super().__init__(None, title=title)
        self.cb_labels = cb_titles
        self.panel = wx.Panel(self)
        self.initUI()
        self.Centre()

    def initUI(self):
        vbox = wx.BoxSizer(wx.VERTICAL)  # I use vbox here because I need to add other items below this grid sizer, not shown in the example.
        self.set_grid(vbox)
        border = wx.BoxSizer()
        border.Add(vbox, 1, wx.ALL | wx.EXPAND, 10)
        self.panel.SetSizer(border)
        self.panel.Fit()

    def set_grid(self, vbox):
        n_cbs = len(self.cb_labels)
        n_col = int(math.sqrt(n_cbs))
        n_row = n_col
        while n_row * n_col < n_cbs:
            n_row += 1
        sb = wx.StaticBox(self.panel, -1, 'CheckBoxes:')
        sb_sizer = wx.StaticBoxSizer(sb, wx.VERTICAL)
        grid_sizer = wx.GridSizer(n_row, n_col, 30)

        for i in range(n_cbs):
            cb_label = self.cb_labels[i]
            cb = wx.CheckBox(self.panel, label=cb_label)
            grid_sizer.Add(cb, 1, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)
        sb_sizer.Add(grid_sizer, 1, wx.ALL|wx.CENTER, 10)
        vbox.Add(sb_sizer, 1, wx.CENTER, 0)

        #Set the size based on the number of columns and
        #the length of the longest label + margins
        self.font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
        pt = self.font.GetPointSize()
        w=(len(max(self.cb_labels,key=len))*n_col)+(pt*(n_col - 1))
        w=(w*pt)
        self.SetSize(w,-1)

app = wx.App()
cb_titles = ["aaaaaaaaaaa", "bbbbbbb", "cccccccccc", "ddddddd", "eeeeeeee", "ffffffffff", "DDDDDDDDDD", 'FFFFFFFFFFFFFF', "GGGGGGGGGG"] * 5  # you can change this number

dlg = Example(cb_titles, title ='Question Example')
dlg.ShowModal()
dlg.Destroy()
app.MainLoop()

最后,将ShowModal()移出对话框并将其放在外面,以便可以使用Destroy()函数,它将正确结束对话框。
您可以将ShowModal留在对话框中,但随后也必须在其中编码Destroy

enter image description here