[英]Python global var in If statement doesn't work
First, I am an absolute beginner and sorry if I ask stupid questions. 首先,我是一个绝对的初学者,如果我问愚蠢的问题,对不起。 I try to code a little program for school.
我尝试为学校编写一个小程序。
Imagine a motor with three emergency switches. 想象一下带有三个紧急开关的电动机。 A "Overheating", a "Circuit breaker" and a "manual"-switch witch all stop the motor.
“过热”,“断路器”和“手动”开关均会停止电动机。
In the program, the switches are simulated by tkinter-buttons in a little GUI. 在程序中,这些开关是通过一个小的GUI中的tkinter按钮进行模拟的。 If you press the button, it should output whatever case is simulated.
如果按下该按钮,它将输出模拟的任何情况。 If the motor "stopped" but a button (or a new button) is pressed again, a message "Machine already stopped" should appear.
如果电动机“已停止”,但再次按下了一个按钮(或一个新按钮),则将出现消息“机器已停止”。 But that last part of the program does not work.
但是该程序的最后一部分不起作用。
I've learned that vars in Python are local by default and so I tried to define the var "triggered" as global. 我了解到Python默认是本地变量,因此我尝试将“触发”的变量定义为全局变量。 But I've probably made some mistakes.
但是我可能犯了一些错误。 If I run the program, the first message (for example "Overheating!") appears but the second message "Machine already stopped" is missing when the button is pressed again.
如果我运行该程序,则再次按下该按钮时,将显示第一条消息(例如“过热!”),但是第二条消息“机器已停止”丢失了。
Can you tell me where my fault is? 你能告诉我我的错在哪里吗? I tried to google it but I don't know what is wrong.
我试图用谷歌搜索,但不知道出什么问题了。 Sometimes it is difficult to read threads or tutorials because I am not native english-speaking.
有时,由于我不是英语母语者,因此很难阅读主题或教程。
And please tell me if there's any pseudocode in there. 并且请告诉我那里是否有任何伪代码。 As I said I am an absolute beginner but I try hard to learn it.
正如我所说的,我是一个绝对的初学者,但我努力学习。
from tkinter import *
import sys, os
root = Tk()
root.title("Control Panel")
root.geometry("400x200")
app = Frame(root)
app.grid()
# Vars can be used later
overheat = False
# Stops motor if temperature is too high
circuitbreaker = False
# Stops if current flow is too high
manual = False
# Stops when switch is triggered manually
global triggered
triggered = False
# Returns True if one emergency unit has triggered
def Button_Overheat():
global triggered
if triggered == False:
triggered = True
print("Overheating!")
blockPrint()
else:
enablePrint()
print("Machine already stopped")
blockPrint
return
button_overheat = Button(app, text = "Overheat", command = Button_Overheat)
button_overheat.grid()
def Button_CircuitBreaker():
global triggered
if triggered == False:
print("Overload! Breaking Circuit...")
blockPrint()
else:
print("Machine already stopped")
blockPrint()
return
button_cicuitbreaker = Button(app, text = "Circuitbreaker", command = Button_CircuitBreaker)
button_cicuitbreaker.grid()
def Button_Manual():
global triggered
if triggered == False:
print("Machine was manually stopped")
blockPrint()
triggered = True
else:
print("Machine already stopped")
blockPrint()
return
button_manual = Button(app, text = "Turn off manually", command = Button_Manual)
button_manual.grid()
def blockPrint():
sys.stdout = open(os.devnull, 'w')
def enablePrint():
sys.stdout = sys.__stdout__
mainloop()
Please notice that other than in Overheating
you never re enabled printing to allow it to print "Machine already stopped"
. 请注意,除了在“
Overheating
您永远不会重新启用打印功能以允许其打印"Machine already stopped"
。
Just add enablePrint()
to the other two options else
clauses as well: 只需将
enablePrint()
添加到其他两个选项else
子句中:
def Button_CircuitBreaker():
global triggered
if triggered == False:
print("Overload! Breaking Circuit...")
blockPrint()
else:
enablePrint()
print("Machine already stopped")
blockPrint()
return
def Button_Manual():
global triggered
if triggered == False:
print("Machine was manually stopped")
blockPrint()
triggered = True
else:
enablePrint()
print("Machine already stopped")
blockPrint()
return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.