我在主班上创建了一个面板。然后,我想创建一个进入面板的按钮。我为按钮创建了一个单独的类,名为panel_in_button,并在其参数中设置了main,希望我可以继承我的主类中的面板,然后在我的panel_in_button类中使用它,但是由于某些奇怪的原因,当我点击按钮时,按钮不会显示运行程序。除此之外,该程序运行良好。请帮忙。这是我收到的错误,但我认为这与为什么我无法访问面板没有任何关系。
警告(来自警告模块):文件“ C:\ Python27 \ GUI Practice.py”,第19行app = wx.PySimpleApp()#这将运行程序wxPyDeprecationWarning:使用已弃用的类PySimpleApp。
import wx
class main(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id, "My window", size=(300, 200))
panel=wx.Panel(self)
class panel_in_button(main):
def __init__(self):
button = wx.Button(main.panel, label="exit",pos=(130,10), size=(60, 60))
self.Bind(wx.EVT_BUTTON, self.closebutton, button)
self.Bind(wx.EVT_CLOSE, self.closewindow)
def closebutton(self, event):
self.Close(True)
def closewindow(self, event):
self.Destroy()
if __name__=="__main__":
app=wx.PySimpleApp() #This runs the program
frame=main(parent=None, id=-1)#Displays the program
frame.Show()
app.MainLoop()
您不能以这种方式编写代码。main是一个类,而不是一个类的实例。您不应该直接调用类的方法。相反,您需要实例化它,然后调用对象的方法。在此代码中的任何地方都没有实例化panel_in_button。无论如何,我不建议这样编程。这是一个清理的版本:
import wx
class main(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id, "My window", size=(300, 200))
panel=wx.Panel(self)
button = wx.Button(panel, label="exit",pos=(130,10), size=(60, 60))
self.Bind(wx.EVT_BUTTON, self.closebutton, button)
self.Bind(wx.EVT_CLOSE, self.closewindow)
def closebutton(self, event):
self.Close(True)
def closewindow(self, event):
self.Destroy()
if __name__=="__main__":
app=wx.App(False) #This runs the program
frame=main(parent=None, id=-1)#Displays the program
frame.Show()
app.MainLoop()
这将两个类别合并为一个类别。我也替换了对wx.PySimpleApp的引用,因为该引用已被弃用。我建议您看看尺寸调整器,而不是绝对的位置。大小调整器绝对值得学习。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句