我正在使用wxGlade进行简单的GUI计算。
通过wxGlade创建GUI并生成代码后, 我修改并输入了一些特定的代码,以便计算器的功能应该是这样的。
简而言之,用户需要通过选择代表用户所需任务的单选按钮来选择添加,减去或乘以用户在开始时输入的两个值。
通过选择添加按钮,用户将选择添加任务。以下是代码:
def add(self, event): # wxGlade: MyFrame.<event_handler>
self.sommation = True
self.moins = False
self.multiply = False
event.Skip()
减法也是如此,
def moins(self, event): # wxGlade: MyFrame.<event_handler>
self.moins = True
self.sommation = False
self.multiply = False
event.Skip()
和乘法
def multiply(self, event): # wxGlade: MyFrame.<event_handler>
self.multiply = True
self.sommation = False
self.moins = False
event.Skip()
选择任务后,用户单击名为calcul的按钮以获得结果。下面是calcul任务的代码:
def calcul(self, event): # wxGlade: MyFrame.<event_handler>
if self.sommation == True :
self.c = int(self.text_ctrl_1.GetValue()) + int(self.text_ctrl_2.GetValue())
self.somation == False
elif self.moins == True :
self.c = int(self.text_ctrl_1.GetValue()) - int(self.text_ctrl_2.GetValue())
self.moins = False
elif self.multiply == True :
self.c = int(self.text_ctrl_1.GetValue()) * int(self.text_ctrl_2.GetValue())
self.multiply = False
self.button_1.SetLabel(str(self.c))
event.Skip()
问题在于,我一直在标题上引用属性错误的问题。
p / s这是完整的程序
#!/usr/bin/env python
# -*- coding: CP1252 -*-
#
# generated by wxGlade 0.6.8 (standalone edition) on Thu Mar 17 07:56:19 2016
#
import wx
# begin wxGlade: dependencies
import gettext
# end wxGlade
# begin wxGlade: extracode
# end wxGlade
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.radio_btn_1 = wx.RadioButton(self, wx.ID_ANY, _("addition\n"), style=wx.RB_GROUP)
self.text_ctrl_1 = wx.TextCtrl(self, wx.ID_ANY, "")
self.radio_btn_2 = wx.RadioButton(self, wx.ID_ANY, _("subtraction"))
self.text_ctrl_2 = wx.TextCtrl(self, wx.ID_ANY, "")
self.label_1 = wx.StaticText(self, wx.ID_ANY, _("="), style=wx.ALIGN_CENTRE)
self.button_2 = wx.Button(self, wx.ID_ANY, "")
self.radio_btn_3 = wx.RadioButton(self, wx.ID_ANY, _("multiply"))
self.button_1 = wx.Button(self, wx.ID_ANY, _("calcul"))
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_RADIOBUTTON, self.add, self.radio_btn_1)
self.Bind(wx.EVT_RADIOBUTTON, self.moins, self.radio_btn_2)
self.Bind(wx.EVT_RADIOBUTTON, self.multiply, self.radio_btn_3)
self.Bind(wx.EVT_BUTTON, self.calcul, self.button_1)
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle(_("frame_1"))
self.SetFont(wx.Font(20, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
self.label_1.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_1 = wx.GridSizer(3, 5, 0, 0)
sizer_1.Add((0, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
sizer_1.Add(self.radio_btn_1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
sizer_1.Add(self.text_ctrl_1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
sizer_1.Add(self.radio_btn_2, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
sizer_1.Add(self.text_ctrl_2, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
sizer_1.Add(self.label_1, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
sizer_1.Add(self.button_2, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
sizer_1.Add((20, 20), 0, wx.ALIGN_BOTTOM | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
sizer_1.Add(self.radio_btn_3, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
sizer_1.Add(self.button_1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
# end wxGlade
def add(self, event): # wxGlade: MyFrame.<event_handler>
self.sommation = True
self.moins = False
self.multiply = False
event.Skip()
def moins(self, event): # wxGlade: MyFrame.<event_handler>
self.moins = True
self.sommation = False
self.multiply = False
event.Skip()
def multiply(self, event): # wxGlade: MyFrame.<event_handler>
self.multiply = True
self.sommation = False
self.moins = False
event.Skip()
def calcul(self, event): # wxGlade: MyFrame.<event_handler>
if self.sommation == True :
self.c = int(self.text_ctrl_1.GetValue()) + int(self.text_ctrl_2.GetValue())
self.somation == False
elif self.moins == True :
self.c = int(self.text_ctrl_1.GetValue()) - int(self.text_ctrl_2.GetValue())
self.moins = False
elif self.multiply == True :
self.c = int(self.text_ctrl_1.GetValue()) * int(self.text_ctrl_2.GetValue())
self.multiply = False
self.button_1.SetLabel(str(self.c))
event.Skip()
# end of class MyFrame
if __name__ == "__main__":
gettext.install("app") # replace with the appropriate catalog name
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, wx.ID_ANY, "")
frame_1 = MyFrame(None, wx.ID_ANY, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()
答案 0 :(得分:0)
如果有人在输入任何数字之前点击了计算按钮,您就会收到此错误;因为您只是在按钮处理程序中创建变量。
要避免这种情况,请将self.sommation = None
添加到__init__
方法,以使其具有默认值。