2013-10-24 42 views
0

在我而言,這是我的錯誤,當我運行代碼:我怎麼能理清缺少必需的位置參數

TypeError: __init__() missing 1 required positional argument: 'Screen' 

現在通常這是可以解決的容易,但是對於一些奇怪的原因,我不能似乎解決了這個問題。我使用了一段完美的代碼,但現在我開始亂搞了,我的代碼現在充滿了錯誤。我會回到我的舊代碼,但是一旦我找出錯誤,這個新代碼將比舊代碼好很多。嘿,他們喜歡他們說,「你必須一步一步地採取一切措施」。

代碼(抱歉給了這麼大的代碼,但我不知道你是否需要在我的案件的全部代碼與否):感謝您提前爲你的時間和精力

import pygame 
import sys 
from pygame.locals import * 

white = (255,255,255) 
black = (0,0,0) 
objs = [] 
MAIN_BUTTON = 1 


class Pane(): 

    def __init__(self, textToDisplay, coordinates, screen): 
     self.textToDisplay = textToDisplay 
     self.coordinates = coordinates 
     self.screen = screen 

    def drawPane(self): 
     self.screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115)) 
     pygame.draw.rect(self.screen, (black), self.coordinates, 2) 
     pygame.display.update() 


class Screen(): 

    #constants/array(?) outlining the x,y boundaries of each of x10 panes 

    #Remember to change co-ordinate values 
    #number of panes 
    NoOfPanes = 0 
    Panes = [] 

    def __init__(self, Screen): 
     pygame.init() 
     pygame.display.set_caption('Box Test') 

     self.font = pygame.font.SysFont('Arial', 25) 
     Screen = pygame.display.set_mode((1000,600), 0, 32) 
     self.screen = Screen #I'm guessing the error of my code is somewhere around here, however I thought that I had overcome the situation. Evidently I haven't "sigh"! 
     self.screen.fill((white)) 

     pygame.display.update() 

    def addPane(self, textToDisplay): 
     #right now it is displaying them all change so that it checks how many 
     #panes are on through (numberOfPanes = 0) 10 is limit display no more 
     #than ten. 
     paneLocs = [(175, 75, 200, 100), 
        (0, 0, 200, 100), 
        (600, 400, 200, 100), 
        (175, 75, 200, 100), 
        (175, 75, 200, 100), 
        (175, 75, 200, 100), 
        (175, 75, 200, 100), 
        (175, 75, 200, 100), 
        (175, 75, 200, 100), 
        (175, 75, 200, 100) 
        ] 

     if self.NoOfPanes > 10: 
      print("Limit Reached")    
     else: 
      #self.Panes[self.NoOfPanes] = Pane(textToDisplay, paneLocs[self.NoOfPanes]) 
      #self.Panes[self.NoOfPanes].drawPane() 

      myPane = Pane(textToDisplay, paneLocs[self.NoOfPanes]) 
      myPane.drawPane() 

      self.NoOfPanes = self.NoOfPanes + 1 
      pygame.display.update() 


    def mousePosition(self): 
     global clickPos 
     global releasePos 
     for event in pygame.event.get(): 
      if event.type == MAIN_BUTTON: 
       self.Pos = pygame.mouse.get_pos() 
       return MAIN_BUTTON 
      else: 
       return False 


if __name__ == '__main__': 

    myScreen = Screen() 
    myScreen.addPane("1") 
    myScreen.addPane("2") 
    myScreen.addPane("3") 
    myScreen.addPane("4") 

    while True: 
     ev = pygame.event.get() 
     for event in ev: 
      if event.type == pygame.MOUSEBUTTONUP: 
       posx,posy = pygame.mouse.get_pos() 
       if (posx >= 175 and posx <= 375) and (posy >= 75 and posy <= 175): 
        print("BOB") #Printing bob was for test purposes. 

     for event in pygame.event.get():   
      if event.type == pygame.QUIT: 
       pygame.quit(); sys.exit(); 

+0

您是否使用VCS? (例如Git或Mercurial,Bazaar也可以通過) –

+0

您能提供完整的追溯嗎?很難弄清楚最後一部分例外情況。追蹤的其餘部分應該告訴你究竟是哪條線路導致了這個問題。 – Blckknght

+0

你的事件循環被分成兩個循環。人們可以吃其他人期待的事件。它應該是一個循環。 – ninMonkey

回答

3

它看起來像你的構造爲Screen類有一個流氓參數:

def __init__(self, Screen): <-- the 'Screen' here is causing the issue. 

這就是你的錯誤信息,似乎有什麼在說。刪除'屏幕'參數,否則Python認爲它是一個位置(命名)參數。

至於說:我還建議閱讀Python風格指南(Google for PEP-8)。您似乎使用爲變量類保留的外殼,並且確實使任何編寫Python的代碼在任何合理的時間段內都不可讀取。

+0

對不起,對於非常晚的答覆,但我已經從參數中刪除屏幕,但我仍然收到此錯誤消息: myScreen.addPane(「1」) myPane = Pane(textToDisplay,paneLocs [self.NoOfPanes] ) TypeError:__init __()缺少1個必需的位置參數:'screen'> – PythonNovice

+0

@PythonNovice:您的窗格構造函數需要第三個參數名爲'screen'。所以:'myPane = Pane(textToDisplay,paneLocs [self.NoOfPanes])'應該是'myPane = Pane(textToDisplay,paneLocs [self.NoOfPanes],screen)'其中'screen'大概是'Screen'類型,這意味着你可以這樣說:'myPane = Pane(textToDisplay,paneLocs [self.NoOfPanes],Screen())'''''''''''''''''''''''''''''' – scorpiodawg

相關問題