Python Pygame和类

Python Pygame和类,python,pygame,subclass,Python,Pygame,Subclass,所以我在类中制作一个tkinter/pygame游戏,而python在类中不识别pygame。 (不要担心我将如何单独启动窗口,这将在窗口工作后完成 from tkinter import * from pygame import * class firstscreen: def __init__(self): self.window = Tk() self.window.minsize(300, 250) self.window.resi

所以我在类中制作一个tkinter/pygame游戏,而python在类中不识别pygame。 (不要担心我将如何单独启动窗口,这将在窗口工作后完成

from tkinter import *
from pygame import *
class firstscreen:
    def __init__(self):
        self.window = Tk()
        self.window.minsize(300, 250)
        self.window.resizable(width=False, height=False)
        self.window.title("Login")
        self.username = ""
        self.password = ""
        self.userlabel = Label(self.window, text="Username")
        self.userlabel.place(x=50, y=50)
        self.passlabel = Label(self.window, text="Password")
        self.passlabel.place(x=50, y=100)
        self.userentry = Entry(self.window, textvariable=self.username)
        self.userentry.place(x=110, y=50)
        self.passentry = Entry(self.window, textvariable=self.password, show="*")
        self.passentry.place(x=110, y=100)
        self.loginbutton = Button(self.window, text="Login", command = self.login())
        self.loginbutton.place(x=80, y=140)
        self.registerbutton = Button(self.window, text="Register",command=self.login())
        self.registerbutton.place(x=140, y=140)
        self.window.mainloop()
    def login(self):
        print("Hello")

class gamewindow():
    def __init__(self):
        pygame.init()
        game = pygame.display.set_mode((800, 300))
runlogin = firstscreen()
rungame = gamewindow()
(对于第29行,pygame.init())

必须是

从pygame导入*
# [...]
类gamewindow():
定义初始化(自):
init()
游戏=显示。设置_模式((800300))

导入pygame
# [...]
类gamewindow():
定义初始化(自):
pygame.init()
game=pygame.display.set_模式((800300))

请参见

您是否收到某种特定错误?请使用此信息更新问题。您的代码对我来说运行良好,请确保pygame正常downloaded@quamrana未定义名称pygame(在pygame.init()行上)我在你的问题中没有看到这个错误的文本。你更新了这个问题吗?@rabbi76我需要先检查一下,但很可能是答案,所以我会暂时将它标记为正确
NameError:name 'pygame' is not defined