我正在学习使用 wxPython。我已经做了一些工作正常的基本示例,但是在这个示例中它失败了。它是一个简单的平方数框架,在 wxFormBuilder 的帮助下准备。我正在运行:Python 3.5.1 |Anaconda 2.5.0(64 位)| (默认,2016 年 2 月 16 日,09:49:46)[MSC v.1900 64 位 (AMD64)] 在 Windows 8.1 上。wxPython的版本是Phoenix (3.0.3.dev1964+f780b21)
wxFormBuilder 代码“test_wx.py”是:
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################
import wx
import wx.xrc
import gettext
_ = gettext.gettext
###########################################################################
## Class MyFrame1
###########################################################################
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ (self, parent, id = wx.ID_ANY,
title = wx.EmptyString,
pos = wx.DefaultPosition,
size = wx.Size( 342,246 ),
style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.m_staticText1 = wx.StaticText( self, wx.ID_ANY,
_(u"Give me a number"),
wx.DefaultPosition,
wx.DefaultSize, wx.ALIGN_CENTRE )
self.m_staticText1.Wrap( -1 )
bSizer1.Add( self.m_staticText1, 0, wx.ALL|wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.m_textCtrl1 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer1.Add( self.m_textCtrl1, 0, wx.ALL|wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.m_button1 = wx.Button( self, wx.ID_ANY,
_(u"Find the square!"),
wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer1.Add( self.m_button1, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.m_textCtrl2 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString,
wx.DefaultPosition, wx.DefaultSize, wx.TE_CENTRE )
bSizer1.Add( self.m_textCtrl2, 0, wx.ALL|wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.SetSizer( bSizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.m_button1.Bind( wx.EVT_BUTTON, self.square)
def __del__( self ):
pass
# Virtual event handlers, override them in your derived class
def square( self, event ):
event.Skip()
然后我有这个主脚本调用我的演示:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 10 12:42:45 2016
@author: Miguel
"""
# -*- coding: utf-8 -*-
import wx
# Import the GUI as prepared by wxFormBuilder
import test_wx as demo
class CalcFrame(demo.MyFrame1):
def __init__(self,parent):
demo.MyFrame1.__init__(self,parent)
def square(self,event):
num = int(self.m_textCtrl1.GetValue())
self.m_textCtrl2.SetValue (str(num * num))
app = wx.App(False)
frame = CalcFrame(None)
frame.Show(True)
# Start the GUI
app.MainLoop()
wx.__version__
代码没有运行。我得到的唯一线索是:
AttributeError:“CalcFrame”对象没有属性“SetSizeHintsSz”
但在我看来,这在继承的类中是可用的!!!无论如何,我试图在 CalcFrame 的定义中包含以下语句:
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
但也没有奏效。这段代码可能有什么问题?