【问题标题】:How can I put another 3 buttons on this WXPython programs我怎样才能在这个 WXPython 程序上再放 3 个按钮
【发布时间】:2015-11-28 16:02:26
【问题描述】:

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

【问题讨论】:

  • 抱歉题主有误。这只是一个程序。
  • 你有什么错误?始终提出完整的错误消息。
  • 问题是我没有错误。我只想再添加 3 个按钮,但我不知道怎么做。
  • wx.PySimpleApp 已弃用。你应该使用wx.App(False)。至于添加更多按钮,只需创建更多按钮实例并更改位置,使它们不会彼此重叠(或使用大小调整器)

标签: python button wxpython


【解决方案1】:

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

使用 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()

【讨论】:

    猜你喜欢
    • 2017-08-20
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 2022-12-11
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多