本文介绍了wx python 如何在框架上放置按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在面板上有两个按钮.我想要左边一个,右边一个.一定有一个简单的方法,但我看不到它((400, 0) 间隔只是一个例子;它应该适用于任何面板)

I have two buttons on a panel. I want one on the left and one on the right. There must be a simple way but I can't see it (the (400, 0) spacer is just an example; it should work on any panel)

import wx


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1,
                          'Demonstrate wxPython button')
        self.panel = MainPanel(self)
        self.Fit()
        self.Centre()
        self.Show()


class MainPanel(wx.Panel):
    def __init__(self, frame):
        wx.Panel.__init__(self, frame)

        # Button
        button_sizer = self._button_sizer(frame)

        # Main sizer
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add((400, 0))
        main_sizer.Add(button_sizer)
        self.SetSizer(main_sizer)
        self.Fit()

    def _button_sizer(self, frame):
        cmd_save = wx.Button(self, wx.ID_SAVE)
        cmd_cancel = wx.Button(self, wx.ID_CANCEL)
        button_sizer = wx.BoxSizer(wx.HORIZONTAL)
        button_sizer.Add(cmd_save)
        button_sizer.Add(cmd_cancel, flag=wx.ALIGN_RIGHT)
        return button_sizer


if __name__ == '__main__':
    screen_app = wx.App()
    main_frame = MainFrame()
    screen_app.MainLoop()

(对此很抱歉;StackOverflow 需要更多文本)

(Sorry about this; StackOverflow want more text)

Lorem ipsum dolor sat amet、porttitor donec、eget dolor tempor imperdiet、dignissim a sat wisi sed、congue sat accumsan aliquam urna fusce.Placerat vel ligula curabitur in nostra nunc,lorem lorem morbi in aliquet.Etiam ultricies ant sed at aenean id, ligula nibh aenean fusce sapien, ipsum felis sapien eu, mollis ut tempus odio suspendisse 名声.a massa wisi sed nunc maecenas pretium, massa duis vitae non rhoncus nunc, orci sat maecenas ut, aliquet amet, vitae sat.Quam velit rutrum justo varius felis ligula,uthabasse mauris,platea nunc.Et tincidunt vel, libero vitae aliquet donec suspendisse arcu, faucibus orci.Praesent mollis diam eu justo, quam diam arcu vel tincidunt.Aliquam eu fusce ipsum、ante turpis socialsqu、quis posuere pellentesque in pellentesque、nulla sat、phasellus lorem inceptos.Sociis ornare quam magnis eget cursus, ut pretium, velit vitae et sat vivamus, mimalesuada at lectus amet amet vitae.Convallis tristique gravida cras aenean, amet risus sem vehicula ac lacinia et, elit ac, magna eget ut.

Lorem ipsum dolor sit amet, porttitor donec, eget dolor tempor imperdiet, dignissim a sit wisi sed, congue sit accumsan aliquam urna fusce. Placerat vel ligula curabitur in nostra nunc, lorem lorem morbi in aliquet. Etiam ultricies ante sed at aenean id, ligula nibh aenean fusce sapien, ipsum felis sapien eu, mollis ut tempus odio suspendisse fames. A massa wisi sed nunc maecenas pretium, massa duis vitae non rhoncus nunc, orci sit maecenas ut, aliquet amet, vitae sit. Quam velit rutrum justo varius felis ligula, ut habitasse mauris, platea nunc. Et tincidunt vel, libero vitae aliquet donec suspendisse arcu, faucibus orci. Praesent mollis diam eu justo, quam diam arcu vel tincidunt. Aliquam eu fusce ipsum, ante turpis sociosqu, quis posuere pellentesque in pellentesque, nulla sit, phasellus lorem inceptos. Sociis ornare quam magnis eget cursus, ut pretium, velit vitae et sit vivamus, mi malesuada at lectus amet amet vitae. Convallis tristique gravida cras aenean, amet risus sem vehicula ac lacinia et, elit ac, magna eget ut.

推荐答案

我一直发现 sizer 元素 proportion 在此类情况下是您的朋友.它定义了某个元素相对于其他元素应该占用多少空间.
这是您的代码,其中使用了比例选项.

I have always found the the sizer element proportion is your friend in cases like these. Which defines how much room a certain element should take up in relation to others.
Here is your code, with the proportion option being used.

import wx
class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Demonstrate wxPython button')
        self.panel = MainPanel(self)
       # self.Fit()
        self.Centre()
        self.Show()

class MainPanel(wx.Panel):
    def __init__(self, frame):
        wx.Panel.__init__(self, frame)
        # Button
        button_sizer = self._button_sizer(frame)
        # Main sizer
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(button_sizer,proportion=1, flag=wx.EXPAND)
        self.SetSizer(main_sizer)
        self.Fit()

    def _button_sizer(self, frame):
        cmd_save = wx.Button(self, wx.ID_SAVE)
        cmd_cancel = wx.Button(self, wx.ID_CANCEL)
        button_sizer = wx.BoxSizer(wx.HORIZONTAL)
        button_sizer.Add(cmd_save)
        button_sizer.Add((-1, -1), proportion=1)
        button_sizer.Add(cmd_cancel)
        return button_sizer

if __name__ == '__main__':
    screen_app = wx.App()
    main_frame = MainFrame()
    screen_app.MainLoop()

这篇关于wx python 如何在框架上放置按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:14