【问题标题】:Returning a variable from second python file从第二个 python 文件返回一个变量
【发布时间】:2020-07-05 20:07:56
【问题描述】:

我有两个文件,一个叫做game.py,另一个叫做menu.py。我需要得到游戏。 py 从 menu.py 中的 Menu_Screen() 返回一个值

#game.py
import menu
import turtle
Single_Multi = 0

def SinglePlayer():
  #do stuff

def MultiPlayer():
  #do stuff
if __name__ == "__main__"

  Single_Multi = int(menu.Menu_Screen())

  if Single_Multi == 1:
    SinglePlayer()

  elif Single_Multi == 3:
    MultiPlayer()


#menu.py
import turtle

wn = turtle.Screen()
wn.cv._rootwindow.resizable(False, False)
wn.title("Pong")
wn.setup(width=800, height=600)

Single_Multi = 0


class Button(turtle.Turtle):
    def __init__(self, s, text, x, y, w, h, c, i, p, a=None, b2=None, b3=None):
        turtle.Turtle.__init__(self)
        self.msg = text
        self.x = x
        self.y = y
        self.pensize = p
        self.width = w
        self.height = h
        self.colour = c
        self.action = a
        self.screen = s
        self.i_color = i
        self.button2 = b2
        self.button3 = b3
        self.screen.tracer(0)
        self.ht()
        self.color(self.colour)
        self.penup()
        self.begin_fill()
        self.goto(self.x, self.y)
        self.pendown()
        self.goto(self.x + self.width, self.y)
        self.goto(self.x + self.width, self.y + self.height)
        self.goto(self.x, self.y + self.height)
        self.goto(self.x, self.y)
        self.end_fill()
        self.penup()
        self.goto(self.x + self.width / 2, self.y + self.height / 3.5)
        self.color("black")
        self.write(self.msg, False, align="center", font=("Arial", self.pensize, "bold"))
        self.screen.update()

    def check(self, f, b):
        if f < self.x + self.width and f > self.x and b > self.y and b < self.y + self.height and self.action != None:
            turtle.listen()
            turtle.onscreenclick(None, 1)
            self.color(self.colour, self.i_color)
            self.begin_fill()
            self.goto(self.x, self.y)
            self.pendown()
            self.goto(self.x + self.width, self.y)
            self.goto(self.x + self.width, self.y + self.height)
            self.goto(self.x, self.y + self.height)
            self.goto(self.x, self.y)
            self.end_fill()
            self.penup()
            self.goto(self.x + self.width / 2, self.y + self.height / 3.5)
            self.color("black")
            self.write(self.msg, False, align="center", font=("Arial", self.pensize, "bold"))
            self.screen.update()
            self.clear()
            self.action()
        elif self.button2 != None:
            self.button2.check(f, b)
            if self.button3 != None:
                self.button3.check(f, b)


def Multi_Player():
    global Single_Multi
    Single_Multi = 3


def Single_Player():
    global Single_Multi
    Single_Multi = 1

def Menu_Screen():
    wn = turtle.Screen()
    wn.clear()
    turtle.tracer(0)
    pen = turtle.Turtle()
    wn.bgcolor("black")
    pen.ht()
    pen.penup()
    pen.clear()
    button = Button(wn, "Single_Player", -95, 50, 150, 50, "white", "#8470ff", 15, Single_Player)
    button.intro = True
    button2 = Button(wn, "Multi_Player", -95, -10, 150, 50, "white", "#8470ff", 15, Multi_Player)
    button3 = Button(wn, "Exit", -95, -70, 150, 50, "white", "grey", 15, quit, button, button2)
    pen.penup()
    pen.goto(0, 100)
    pen.color("red")
    pen.write("Pong", False, align="center", font=("Arial", 90, "bold"))
    turtle.listen()
    turtle.onscreenclick(button3.check, 1)
    turtle.update()
    
    return Single_Multi
    


Menu_Screen()
wn.mainloop()

我无法让menu.py 中的主循环结束并将Single_Multi 返回到game.py,因为当我运行它时,它将运行菜单,然后在我按下按钮时冻结(退出除外,这可行很好)。

这包含完整的 menu.py 文件,所以没有任何混淆

如果您需要我添加更多代码,请告诉我,这已经困扰了我一天半

【问题讨论】:

  • 您能否提供有关您遇到的错误类型以及 wn.winloop() 的作用/它的代码的信息?
  • 当我按下菜单按钮时它会冻结,然后当我点击 X 退出时它会抛出这个: Traceback(最近一次调用最后一次):文件“game.py”,第 4 行,在 导入菜单文件“/home/alucard/other_python/Pygame/Space-invaders/Space-Invaders-Application-master/extra/menu.py”,第 113 行,在 wn.clear()
  • 文件 "/usr/lib/python3.8/turtle.py",第 1019 行,清晰的 self._delete("all") 文件 "/usr/lib/python3.8/turtle. py",第 557 行,在 delete self.cv.delete(item) 文件 "",第 1 行,在删除文件 "/usr/lib/python3.8/tkinter/__init_ 中。 py",第 2818 行,删除 self.tk.call((self._w, 'delete') + args) _tkinter.TclError: invalid command name ".!canvas"
  • "window.mainloop() 告诉 Python 运行 Tkinter 事件循环。此方法侦听事件,例如按钮单击或按键,并阻止其后的任何代码运行,直到它所在的窗口调用已关闭。"
  • 我还是很困惑,为什么需要在menu.py文件中调用Menu_Screen函数。

标签: python python-3.x python-turtle


【解决方案1】:

我找到了我的问题的答案,所以我会发布这个答案,以防将来对某人有所帮助


def Menu_Screen():
    wn = turtle.Screen()
    wn.clear()
    turtle.tracer(0)
    pen = turtle.Turtle()
    wn.bgcolor("black")
    pen.ht()
    pen.penup()
    pen.clear()
    button = Button(wn, "Single_Player", -95, 50, 150, 50, "white", "#8470ff", 15, Single_Player)
    button.intro = True
    button2 = Button(wn, "Multi_Player", -95, -10, 150, 50, "white", "#8470ff", 15, Multi_Player)
    button3 = Button(wn, "Exit", -95, -70, 150, 50, "white", "grey", 15, quit, button, button2)
    pen.penup()
    pen.goto(0, 100)
    pen.color("red")
    pen.write("Pong", False, align="center", font=("Arial", 90, "bold"))
    while 1 == 1:
        turtle.listen()
        turtle.onscreenclick(button3.check, 1)
        turtle.update()
        if Single_Multi != 0:
            break;


    wn.clear()
    return Single_Multi
    


    Menu_Screen()

if __name__ == "__main__":

    wn.mainloop()

我刚刚在 Menu_Screen() 函数中添加了一个无限循环,并在 if __name__ == "__main__": 下获得了 wn.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多