看到别人写口算生成器,我就有点蠢蠢欲动了( @cht )
运行界面如下
注:如需运行,请使用python3并下载wxpython库
import wx
import wx.xrc
import random
class MyDialog1 ( wx.Dialog ):
x=123
y=321
tot=0
zq=0
def __init__( self, parent ):
wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 400,200 ), style = wx.DEFAULT_DIALOG_STYLE )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.m_staticText1 = wx.StaticText( self, wx.ID_ANY, u"123+321", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText1.Wrap( -1 )
self.m_staticText1.SetFont( wx.Font( 36, 70, 90, 90, False, "宋体" ) )
bSizer1.Add( self.m_staticText1, 0, wx.ALL, 5 )
self.m_textCtrl1 = wx.TextCtrl( self, wx.ID_ANY, u"在此输入您的答案", wx.DefaultPosition, wx.DefaultSize, wx.TE_PROCESS_ENTER )
bSizer1.Add( self.m_textCtrl1, 0, wx.ALL, 5 )
self.m_button2 = wx.Button( self, wx.ID_ANY, u"提交", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer1.Add( self.m_button2, 0, wx.ALL, 5 )
self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, u"", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText2.Wrap( -1 )
bSizer1.Add( self.m_staticText2, 0, wx.ALL, 5 )
self.SetSizer( bSizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.m_textCtrl1.Bind( wx.EVT_TEXT_ENTER, self.tj )
self.m_button2.Bind( wx.EVT_BUTTON, self.tj )
def __del__( self ):
pass
def tj( self, event ):
self.tot+=1
ans=self.m_textCtrl1.Value
try:
if eval(ans)==self.x+self.y:
self.zq+=1
self.m_staticText2.Label='答案正确'+' 正确率:'+str(self.zq)+'/'+str(self.tot)
else:
self.m_staticText2.Label='答案错误'+' 正确率:'+str(self.zq)+'/'+str(self.tot)
except:
self.m_staticText2.Label='输入错误'+' 正确率:'+str(self.zq)+'/'+str(self.tot)
else:
pass
self.m_textCtrl1.Value=''
self.x=random.choice(range(1000))
self.y=random.choice(range(1000))
self.m_staticText1.Label=str(self.x)+'+'+str(self.y)
app=wx.App(False)
zjm=MyDialog1(None)
zjm.Show(True)
app.MainLoop()
Orz