|
楼主 |
发表于 2020-3-8 10:04:01
|
显示全部楼层
程序怎么打包成函数呀,求助,比如说这个程序,其中有多个类
import RPi.GPIO as GPIO
import time
import numpy as np
from tkinter import *
import XY
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PX, GPIO.OUT)
GPIO.setup(DX, GPIO.OUT)
GPIO.setup(PY, GPIO.OUT)
GPIO.setup(DY, GPIO.OUT)
def rightStep():
setStepper1(1, 0)
setStepper1(0, 0)
setStepper1(1, 0)
setStepper1(0, 0)
def leftStep():
setStepper1(1, 1)
setStepper1(0, 1)
setStepper1(1, 1)
setStepper1(0, 1)
def forwardStep():
setStepper2(1, 0)
setStepper2(0, 0)
setStepper2(1, 0)
setStepper2(0, 0)
def backwardStep():
setStepper2(1, 1)
setStepper2(0, 1)
setStepper2(1, 1)
setStepper2(0, 1)
def setStepper1(in1, in2):
GPIO.output(PX, in1)
GPIO.output(DX, in2)
time.sleep(float(inp2.get()))
def setStepper2(in1, in2):
GPIO.output(PY, in1)
GPIO.output(DY, in2)
time.sleep(float(inp2.get()))
def up():
a=int(inp1.get())
for i in range(a):
forwardStep()
XY.Y=XY.Y+1
def down():
a=int(inp1.get())
for i in range(a):
backwardStep()
XY.Y=XY.Y-1
def left():
a=int(inp1.get())
for i in range(a):
leftStep()
XY.X=XY.X-1
def right():
a=int(inp1.get())
for i in range(a):
rightStep()
XY.X=XY.X+1
def move():
XY.A=XY.X
XY.B=XY.Y
if(XY.X>=0):
for i in range(XY.X):
leftStep()
XY.X=XY.X-1
if(XY.X<0):
for i in range(abs(XY.X)):
rightStep()
XY.X=XY.X+1
if(XY.Y>=0):
for i in range(XY.Y):
backwardStep()
XY.Y=XY.Y-1
if(XY.Y<0):
for i in range(abs(XY.Y)):
forwardStep()
XY.Y=XY.Y+1
def back():
XY.X=XY.A
XY.Y=XY.B
if(XY.B>=0):
for i in range(XY.B):
forwardStep()
if(XY.B<0):
for i in range(abs(XY.B)):
backwardStep()
if(XY.A>=0):
for i in range(XY.A):
rightStep()
if(XY.A<0):
for i in range(abs(XY.A)):
leftStep()
def Repeat():
while True:
forwardStep()
M=XY.X
N=XY.Y
if(M>=0):
for i in range(M):
leftStep()
if(M<0):
for i in range(abs(M)):
rightStep()
if(N>=0):
for i in range(N):
backwardStep()
if(N<0):
for i in range(abs(N)):
forwardStep()
time.sleep(5)
if(N<0):
for i in range(N):
backwardStep()
if(N>=0):
for i in range(abs(N)):
forwardStep()
if(M<0):
for i in range(M):
leftStep()
if(M>=0):
for i in range(abs(M)):
rightStep()
time.sleep(5)
def zero():
XY.X=0
XY.Y=0
XY.A=0
XY.B=0
def Key(a):#keyborad
if(a.keysym=='Up'):
forwardStep()
XY.Y=XY.Y+1
elif(a.keysym=='Down'):
backwardStep()
XY.Y=XY.Y-1
elif(a.keysym=='Right'):
rightStep()
XY.X=XY.X+1
elif(a.keysym=='Left'):
leftStep()
XY.X=XY.X-1
def getxy():
lab3.configure(text=("(%d,%d)" %(XY.X,XY.Y)))
root.after(1,getxy)
root=Tk()
root.geometry('480x480')
root.title('3+1物创操作窗口1.0')
PX = 18
DX = 12
PY = 32
DY = 37
btn1=Button(root,text='Up',command=up) #up
btn1.place(relx=0.2, rely=0.1, relwidth=0.1, relheight=0.1)
btn2=Button(root,text='Down',command=down) #down
btn2.place(relx=0.2, rely=0.2, relwidth=0.1, relheight=0.1)
btn3=Button(root,text='Left',command=left) #left
btn3.place(relx=0.1, rely=0.2, relwidth=0.1, relheight=0.1)
btn4=Button(root,text='Right',command=right) #right
btn4.place(relx=0.3, rely=0.2, relwidth=0.1, relheight=0.1)
btn5=Button(root,text='Capture',command=capture)#capture
btn5.place(relx=0.3, rely=0.1, relwidth=0.1, relheight=0.1)
inp1=Entry(root) #distance
inp1.place(relx=0.6, rely=0.1, relwidth=0.3, relheight=0.1)
inp2=Entry(root) #time
inp2.place(relx=0.6, rely=0.2, relwidth=0.3, relheight=0.1)
btn6=Button(root,text='Back Origin',command=move) #back
btn6.place(relx=0.3, rely=0.8, relwidth=0.2, relheight=0.1)
btn7=Button(root,text='Zero',command=zero) #
btn7.place(relx=0.2, rely=0.6, relwidth=0.1, relheight=0.1)
btn8=Button(root,text='Back ',command=back) #move
btn8.place(relx=0.6, rely=0.8, relwidth=0.1, relheight=0.1)
btn9=Button(root,text='Repeat',command=Repeat) #repeatbtn8.place(relx=0.8, rely=0.8, relwidth=0.1, relheight=0.1)
lab1=Label(root,text="Length:")
lab1.place(relx=0.4, rely=0.1, relwidth=0.2, relheight=0.1)
lab2=Label(root,text="Speed:")
lab2.place(relx=0.4, rely=0.2, relwidth=0.2, relheight=0.1)
lab3=Label(root,text='')
lab3.pack(padx=1,pady=206)
getxy()
lab4=Label(root,text="Coordinate:")
lab4.place(relx=0.2, rely=0.4, relwidth=0.2, relheight=0.1)
root.bind("<Key>",Key)
root.mainloop()
GPIO.cleanup()
[/code] |
|