我怎样才能找出缺少必需的位置参数

时间:2013-10-24 20:23:31

标签: python pygame

在我的情况下,这是我运行代码时出现的错误:

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();

提前感谢您的时间和精力。

1 个答案:

答案 0 :(得分:3)

看起来Screen类的构造函数有一个恶意参数:

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

这就是您的错误消息似乎在说什么。删除'Screen'参数,否则Python认为这是一个位置(命名)参数。

除此之外:我还建议您阅读Python样式指南(Google PEP-8)。您似乎使用为变量类保留的大小写,并且对于任何在任何合理时间内编写Python的人来说,这些代码的可读性都会降低。