wxPython面板未显示

时间:2019-11-07 18:55:51

标签: python wxpython

我正在玩wxPython。我的理解是您需要以下物品: 1.主要的“应用” 2.框架(或我认为的主窗口) 3.框架内的面板 4.面板中的小部件可以做事。

认为我对第1点和第2点没问题,因为在运行代码时出现准系统弹出窗口。但是,我尝试向其中添加一个面板和一些基本文本-没有任何显示。

我的代码是:

import wx

class PDFApp(wx.App):
    def OnInit(self):   #Method used to define Frame & show it

        self.frame = PDFFrame(parent=None, title="PDF Combiner", size=(300, 300))
        self.frame.Show()
        return True  

class PDFFrame(wx.Frame):
    def _init_(self, parent, title):
        super(PDFFrame, self).__init__(parent, title=title)

        Panel = PDFPanel(self)

class PDFPanel(wx.Panel):
    def _init_(self, parent):
        super(PDFPanel, self).__init__(parent)

        self.Label = wx.StaticText(self, label="hello")


App = PDFApp()
App.MainLoop()

指出我的错误/遗漏的指针-非常感谢!

1 个答案:

答案 0 :(得分:0)

您的代码是非常规的,因为wx.App通常只是app = wx.App()
但是,_init_应该是__init__,并且请勿使用会与保留字或内部字冲突的变量名。
PanelLabel,也可能还有App
以下应该起作用。

import wx

class PDFApp(wx.App):
    def OnInit(self):   #Method used to define Frame & show it

        frame = PDFFrame(parent=None, title="PDF Combiner")
        frame.Show()
        return True  

class PDFFrame(wx.Frame):
    def __init__(self, parent, title):
        super(PDFFrame, self).__init__(parent, title=title)

        panel = PDFPanel(self)

class PDFPanel(wx.Panel):
    def __init__(self, parent):
        super(PDFPanel, self).__init__(parent)

        label = wx.StaticText(self, label="hello", pos=(50,100))


App = PDFApp()
App.MainLoop()

enter image description here

编辑:
我比较老套(并且是自学的),所以我会像这样简单地编写一些代码,例如:

import wx

class PdfFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title)
        panel = wx.Panel(self)
        label = wx.StaticText(panel, -1, label="hello", pos=(50,100))
        self.Show()

app = wx.App()
frame = PdfFrame(None, title = "Simple Panel")
app.MainLoop()