wxPython面板不显示
wxPython Panel not showing
我正在玩 wxPython。我的理解是您需要以下项目:
1.主要'App'
2.一个框架(或者我认为的main window)
3.框架内的面板
4.面板内的小部件做事
认为我对第 1 点和第 2 点没意见,因为当我 运行 我的代码时,我得到一个准系统 window 弹出窗口。但是,我尝试向其中添加一个面板和一些基本文本 - 但没有任何显示。
我的代码是:
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()
指出我的错误/遗漏 - 非常感谢!
您的代码非常规,因为 wx.App
通常只是 app = wx.App()
但是,_init_
应该是 __init__
并且不要使用可能与保留字或内部冲突的变量名。
即 Panel
或 Label
,可能还有 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()
编辑:
我比较守旧(并且自学),所以我会编写像这样简单的代码,如下所示:
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()
我正在玩 wxPython。我的理解是您需要以下项目: 1.主要'App' 2.一个框架(或者我认为的main window) 3.框架内的面板 4.面板内的小部件做事
认为我对第 1 点和第 2 点没意见,因为当我 运行 我的代码时,我得到一个准系统 window 弹出窗口。但是,我尝试向其中添加一个面板和一些基本文本 - 但没有任何显示。
我的代码是:
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()
指出我的错误/遗漏 - 非常感谢!
您的代码非常规,因为 wx.App
通常只是 app = wx.App()
但是,_init_
应该是 __init__
并且不要使用可能与保留字或内部冲突的变量名。
即 Panel
或 Label
,可能还有 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()
编辑:
我比较守旧(并且自学),所以我会编写像这样简单的代码,如下所示:
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()