-1

就我而言,这是我在运行代码时遇到的错误:

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

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

4

1 回答 1

3

看起来你的Screen类的构造函数有一个流氓参数:

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

That's what your error message seems to be saying. Remove the 'Screen' param as otherwise Python thinks that is a positional (named) parameter.

As as aside: I'd also recommend reading through the Python style guide (Google for PEP-8). You appear to use casing reserved for classes for variables and does make the code less readable for anyone that's written Python for any reasonable length of time.

于 2013-10-24T20:39:16.850 回答