简体   繁体   English

我如何在 Python 从一开始就执行 def 下的脚本列表

[英]How do I in Python from the start execute a list of script under def

Sorry I am considered quite new to Python but I still hope I can get help from here.对不起,我被认为是 Python 的新手,但我仍然希望我能从这里得到帮助。

So I made a rock paper scissor game, and the script needs to be executed again after one round is over.于是做了一个剪刀石头布的游戏,一轮结束后需要再次执行脚本。 The script looks something like this:脚本看起来像这样:

def introStuff():
  window.geometry("300x300")
  window.title("Rock Paper Scissor")
  welcomeMessage=Label(window, text="Welcome to Rock Paper Scissor!", font=("Arial Bold", 15))
  welcomeMessage.grid(column=0, row=0)
  welcomeButton=Button(window, text="Start game", bg="green", fg="white", command=startGame)
  welcomeButton.grid(column=0, row=3)

So I placed the script under def introStuff() .所以我把脚本放在def introStuff()下。 I know how to restart the script by using command=introStuff in a button that user clicks after one round, but the issue is that the script introStuff needs to execute upon launch to start a game of rock paper scissors, but I am unaware of any code that can accomplish that.我知道如何通过在用户在一轮后单击的按钮中使用command=introStuff来重新启动脚本,但问题是脚本introStuff需要在启动时执行才能开始石头剪刀布游戏,但我不知道有任何可以做到这一点的代码。 Any suggestions?有什么建议么?

Where to call the function? function在哪里打电话?

It depends how you launch your game.这取决于您如何启动游戏。 If there is some launch lifecycle hooks in whatever game framework you are using, then typically just calling your introStuff function in that framework launch sequence will work.如果在您使用的任何游戏框架中都有一些启动生命周期挂钩,那么通常只需在该框架启动序列中调用您的introStuff function 即可。

In other words, the framework you are using probably does some stuff when it is launched, and probably has provisioned ways to allow you to hook into this launch stage to run your own code.换句话说,您使用的框架在启动时可能会做一些事情,并且可能已经提供了允许您进入此启动阶段以运行自己的代码的方法。 It is simply a matter of determining how the framework exposes this functionality, which depends on the framework.这只是确定框架如何公开此功能的问题,这取决于框架。

How to call the function?如何调用 function?

You can call the function, which will execute the code indented below it, by using the parentheses operators.您可以使用括号运算符调用 function,它将执行缩进它下面的代码。 Since the function definition does not specify any parameters, to call the function we need not supply any arguments, so the parentheses are empty:由于 function 定义没有指定任何参数,调用 function 我们不需要提供任何 arguments,所以括号是空的:

introStuff()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM