在wx Python中对齐数字列

时间:2016-05-17 15:26:55

标签: python formatting wxpython

我试图在wx.Python GridBagSizer中正确对齐整数

我尝试对齐标签并格式化标签中的值,但没有效果

有人可以告诉我出错的地方

由于

    """
        module description
    """

    import wx
    import random


    class AlignNumbers(wx.Frame):
        """
            class description
        """
        def __init__(self):
            """
                initialise form
            """
            wx.Frame.__init__(self, None)
            self.Title = 'Align numbers'

            lbl_points = wx.StaticText(self, label='Points')
            lbl_first = wx.StaticText(self, label='First:')
            lbl_second = wx.StaticText(self, label='Second:')
            self.lbl_first_score = wx.StaticText(self, label='')
            self.lbl_second_score = wx.StaticText(self, label='')

            cmd_score = wx.Button(self, label='Score')
            cmd_score.Bind(wx.EVT_BUTTON, self.on_cmd_score_click)

            main_sizer = wx.GridBagSizer()
            main_sizer.Add(lbl_points, pos=(0, 1),
                        flag=wx.TOP | wx.LEFT | wx.ALIGN_RIGHT,
                        border=10)
            main_sizer.Add(lbl_first, pos=(1, 0),
                        flag=wx.TOP | wx.LEFT | wx.ALIGN_RIGHT,
                        border=10)
            main_sizer.Add(lbl_second, pos=(2, 0),
                        flag=wx.TOP | wx.LEFT | wx.ALIGN_RIGHT,
                        border=10)
            main_sizer.Add(self.lbl_first_score, pos=(1, 1),
                        flag=wx.TOP | wx.LEFT | wx.ALIGN_RIGHT,
                        border=10)
            main_sizer.Add(self.lbl_second_score, pos=(2, 1),
                        flag=wx.TOP | wx.LEFT | wx.ALIGN_RIGHT,
                        border=10)
            main_sizer.Add(cmd_score, pos=(3, 2),
                        flag=wx.TOP | wx.BOTTOM | wx.RIGHT | wx.ALIGN_RIGHT,
                        border=10)
            self.SetSizer(main_sizer)
            self.Layout()
            self.Fit()
            self.Centre()
            self.scores = [score for score in range(-20, 21)]

        def on_cmd_score_click(self, event):
            del event
            self.lbl_first_score.SetLabel('{}'.format(random.choice(self.scores)))
            self.lbl_second_score.SetLabel('{:4d}'.format(random.choice(self.scores)))

    if __name__ == '__main__':
        """
            initialise application
        """
        simple_screen_app = wx.App()
        main_frame = AlignNumbers()
        main_frame.Show(True)
        simpl

1 个答案:

答案 0 :(得分:1)

使用其他StaticText来显示您的输出,您可以格式化 我在下面使用了TextCtrl

import wx
import random


class AlignNumbers(wx.Frame):
    """
        class description
    """
    def __init__(self):
        """
            initialise form
        """
        wx.Frame.__init__(self, None, size=(500,300))
        self.Title = 'Align numbers'

        lbl_points = wx.StaticText(self, label='Points')
        lbl_first = wx.StaticText(self, label='First:')
        lbl_second = wx.StaticText(self, label='Second:')
        self.lbl_first_score = wx.TextCtrl(self, -1, style=wx.ALIGN_RIGHT, size=(50,20))
        self.lbl_second_score = wx.TextCtrl(self, -1, style=wx.ALIGN_RIGHT, size=(50,20))

        cmd_score = wx.Button(self, label='Score')
        cmd_score.Bind(wx.EVT_BUTTON, self.on_cmd_score_click)

        main_sizer = wx.GridBagSizer(hgap=10,vgap=10)
        main_sizer.Add(lbl_points, pos=(0, 2),
                    flag=wx.EXPAND | wx.ALL)
        main_sizer.Add(lbl_first, pos=(1, 0),
                    flag=wx.EXPAND | wx.ALL)
        main_sizer.Add(lbl_second, pos=(2, 0),
                    flag=wx.EXPAND | wx.ALL)
        main_sizer.Add(self.lbl_first_score, pos=(1, 2),
                    flag=wx.EXPAND | wx.ALL)
        main_sizer.Add(self.lbl_second_score, pos=(2, 2),
                    flag=wx.EXPAND | wx.ALL)
        main_sizer.Add(cmd_score, pos=(3, 2),
                    flag=wx.EXPAND | wx.ALL)
        self.SetSizer(main_sizer)
        self.scores = [score for score in range(-20, 21)]

    def on_cmd_score_click(self, event):
        del event
        self.lbl_first_score.SetValue('{}'.format(random.choice(self.scores)))
        self.lbl_second_score.SetValue('{:4d}'.format(random.choice(self.scores)))

if __name__ == '__main__':
    """
        initialise application
    """
    simple_screen_app = wx.App()
    main_frame = AlignNumbers()
    main_frame.Show(True)
    simple_screen_app.MainLoop()