wxPython面板未显示

2024-09-30 03:23:38 发布

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

我在和wxPython玩。我的理解是您需要以下物品: 1主“应用程序” 2框架(或我认为的主窗口) 三。框架内的面板 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()

指出我的错误/遗漏-非常感谢!你知道吗


Tags: 代码self框架app面板titleinitdef
1条回答
网友
1楼 · 发布于 2024-09-30 03:23:38

您的代码很不传统,因为wx.App通常只是app = wx.App()
但是,_init_应该是__init__,不要使用可能与保留字或内部冲突的变量名。
i、 例如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()

相关问题 更多 >

    热门问题