Python Tkinter while循环不停止

时间:2017-10-02 22:56:41

标签: python python-2.7 tkinter

#!/usr/bin/python
#--------------------------------------

使用步进电机打开和关闭空间加热器旋钮的程序

from Tkinter import *  
import pickle 
import sys 
import time
import RPi.GPIO as GPIO
from datetime import datetime, timedelta

#import motor state
filename="Motor_State"
fileobject=open(filename,'rb')
marker=pickle.load(fileobject)
fileobject.close()


global stepper

GPIO.setmode(GPIO.BCM)

StepPins = [17,18,27,22]

for pin in StepPins:
    GPIO.setup(pin,GPIO.OUT)
    GPIO.output(pin, False)    

time.sleep(0.5)

Seq = [[1,0,0,1],
       [1,0,0,0],
       [1,1,0,0],
       [0,1,0,0],
       [0,1,1,0],
       [0,0,1,0],
       [0,0,1,1],
       [0,0,0,1]]


StepCounter=0        
stepDir=0    
StepCount=8
user=0

#Get来自用户的输入(目前未使用,尝试设置GUI)

def CallInput():
    global user
    #print "StepMarker: ", (marker)
    while True:
        try:
            user=int(input("Enter steps or -999 to quit:"))
        except:
            print("Sorry, invalid response.")
            continue
        if (user==999):
                Schedule()
                continue
        if (user<0 or user>3000):
            if (user==-999):
                GPIO.cleanup()
                quit()

            print ("Cannot be less than 0 or greater than 3000.")   
            continue
        else:
            break
    return user

#Save motor state

def SaveMarker(state):
    fileobject=open(filename, 'wb')
    pickle.dump(marker,fileobject)
    fileobject.close()
    print ("marker saved, Goodbye!!!")

#Schedule打开加热器

def Schedule():
    import datetime
    print ("hello welcome to the Scheduler!")
    print ("")
    CallInput()
    year=2017
    month=datetime.datetime.now().strftime("%m")
    hour=input("please enter the hour: ")
    AMPM=str(raw_input("AM or PM: "))

    minute=input("please enter the minute: ")
    day=input("please enter the day: ")


    if (AMPM=="PM" or AMPM== "pm"):
        print (AMPM)
        print ("hour: ", hour)
        hour+=12
        print ("hour: ", hour)
    print (year, " ", month, " ", day, " ", hour, " ", minute)
    starttime=datetime.datetime(int(year),int(month),int(day),hour,minute)
    #if (hour<int(datetime.datetime.now().strptime("%h"))
        #oneday=datetime.timedelta(days=+1)
    while datetime.datetime.now() <starttime:
        time.sleep(1)
        print("youdidit",starttime, " ", datetime.datetime.now())
    print("youdidit",starttime, " ", datetime.datetime.now())
    RunMotor(user)

#GUI尝试

class Application(Frame):
    """ A GUI application with three buttons. """
    def __init__(self, master):
        """initialize the frame"""
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.instruction = Label(self, text= marker )
        self.instruction.grid(row=0, column=0, columnspan=2, sticky=W)

        self.userinput= Entry(`enter code here`self)
        self.userinput.grid(row=1, column=1, sticky=W)

        self.submit_button=Button(self, text= "Submit", command=self.reveal)
        self.submit_button.grid(row=2, column=0, sticky=W)

        self.text= Text(self, width=35, height=5, wrap=WORD)
        self.text.grid(row=3, column=0, columnspan=2, sticky=W)


    def reveal(self):
        stepper=self.userinput.get()

用于转动电机的#code

        global StepCount
        global StepCounter
        global StepDir
        global marker

帮助!这个while循环以某种方式结束无限的???????????? Noob在这里。它在Tkinter之外工作。我完全复制并粘贴了代码,它只是在点击提交按钮后永久执行,电机只是继续转动而且不会停止。当stepper = marker right?

时应该会中断
        while stepper!=marker:
                print StepCounter,
                print Seq[StepCounter], "stepper: ", (stepper), "Marker: ", (marker)
                if (stepper<marker):
                    stepDir=-1
                else:
                    stepDir=1

                for pin in range(0, 4):
                    xpin = StepPins[pin]
                    if Seq[StepCounter][pin]!=0:
                        GPIO.output(xpin, True)
                    else:
                        GPIO.output(xpin, False)
                marker+=stepDir
                StepCounter += stepDir

                # If we reach the end of the sequence
                # start again
                if (StepCounter>=StepCount):
                    StepCounter = 0
                if (StepCounter<0):
                    StepCounter = StepCount+stepDir

                # Wait before moving on
                time.sleep(.006)

                for pin in StepPins:
                    GPIO.output(pin, False)
        SaveMarker(marker)
        if content == marker:
            message = "content" + str(content)

        else:
            print "success"
            message ="marker" + str(marker) +"content" + str(content)
        self.text.insert(0.0, message)

root = Tk()
root.title("Passwords")
root.geometry("250x150")
app=Application(root)
root.mainloop()
  # Start main loop
#Old code
#while True:
#    CallInput()   
#    RunMotor(user)

import atexit
atexit.register(SaveMarker)

1 个答案:

答案 0 :(得分:0)

看起来stepper应该转换为数值,可能是int

stepper=self.userinput.get()
...
while stepper!=marker:
    ...
    if (stepper<marker):
    ...
    if content == marker:
        message = "content" + str(content)