我怎样才能在这个WXPython程序上再加3个按钮呢

2024-09-23 22:28:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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()

Tags: imageself程序idinitdeffileparent
1条回答
网友
1楼 · 发布于 2024-09-23 22:28:47

我试过这个代码,但对我无效。它给我错误的“分段错误”

使用self.bitmap1作为按钮的父级对我来说似乎很奇怪,所以我尝试使用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()

相关问题 更多 >