简体   繁体   中英

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. 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. 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" .

Just add enablePrint() to the other two options else clauses as well:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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