Python 3.x 向按钮添加图像

Python 3.x 向按钮添加图像,python-3.x,wxpython,Python 3.x,Wxpython,我有这个: 以下是创建上述图像的一些代码: hbox = wx.BoxSizer(wx.HORIZONTAL) _img = wx.StaticBitmap() _img.Create(parent, label=wx.Bitmap(os.path.join( os.path.dirname(__file__), 'images', 'exit-to-app.svg'))) hbox.Add(_img, proportion=1, flag=wx.ALIGN_CENTER | wx.A

我有这个:

以下是创建上述图像的一些代码:

hbox = wx.BoxSizer(wx.HORIZONTAL)
_img = wx.StaticBitmap()
_img.Create(parent, label=wx.Bitmap(os.path.join(
    os.path.dirname(__file__), 'images', 'exit-to-app.svg')))
hbox.Add(_img, proportion=1, flag=wx.ALIGN_CENTER | wx.ALL)
_exit = wx.Button(parent, label="Exit")
_exit.SetBackgroundColour('#5968c3')
self.Bind(wx.EVT_BUTTON, self.OnQuit, _exit)
hbox.Add(_exit, proportion=1, flag=wx.ALIGN_CENTER | wx.ALL)
return hbox

如何将位图添加到按钮?

您可以使用
wx.lib.buttons
库,尽管按钮可能有点简单。
然而,正如@RobinDunn指出的,他应该知道,因为版本2.9.1 wx.Button支持显示文本和图像(目前仅在使用wxMSW、wxGTK或OSX/Cocoa端口时)

在Linux上,我必须确保在
桌面设置
中设置了按钮上的
显示图标
,或者只使用
wx的第二种方法。按钮
不显示图像。
使用第二种方法,按钮看起来更漂亮,失去了扁平的外观

import wx
import wx.lib.buttons as buts
class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        pause_button = buts.GenBitmapTextButton(self, -1, bitmap=wx.Bitmap("pause.png"), label= "Pause")
        play_button = buts.GenBitmapTextButton(self, -1, bitmap=wx.Bitmap("play.png"), label= "Play")
        time_button = wx.Button(self, -1, label= "Time")
        time_button.SetBitmap(wx.Bitmap("toggle1.png"),wx.RIGHT)
        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(pause_button, 0, wx.CENTER | wx.ALL,10)
        box.Add(play_button, 0, wx.CENTER | wx.ALL,10)
        box.Add(time_button, 0, wx.CENTER | wx.ALL,10)
        self.SetSizerAndFit(box)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
        pause_button.Bind(wx.EVT_BUTTON, self.OnPause)
        play_button.Bind(wx.EVT_BUTTON, self.OnPlay)
        time_button.Bind(wx.EVT_BUTTON, self.OnTime)
        self.Show()

    def OnCloseWindow(self, event):
        self.Destroy()
    def OnPause(self, event):
        print "Pause pressed"
    def OnPlay(self, event):
        print "Play pressed"
    def OnTime(self, event):
        print "Time pressed"

if __name__ == "__main__":
    app = wx.App()
    frame = TestFrame(None, -1, "wxBitmap Test")
    app.MainLoop()

您可以使用
wx.lib.buttons
库,尽管按钮可能有点简单。
然而,正如@RobinDunn指出的,他应该知道,因为版本2.9.1 wx.Button支持显示文本和图像(目前仅在使用wxMSW、wxGTK或OSX/Cocoa端口时)

在Linux上,我必须确保在
桌面设置
中设置了按钮上的
显示图标
,或者只使用
wx的第二种方法。按钮
不显示图像。
使用第二种方法,按钮看起来更漂亮,失去了扁平的外观

import wx
import wx.lib.buttons as buts
class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        pause_button = buts.GenBitmapTextButton(self, -1, bitmap=wx.Bitmap("pause.png"), label= "Pause")
        play_button = buts.GenBitmapTextButton(self, -1, bitmap=wx.Bitmap("play.png"), label= "Play")
        time_button = wx.Button(self, -1, label= "Time")
        time_button.SetBitmap(wx.Bitmap("toggle1.png"),wx.RIGHT)
        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(pause_button, 0, wx.CENTER | wx.ALL,10)
        box.Add(play_button, 0, wx.CENTER | wx.ALL,10)
        box.Add(time_button, 0, wx.CENTER | wx.ALL,10)
        self.SetSizerAndFit(box)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
        pause_button.Bind(wx.EVT_BUTTON, self.OnPause)
        play_button.Bind(wx.EVT_BUTTON, self.OnPlay)
        time_button.Bind(wx.EVT_BUTTON, self.OnTime)
        self.Show()

    def OnCloseWindow(self, event):
        self.Destroy()
    def OnPause(self, event):
        print "Pause pressed"
    def OnPlay(self, event):
        print "Play pressed"
    def OnTime(self, event):
        print "Time pressed"

if __name__ == "__main__":
    app = wx.App()
    frame = TestFrame(None, -1, "wxBitmap Test")
    app.MainLoop()

wx.Button
支持向标签添加位图已有一段时间了,尽管在GTK版本中,我认为图标是否显示也取决于全局首选项,可能还取决于活动主题。有关示例,请参见演示中的按钮示例


wx.Button
支持向标签添加位图已有一段时间了,尽管在GTK版本中,我认为图标是否显示也取决于全局首选项,可能还取决于活动主题。有关示例,请参见演示中的按钮示例


在答案中添加了Robins的新方法。谢谢你,罗宾!在答案中添加了Robins的新方法。谢谢你,罗宾!是,您还可以设置标签的哪一侧放置位图。是,您还可以设置标签的哪一侧放置位图。