2013-03-30 81 views
0

基本上我现在正在学习Python,所以我只是使用其他人的模板,只是编辑它们。到目前为止,我已经了解到Python中的缩进非常挑剔。 然而,我卡住了,我想我已经正确地缩进并定义好了,但我仍然在控制台中看到这个错误。 (窗口)名称错误和其他?

(是的,我知道这是尚未完成的)

"...\documents\python_files>python calc.py 
Traceback (most recent call last): 
    File "calc.py", line 20, in <module> 
    class Calculator(wx.Dialog): 
    File "calc.py", line 46, in Calculator 
    b = wx.Button(self, -1, label) 
NameError: name 'self' is not defined" 

这里是我的代码(我认为我把它放在这里的代码格式):

# -*- coding: utf-8 -*- 
from __future__ import division 

__author__ = 'Sean' 
__version__ = '0.0.2' 

#Calculator GUI: 

# ____________v 
#[(][)][^][log] 
#[C][±][√][%] 
#[7][8][9][/] 
#[4][5][6][*] 
#[1][2][3][-] 
#[0][.][+][=] 

import wx 
from math import * 

class Calculator(wx.Dialog): 
    '''Main calculator dialog''' 
    def __init__(self): 
     title = 'Calculator version %s' % __version__ 
     wx.Dialog.__init__(self, None, -1, title) 
     sizer = wx.BoxSizer(wx.VERTICAL) # Main vertical sizer 

     # ____________v 
     self.display = wx.ComboBox(self, -1) 
     sizer.Add(self.display, 0, wx.EXPAND) 


    #[(][)][^][log] 
    #[C][±][√][%] 
    #[7][8][9][/] 
    #[4][5][6][*] 
    #[1][2][3][-] 
    #[0][.][+][=] 
    gsizer = wx.GridSizer(4,6) 
    for row in (("(",")","^","log"), 
    ("C","±","√","%"), 
    ("7", "8", "9", "/"), 
    ("4", "5", "6", "*"), 
    ("1", "2", "3", "-"), 
    ("0", ".", "+", "=")): 
     for label in row: 
      b = wx.Button(self, -1, label) 
      gsizer.Add(b) 
      self.Bind(wx.EVT_Button,self.OnButton, b) 
      sizer.Add(gsizer, 1, wx.EXPAND) 

    b = wx.Button(self, -1, "=") 
    self.Bind(wx.EVT_BUTTON, self.OnButton, b) 
    sizer.Add(b, 0, wx.EXPAND) 
    self.equal = b 

    self.SetSizer(sizer) 
    sizer.Fit(self) 
    self.CenterOnScreen() 

def OnButton(self, evt): 
    '''Handle button click event''' 
    label = evt.GetEventObject().GetLabel() 

    if label == '=': 
     try: 
      compute = self.display.GetValue() 
      if not compute.strip(): 
       return 

      result = eval(compute) 

      self.display.Insert(compute, 0) 

      self.display.SetValue(str(result)) 
     except Exception as err: 
      wx.LogError(str(err)) 
      return 

    elif label == 'C': 
     self.display.SetValue('') 

    else: 
     self.display.SetValue(self.display.GetValue() + label) 
     self.equal.SetFocus() 

if __name__ == '__main__': 
    app = wx.PySimpleApp() 
    dlg = Calculator() 
    dlg.ShowModal() 
    dlg.Destroy() 

回答

2

__init__方法部分的缩进是错误的:

class Calculator(wx.Dialog): 
    '''Main calculator dialog''' 
    def __init__(self): 
     title = 'Calculator version %s' % __version__ 
     wx.Dialog.__init__(self, None, -1, title) 
     sizer = wx.BoxSizer(wx.VERTICAL) # Main vertical sizer 
     self.display = wx.ComboBox(self, -1) 
     sizer.Add(self.display, 0, wx.EXPAND) 
     # This part was indented wrong 
     gsizer = wx.GridSizer(4,6) 
     for row in (("(",")","^","log"), 
        ("C","±","√","%"), 
        ("7", "8", "9", "/"), 
        ("4", "5", "6", "*"), 
        ("1", "2", "3", "-"), 
        ("0", ".", "+", "=")): 
     for label in row: 
      b = wx.Button(self, -1, label) 
      gsizer.Add(b) 
      self.Bind(wx.EVT_Button,self.OnButton, b) 
      sizer.Add(gsizer, 1, wx.EXPAND) 
     b = wx.Button(self, -1, "=") 
     self.Bind(wx.EVT_BUTTON, self.OnButton, b) 
     sizer.Add(b, 0, wx.EXPAND) 
     self.equal = b 

你应该阅读Python。从Python 2 tutorial

请注意,基本块内的每行必须缩进相同的数量。

另一件要读的是Python Style Guide

+0

通常最好拿到一本书并阅读它。编辑不会帮助他们,因为没有语法错误 - 可以在类体中使用代码。 – georg

+1

@ thg435:我添加了一个指向教程的指针。这是一个开始... –

+0

谢谢你,我也应该阅读教程,因为编辑事情已经使我陷入死胡同。 – user2227732