-1

Hello every one i am having some trouble with my python gui app i created, the app works well except for when i click on the sensor mode button. the value "m" just stays the same as when the gui - app first loaded and does not refresh. I have only started using tkinter that past month and i am struggling to understand how to implement a while loop or something similar. I placed a while loop inside the main and all it seems to do is just reopen the app when i close it. The app was created on my raspberryPi so i commented out all the gpio and raspberrypi stuff so it will work on pc,s. the app is basicly 2 modes a timer mode and a sensor mode though i have not put in any a function for when the value m "the sensor" drops below 50% but i already have a different sensor program that i will use when i get the sensor to update.

May some one Please fix my app so that the sensor value m - mpc3008 constantly refreshes.

    # made by edwin martin 02/05/2016

    #!/usr/bin/python
    from datetime import datetime
    from threading import Timer
    import sys
    import time
    import threading
    import tkinter
    from tkinter import *
    from time import sleep
    from time import sleep
    #t1 = 0
    #t2 = 0
    #t3 = 0
    #t4 = 0

    class simpleapp_tk(tkinter.Tk, threading.Thread):
        def __init__(self,parent):
            tkinter.Tk.__init__(self,parent)
            self.parent = parent
            self.initialize()
            self.minsize(width=720, height=180)
            self.maxsize(width=720, height=240)
        def initialize(self):
            self.grid()
    #lables
            self.labelVariable2 = tkinter.StringVar()
            label2 = tkinter.Label(self,textvariable=self.labelVariable2,
                                  anchor="w",fg="white",bg="blue")
            label2.grid(column=0,row=0,columnspan=2,sticky='EW')
            self.labelVariable2.set(u"ENTER THE HOUR FOR THE START TIME ")

            self.entryVariable = tkinter.IntVar()
            self.entry = tkinter.Entry (self,textvariable=self.entryVariable)
            self.entry.grid(column=0,row=1,sticky='EW')
            self.entry.bind("<Return>", self.OnPressEnter)
            self.entryVariable.set(u"Enter text here.")

            self.labelVariable = tkinter.StringVar()
            label = tkinter.Label(self,textvariable=self.labelVariable,
                                  anchor="w",fg="black",bg="yellow")
            label.grid(column=0,row=2,columnspan=2,sticky='EW')
            self.labelVariable.set(u"")

            self.labelVariable4 = tkinter.StringVar()
            label4 = tkinter.Label(self,textvariable=self.labelVariable4,
                                  anchor="w",fg="white",bg="blue")
            label4.grid(column=0,row=3,columnspan=2,sticky='EW')
            self.labelVariable4.set(u"ENTER THE MINUTE FOR THE START TIME")

            self.entryVariable2 = tkinter.IntVar()
            self.entry = tkinter.Entry(self,textvariable=self.entryVariable2)
            self.entry.grid(column=0,row=4,sticky='EW')
            self.entry.bind("<Return>", self.OnPressEnter)
            self.entryVariable2.set(u"Enter text here.")

            self.labelVariable1 = tkinter.StringVar()
            label5 = tkinter.Label(self,textvariable=self.labelVariable1,
                                  anchor="w",fg="black",bg="grey")
            label5.grid(column=0,row=6,columnspan=2,sticky='EW')

            self.labelVariable3 = tkinter.StringVar()
            label3 = tkinter.Label(self,textvariable=self.labelVariable3,
                                  anchor="w",fg="black",bg="yellow")
            label3.grid(column=0,row=5,columnspan=2,sticky='EW')
    #buttons

            button = tkinter.Button(self,text=u"ENTER",
                                    command=self.OnButtonClick,anchor="w",fg="black",bg="green")
            button.grid(column=1,row=1)

            button2 = tkinter.Button(self,text=u"ENTER",
                                    command=self.OnButtonClick2,anchor="w",fg="black",bg="green")
            button2.grid(column=1,row=4)

            button3 = tkinter.Button(self,text="START TIMER",
                                    command=self.OnButtonClick3,anchor="w",fg="black",bg="green")
            button3.grid(column=0,row=6)

            button4 = tkinter.Button(self,text="PREVIEW PAGE",
                                    command=self.OnButtonClick4,anchor="w",fg="black",bg="green")
            button4.grid(column=1,row=6)

            button5 = tkinter.Button(self,text="SENSOR MODE",
                                    command=self.OnButtonClick5,anchor="w",fg="black",bg="purple")
            button5.grid(column=0,row=9)

            self.labelVariable7 = tkinter.StringVar()
            label7 = tkinter.Label(self,textvariable=self.labelVariable7,
                                    anchor="w",fg="black",bg="white")
            label7.grid(column=0,row=8,columnspan=2,sticky='EW')

            self.labelVariable8 = tkinter.StringVar()
            label8 = tkinter.Label(self,textvariable=self.labelVariable8,anchor="w",fg="black",bg="pink")
            label8.grid(column=0,row=7,columnspan=2,sticky='EW')

            self.grid_columnconfigure(0,weight=1)
            self.resizable(True,False)
            self.update()
            self.geometry(self.geometry())       
            self.entry.focus_set()
            self.entry.selection_range(0, tkinter.END)

        def light_on(self):
            print ("led on")
            self.grid()
            self.labelVariable7 = tkinter.StringVar()
            label7 = tkinter.Label(self,textvariable=self.labelVariable7,
                                    anchor="w",fg="black",bg="red")
            label7.grid(column=0,row=8,columnspan=2,sticky='EW')
            self.labelVariable7.set(u"!!WATERING !!")
            slep(5)
            self.grid()
            self.labelVariable7 = tkinter.StringVar()
            label7 = tkinter.Label(self,textvariable=self.labelVariable7,
                                    anchor="w",fg="black",bg="white")
            label7.grid(column=0,row=8,columnspan=2,sticky='EW')
            self.labelVariable7.set(u"")

        def OnButtonClick(self):
            self.labelVariable.set( "You entered: " + str(self.entryVariable.get()))
            self.entry.focus_set()
            self.entry.selection_range(0, tkinter.END)

        def OnButtonClick2(self):
            self.labelVariable3.set( "You entered: " + str(self.entryVariable2.get()))
            self.entry.focus_set()
            self.entry.selection_range(0, tkinter.END)

        def OnButtonClick3(self):
            time=datetime.today()
            int (self.entryVariable.get())
            int (self.entryVariable2.get())
            start=time.replace(day=time.day, hour=self.entryVariable.get(), minute=self.entryVariable2.get(), second=0, microsecond=0)
            delta_t=start-time
            secs=delta_t.seconds+1
            timer = Timer(secs,self.light_on)
            timer.start()
            self.labelVariable8 = tkinter.IntVar()
            label8 = tkinter.Label(self,textvariable=self.labelVariable8,
                                    anchor="w",fg="black",bg="pink")
            label8.grid(column=0,row=7,columnspan=2,sticky='EW')
            if self.entryVariable.get() >= 12:
                self.labelVariable8.set( "start time: " + str(self.entryVariable.get())+ ":" + str(self.entryVariable2.get()) + "pm")
            elif self.entryVariable.get() < 12:
                self.labelVariable8.set( "start time: " + str(self.entryVariable.get())+ ":" + str(self.entryVariable2.get()) + "am")

        def OnButtonClick4(self):
            for widget in app.winfo_children():
                widget.destroy()
                self.grid()
                self.labelVariable2 = tkinter.StringVar()
                label2 = tkinter.Label(self,textvariable=self.labelVariable2,
                                      anchor="w",fg="white",bg="blue")
                label2.grid(column=0,row=0,columnspan=2,sticky='EW')
                self.labelVariable2.set(u"WATERING START TIME ")

                self.labelVariable = tkinter.IntVar()
                label = tkinter.Label(self,textvariable=self.labelVariable,
                                      anchor="w",fg="black",bg="pink")
                label.grid(column=0,row=1,columnspan=2,sticky='EW')



        def OnPressEnter(self,event):
            print("enter button pressed\nplease use the mouse instead !")

#NOT WORKING IN THIS SECTION WHERE M IS CALLED , THE VALUE JUST STAYS THE SAME AND DOES NOT REFRESH    ???????

        def display(self):
            self.grid()
            self.labelVariable = tkinter.IntVar()
            label = tkinter.Label(self,textvariable=self.labelVariable,
                                    anchor="w",fg="black",bg="pink")
            label.grid(column=0,row=1,columnspan=2,sticky='EW')
            self.labelVariable.set("Water Level:%d%%" % m)

        def OnButtonClick5(self):


# THIS IS JUST SHOWING AFTER 2  SECONDS, I WANT TO REFRESH EVERY 2 SECONDS              
            app.after(200, self.display)
# WORKING OK HERE THIS JUST DISPLAYS A HEADER FOR THE SENSOR PAGE       
            for widget in app.winfo_children():
                widget.destroy()
                self.grid()
                self.labelVariable2 = tkinter.StringVar()
                label2 = tkinter.Label(self,textvariable=self.labelVariable2,
                                      anchor="w",fg="white",bg="blue")
                label2.grid(column=0,row=0,columnspan=2,sticky='EW')
                self.labelVariable2.set(u"WATERING LEVEL")
# M IS LOADED HERE BUT DOES NOT REFRESH
    if __name__ == "__main__":
             #m = mcp3008.readadc(5)/10+19
             app = simpleapp_tk(None)
             app.title('WATER BUDDY APP') 
             app.mainloop()

This last part is where the problem lies

       def display(self):
            self.grid()
            self.labelVariable = tkinter.IntVar()
            label = tkinter.Label(self,textvariable=self.labelVariable,
                                    anchor="w",fg="black",bg="pink")
            label.grid(column=0,row=1,columnspan=2,sticky='EW')
            self.labelVariable.set("Water Level:%d%%" % m)

        def OnButtonClick5(self):
# THIS IS JUST SHOWING AFTER 2  SECONDS, I WANT TO REFRESH EVERY 2 SECONDS              
            app.after(200, self.display)
# WORKING OK HERE THIS JUST DISPLAYS A HEADER FOR THE SENSOR PAGE       
            for widget in app.winfo_children():
                widget.destroy()
                self.grid()
                self.labelVariable2 = tkinter.StringVar()
                label2 = tkinter.Label(self,textvariable=self.labelVariable2,
                                      anchor="w",fg="white",bg="blue")
                label2.grid(column=0,row=0,columnspan=2,sticky='EW')
                self.labelVariable2.set(u"WATERING LEVEL")
# M IS LOADED HERE BUT DOES NOT REFRESH
    if __name__ == "__main__":
             #m = mcp3008.readadc(5)/10+19
             app = simpleapp_tk(None)
             app.title('WATER BUDDY APP') 
             app.mainloop()
edwin
  • 181
  • 2
  • 10
  • 2
    Can you please reduce your example to a bare minimum? This helps the people to understand where the problem might be – salomonderossi May 03 '16 at 11:46
  • I edited the code quickly so it still runs and put it capital comments where the problem lies fyi the problem is located right at the bottom of the code in the function __display__ and __main__ – edwin May 03 '16 at 12:32
  • @edwin I believe they were asking you to produce an [MCVE](http://stackoverflow.com/help/mcve). With Tkinter and this particular problem that should take 10-15 lines of code or less. But, this is a duplicate anyway. Also note - `after` takes milliseconds. 200 will refresh every 0.2 seconds. You want *`2000`*, for 2 seconds. – Wayne Werner May 03 '16 at 12:48
  • ty i shall look into what you linked. and oh ok i did not know what MCVE is. lesson learnt and thank you for the upvote :). and yes i realized that it was ms when i read over it all again last night. many thanks from edd – edwin May 03 '16 at 12:54

1 Answers1

0

WOOOO ty wayne :) yes all i had to do was change 1 line and paste another in. Thank you again.

   def display(self):
        import mcp3008
        m = mcp3008.readadc(5)/10+19
        self.grid()
        self.labelVariable = tkinter.IntVar()
        label = tkinter.Label(self,textvariable=self.labelVariable,
                                anchor="w",fg="black",bg="pink")
        label.grid(column=0,row=1,columnspan=2,sticky='EW')
        self.labelVariable.set("Water Level:%d%%" % m)
        self.after(200, self.display)
    def OnButtonClick5(self):
        app.after(200, self.display)    
        for widget in app.winfo_children():
            widget.destroy()
            self.grid()
            self.labelVariable2 = tkinter.StringVar()
            label2 = tkinter.Label(self,textvariable=self.labelVariable2,
                                  anchor="w",fg="white",bg="blue")
            label2.grid(column=0,row=0,columnspan=2,sticky='EW')
            self.labelVariable2.set(u"WATERING LEVEL")
if __name__ == "__main__":
         app = simpleapp_tk(None)
         app.title('WATER BUDDY APP') 
         app.mainloop()
edwin
  • 181
  • 2
  • 10
  • How do i stop this loop once it starts, as i now made a back to timer button in the sensor page but when it loads the page the timer keeps running and overlaps the new page ????? – edwin May 03 '16 at 14:50