如何在Python 3.11中判断函数是否已运行?

huangapple go评论65阅读模式
英文:

how to know if a function has been run in python 3.11

问题

以下是要翻译的代码部分:

我有一个带有tkinterGUI按钮的代码当你按下按钮时例如我使用def块声明的press()函数会运行我想知道如何检查函数是否已经运行并在if块中使用它
示例

def press()
    print("hello world")
press()

while True:
    if press()已经运行
        print("已打印")

你能帮我吗谢谢
英文:

I have code that has a tkinter ( GUI ) button in it. when you press the button , for example , the press() function that I declared with a def block runs. i want to know how to check if a function has been run and use it in a if block.
example:

def press()
    print("hello world")
press()

while True() :
    if press() has been runned (?) :
        print("printed")

can you help me with this? thanks.

答案1

得分: 4

这是一个非常基本的解决方案,使用了一个标志

press_been_run = False

def press():
    print("hello world")
    global press_been_run
    press_been_run = True

press()

if press_been_run:
    print("printed")
英文:

This is a very basic solution by using a flag

press_been_run = False

def press():
    print("hello world")
    global press_been_run 
    press_been_run = True

press()

if press_been_run:
    print("printed")

huangapple
  • 本文由 发表于 2023年2月24日 16:51:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定