Jul-04-2022, 03:07 AM
(This post was last modified: Jul-04-2022, 03:07 AM by deanhystad.)
It doesn't make any sense to compare EntOn() to anything. The function always returns None. But this doesn't matter because what you are doing with the Histo panel doesn't make any sense either.
I think the Histo panel is supposed to be a log of events that occurred while the program is running. When you turn the lights on you add a log message. When you turn the lights off you add a log message. Each log message has a timestamp and a description of what happened. For that to happen the Histo panel must be created at the start of the program, not when the user presses the Afficher button. The only thing the Afficher button should do is show() the panel.
I think the Histo panel would make a great class. In addition to __init__() which makes the widgets the class would have a log() method for adding a messag to the log.
Answers to questions in your last post:
ct does not change because it is assigned a value once, at the start of the program. A datetime object does not change to reflect the current time. You need to call ct = datetime.now() each time you want ct to have the current time.
You history window only shows one line because you make a new history window each time you press the Afficher button.
The value of a function is None unless the function has a "return". This function returns 42
This comment from you post makes no sense to me:
I think the Histo panel is supposed to be a log of events that occurred while the program is running. When you turn the lights on you add a log message. When you turn the lights off you add a log message. Each log message has a timestamp and a description of what happened. For that to happen the Histo panel must be created at the start of the program, not when the user presses the Afficher button. The only thing the Afficher button should do is show() the panel.
I think the Histo panel would make a great class. In addition to __init__() which makes the widgets the class would have a log() method for adding a messag to the log.
class Histo(Toplevel): """A window for viewing log messages""" def __init__(self): super().__init__() self.title('Historique') self.text = Text(self, width=40, height=10) self.text.pack(padx=5, pady=5) def log(self, msg): """Append log message to text""" now = datetime.datetime.now() self.text.insert(INSERT, f"{now} {msg}")Now all the Afficher button has to do is show the window.
histo = Histo() btn6 = Button(root, text='Afficher', command=histo.show)The EntOn() function also does some things at the wrong time. When the user presses the On button the EntOn function should set the Ent output high, update the text of a label, and log a message.
def EntOn(): # Set the output GPIO.output(Ent, GPIO.HIGH) # Change the entOn label and log the event entOnLabel.config(text="On") histo.log(Lumière entrée allumée") # Histo panel needs to exist before this is calledAnd EntOff() should look like this:
def EntOff(): # Set the output GPIO.output(Ent, GPIO.LOW) # Change the entOn label and log the event entOnLabel.config(text="Off) histo.log("Lumière entrée fermée")Notice that neither of these configure the GPIO or create labels. That should be done once at the very start of the program, not every time a button is pressed.
Answers to questions in your last post:
ct does not change because it is assigned a value once, at the start of the program. A datetime object does not change to reflect the current time. You need to call ct = datetime.now() each time you want ct to have the current time.
You history window only shows one line because you make a new history window each time you press the Afficher button.
The value of a function is None unless the function has a "return". This function returns 42
def func(): return 42A function with no arguments can have a return value. A function with many arguments may not return a value. What is returned when a function executes depends on what follows "return" inside the function.
This comment from you post makes no sense to me:
Quote:The idea is to test if what's in the function is executed than execute if statementThere is no reason to test if a function is executed. If you call a function it will execute. Do you mean that you want to test if all the code in EntOn() executed successfully? What code are you concerned about? I don't see where you are checking anything inside EntOn.