python tkinter,传感器值不会刷新

时间:2016-05-03 11:40:14

标签: python loops tkinter

大家好我在使用我创建的python gui应用程序时遇到了一些问题,除了点击传感器模式按钮之外,应用程序运行良好。价值" m"只是保持与gui-app首次加载并且不刷新时相同。我过去一个月才开始使用tkinter,我很难理解如何实现while循环或类似的东西。我在主要内部放置了一个while循环,所有它似乎只是在我关闭它时重新打开应用程序。该应用程序是在我的raspberryPi上创建的,所以我注释掉了所有gpio和raspberrypi的东西,所以它可以在pc上运行。应用程序基本上是2种模式,一种计时器模式和一种传感器模式,虽然我还没有输入任何一个函数,当值m"传感器"降到50%以下,但我已经有了一个不同的传感器程序,我将在传感器更新时使用。

请一些人请修复我的应用,以便传感器值m - mpc3008不断刷新。

    # 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()

最后一部分是问题所在

       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()

1 个答案:

答案 0 :(得分:0)

WOOOO ty wayne :) 是的,我所要做的就是改变1行并粘贴另一行。 再次感谢你。

   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()