我是 Python/wxPython 新手,我使用 wxFormBuilder
创建了两个框架的文本。目的是添加两个数字并在单击 OnAdd
按钮时在两个框架上显示结果。
我已经尽力了,但没有成功?
我的问题是如何在按下添加按钮时调用的第二帧上显示 Final_Result,如下面的代码所示;
注意:代码位于 3 个单独的文件中(q1.py、q2.py 和 q3.py)。 q2.py 是主要运行文件,而 q1.py 和 q2.py 分别创建从 wxFormBuilder 生成的框架。
q1.py
import wx
import wx.xrc
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( 500,300 ), style = wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
Sizer1 = wx.BoxSizer( wx.VERTICAL )
Sizer2 = wx.GridSizer( 0, 2, 0, 0 )
self.val1 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
self.val1.SetFont( wx.Font( 30, 70, 90, 90, False, wx.EmptyString ) )
Sizer2.Add( self.val1, 1, wx.ALL|wx.EXPAND, 5 )
self.val2 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
self.val2.SetFont( wx.Font( 30, 70, 90, 90, False, wx.EmptyString ) )
Sizer2.Add( self.val2, 1, wx.ALL|wx.EXPAND, 5 )
self.Calc = wx.Button( self, wx.ID_ANY, u"Add", wx.DefaultPosition, wx.DefaultSize, 0 )
self.Calc.SetFont( wx.Font( 30, 70, 90, 90, False, wx.EmptyString ) )
Sizer2.Add( self.Calc, 1, wx.ALL|wx.EXPAND, 5 )
self.result = wx.StaticText( self, wx.ID_ANY, u"Result", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
self.result.Wrap( -1 )
self.result.SetFont( wx.Font( 30, 70, 90, 90, False, wx.EmptyString ) )
Sizer2.Add( self.result, 1, wx.ALL|wx.EXPAND, 5 )
Sizer1.Add( Sizer2, 1, wx.EXPAND, 5 )
self.SetSizer( Sizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.Calc.Bind( wx.EVT_BUTTON, self.addFunc )
def __del__( self ):
pass
#===================================================
q2.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
from q1 import MyFrame1
from q3 import MyFrame3
class MyFrame2(MyFrame1):
def __init__(self, parent):
MyFrame1.__init__ (self, parent)
def addFunc( self, event ):
val1 = float(self.val1.GetValue())
val2 = float(self.val2.GetValue())
add = val1 + val2
self.result.SetLabel(str(add))
self.result = MyFrame4(self)
self.result.Show()
self.Final_Result.SetLabel(str(add))
class MyFrame4(MyFrame3):
"""docstring for my_temp_Frame"""
def __init__(self, parent):
MyFrame3.__init__ (self, parent)
if __name__ == "__main__":
app = wx.App(0)
MyFrame2(None).Show()
app.MainLoop()
#===================================================
q3.py
import wx
import wx.xrc
class MyFrame3 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,100 ), style = wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
Sizer1 = wx.BoxSizer( wx.VERTICAL )
Sizer2 = wx.GridSizer( 0, 2, 0, 0 )
self.Text = wx.TextCtrl( self, wx.ID_ANY, u"You result is:", wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY )
self.Text.SetFont( wx.Font( 20, 70, 90, 90, False, wx.EmptyString ) )
Sizer2.Add( self.Text, 1, wx.ALL|wx.EXPAND, 5 )
self.Final_Result = wx.StaticText( self, wx.ID_ANY, u"Final_Result", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
self.Final_Result.Wrap( -1 )
self.Final_Result.SetFont( wx.Font( 30, 70, 90, 90, False, wx.EmptyString ) )
self.Final_Result.SetForegroundColour( wx.Colour( 255, 255, 255 ) )
self.Final_Result.SetBackgroundColour( wx.Colour( 255, 0, 0 ) )
Sizer2.Add( self.Final_Result, 1, wx.ALL|wx.EXPAND, 5 )
Sizer1.Add( Sizer2, 1, wx.EXPAND, 5 )
self.SetSizer( Sizer1 )
self.Layout()
self.Centre( wx.BOTH )
def __del__( self ):
pass
#===================================================
提前致谢。
我是一名优秀的程序员,十分优秀!