我用eclipse制作了一个计算器(自己输入数字,不使用按钮)。我是这样设计的。
图片说明:
但它没有运行(关闭窗口),没有调试.....没有错误信息。
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(400,200))
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
calcus = wx.BoxSizer(wx.HORIZONTAL)
self.SetSizer(calcus)
self.text1 = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.text2 = wx.TextCtrl(self, style=wx.TE_MULTILINE)
button5 = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(button5)
eq = wx.Button(self, label=u"=")
button5.Add(eq, 1, wx.GROW)
self.text3 = wx.TextCtrl(self, style=wx.TE_MULTILINE)
def Click1(event):
value1 = self.text1.GetValue()
value2 = self.text2.GetValue()
value3 = value1 + value2
self.text3.SetValue(self,value3)
def Click2(event):
value1 = self.text1.GetValue()
value2 = self.text2.GetValue()
value3 = value1 - value2
self.text3.SetValue(self,value3)
def Click3(event):
value1 = self.text1.GetValue()
value2 = self.text2.GetValue()
value3 = value1 * value2
self.text3.SetValue(self,value3)
def Click4(event):
value1 = self.text1.GetValue()
value2 = self.text2.GetValue()
value3 = value1 / value2
self.text3.SetValue(self,value3)
buttons = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(buttons)
plu = wx.Button(self, label=u"+")
miu = wx.Button(self, label=u"-")
mut = wx.Button(self, label=u"x")
did = wx.Button(self, label=u"%")
plu.Bind(wx.EVT_BUTTON, Click1)
miu.Bind(wx.EVT_BUTTON, Click2)
mut.Bind(wx.EVT_BUTTON, Click3)
did.Bind(wx.EVT_BUTTON, Click4)
buttons.Add(plu, 1, wx.GROW)
buttons.Add(miu, 1, wx.GROW)
buttons.Add(mut, 1, wx.GROW)
buttons.Add(did, 1, wx.GROW)
calcus.Add(self.text1, 4, wx.GROW)
calcus.Add(buttons, 1, wx.GROW)
calcus.Add(self.text2, 4, wx.GROW)
calcus.Add(button5, 1, wx.GROW)
calcus.Add(self.text3, 4, wx.GROW)
self.Center()
self.Show()
app = wx.App(False)
frame = MainWindow(None, "Calculator")
app.MainLoop()
答案 0 :(得分:0)
您的代码存在各种问题。
窗口大小设置得不够宽,无法显示所有控件。
很多小部件创建代码都在click4事件处理程序中缩进。
设置器只需要使用一次。
事件处理程序缩进是错误的。
事件处理程序应将self作为第一个参数。
文本控件getvalue方法返回一个字符串,需要将其转换为数字,然后在计算后返回字符串。
所有事件处理程序仅进行乘法运算。
以下代码可以正常运行,但可以通过删除重复代码等来进一步改进
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(600, 200))
# sizer = wx.BoxSizer(wx.VERTICAL)
# self.SetSizer(sizer)
calcus = wx.BoxSizer(wx.HORIZONTAL)
self.SetSizer(calcus)
self.text1 = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.text2 = wx.TextCtrl(self, style=wx.TE_MULTILINE)
button5 = wx.BoxSizer(wx.VERTICAL)
# self.SetSizer(button5)
eq = wx.Button(self, label=u"=")
button5.Add(eq, 1, wx.GROW)
self.text3 = wx.TextCtrl(self, style=wx.TE_MULTILINE)
buttons = wx.BoxSizer(wx.VERTICAL)
# self.SetSizer(buttons)
plu = wx.Button(self, label=u"+")
miu = wx.Button(self, label=u"-")
mut = wx.Button(self, label=u"x")
did = wx.Button(self, label=u"%")
plu.Bind(wx.EVT_BUTTON, self.click1)
miu.Bind(wx.EVT_BUTTON, self.click2)
mut.Bind(wx.EVT_BUTTON, self.click3)
did.Bind(wx.EVT_BUTTON, self.click4)
buttons.Add(plu, 1, wx.GROW)
buttons.Add(miu, 1, wx.GROW)
buttons.Add(mut, 1, wx.GROW)
buttons.Add(did, 1, wx.GROW)
calcus.Add(self.text1, 4, wx.GROW)
calcus.Add(buttons, 1, wx.GROW)
calcus.Add(self.text2, 4, wx.GROW)
calcus.Add(button5, 1, wx.GROW)
calcus.Add(self.text3, 4, wx.GROW)
self.Center()
self.Show()
def click1(self, event):
value1 = self.text1.GetValue()
value2 = self.text2.GetValue()
value3 = float(value1) + float(value2)
self.text3.SetValue('{}'.format(value3))
def click2(self, event):
value1 = self.text1.GetValue()
value2 = self.text2.GetValue()
value3 = float(value1) - float(value2)
self.text3.SetValue('{}'.format(value3))
def click3(self, event):
value1 = self.text1.GetValue()
value2 = self.text2.GetValue()
value3 = float(value1) * float(value2)
self.text3.SetValue('{}'.format(value3))
def click4(self, event):
value1 = self.text1.GetValue()
value2 = self.text2.GetValue()
value3 = float(value1) / float(value2)
self.text3.SetValue('{}'.format(value3))
app = wx.App(False)
frame = MainWindow(None, "Calculator")
app.MainLoop()