如何在此WXPython程序上添加另外3个按钮

时间:2015-11-28 16:02:27

标签: python button wxpython

我在WXPython中进行了新的编程,我从互联网上开始编程,开始编写可视化程序。

但是如何在没有错误的情况下在此程序上再添加3个按钮?

import wx
class Panel1(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        try:
            image_file = "roses.jpg"
            bmp1 = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            self.bitmap1 = wx.StaticBitmap(self, -1, bmp1, (0, 0))
            def InitUI(self):    


                str1 = "%s  %dx%d" % (image_file, bmp1.GetWidth(), bmp1.GetHeight()) 
                parent.SetTitle(str1)
        except IOError:
            print "Image file %s not found" % imageFile
            raise SystemExit
        self.button1 = wx.Button(self.bitmap1, id=-1, label='Snake', pos=(200, 300))

app = wx.PySimpleApp()
frame1 = wx.Frame(None, -1, "An image on a panel", size=(640, 480))
panel1 = Panel1(frame1, -1)
frame1.Show(True)
app.MainLoop()

1 个答案:

答案 0 :(得分:0)

我尝试了这段代码,它对我不起作用。它给了我错误“Segmentation faul”

使用self.bitmap1作为Button的父亲对我来说似乎很奇怪所以我尝试使用self(Panel1)作为父母,它对我有用。

我做了一些其他修改 - 我将self.添加到某些变量中,我添加了self.parent。我移动了InitUI

#!/usr/bin/env python

import wx

class Panel1(wx.Panel):

    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)

        self.parent = parent

        try:
            self.image_file = "roses.jpg"
            self.bmp1 = wx.Image(self.image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            self.bitmap1 = wx.StaticBitmap(self, -1, self.bmp1, (0, 0))
        except IOError:
            print "Image file %s not found" % self.image_file
            raise SystemExit

        self.InitUI()

        self.button1 = wx.Button(self, id=-1, label='Snake', pos=(200, 300))
        self.button2 = wx.Button(self, id=-1, label='Apple', pos=(100, 100))

    def InitUI(self):    
        str1 = "%s  %dx%d" % (self.image_file, self.bmp1.GetWidth(), self.bmp1.GetHeight()) 
        self.parent.SetTitle(str1)

app = wx.PySimpleApp()
frame1 = wx.Frame(None, -1, "An image on a panel", size=(640, 480))
panel1 = Panel1(frame1, -1)
frame1.Show(True)
app.MainLoop()