我目前正在使用appJar开发一个python程序。这个程序是为Raspberry制作的,因为我想创建一个可移植的程序。我想通过使用按钮将一个虚拟键盘插入其中,而我目前成功地构建了一个。这是因为它对一个小小的触摸屏更有用。下面是完整程序的代码:
import csv
import RPi.GPIO as GPIO
import time
from appJar import gui
#all the names of the buttons are in italian
l=[]
class misure: #class where I manage all the measures and the functions of the entries
global l
def nome_file(self):
nome_file=app.getEntry("e1") +'.csv'
return nome_file
def verticale(self):
verticale=float(app.getEntry("e2"))
return verticale
def prof_tot(self):
prof_tot=float(app.getEntry("e3"))
return prof_tot
def dist_riv(self):
mis=float(app.getEntry("e2"))
return mis
def dist_fond(self):
dist_fond=float(app.getEntry("e4"))
return dist_fond
def cambia_mis(self):
app.clearEntry("e2")
app.clearEntry("e3")
app.clearEntry("e4")
app.clearLabel("e5")
app.showButton("Inizia misura")
app.setFocus("e2")
def cambia_prof(self):
prof=float(app.getEntry("e4"))
v=app.getLabel("e5")
l.append(prof)
l.append(v)
print(l)
app.clearEntry("e4")
app.showButton("Inizia misura")
def cambia_staz(self):
app.clearEntry("e1")
app.clearEntry("e2")
app.clearEntry("e3")
app.clearEntry("e4")
app.clearLabel("e5")
app.showButton("Inizia misura")
app.setFocus("e1")
def mulinello(self):
mulinello=app.getOptionBox("Mulinello")
return mulinello
def tempo_mis(self):
tempo_mis=app.getOptionBox("Secondi misurazione")
return float(tempo_mis)
class calcoli: #here are all of the calculation to transform rotation of an hydrometer reel to speed
def velocita(self,mulinello,giri):
v=0
giri_1s=0
if giri=='':
v=''
return v
giri=float(giri)
giri_1s=giri/30
if giri_1s==0:
v=0
return v
if mulinello=='125':
if giri_1s<1.98:
v=(1.93+(31.17*giri_1s))/100
return v
elif giri_1s<10.27:
v=(0.19+(32.05*giri_1s))/100
return v
else:
v=(-14.09+(33,44*giri_1s))/100
return v
elif mulinello=='80':
if giri_1s<1:
v=(2.8+(31.34*giri_1s))/100
return v
else:
v=(0.82+(33.32*giri_1s))/100
return v
elif mulinello=='50':
if giri_1s<1.74:
v=(1.23+(24.73*giri_1s))/100
return v
else:
v=(-0.42+(25.68*giri_1s))/100
return v
elif mulinello=='30':
if giri_1s<1.16:
v=(1.90+(10.57*giri_1s))/100
return v
else:
v=(2.26+(10.26*giri_1s))/100
return v
def conta_giri(self,temp_mis):
"""print(temp_mis)
giri=input('Inserire numero di giri') # use this if from computer so you don't have to use raspberry function
t_fine = time.time()+temp_mis
print(t_fine)
return giri"""
GPIO.setmode(GPIO.BOARD) #input from raspberry of the rotations
GPIO.setup(32,GPIO.IN)
#set up a counter
giri = 0
#set up a variable for reed activation
reed_state = 0
print("Misurazione in corso...")
t_fine = time.time()+temp_mis
#while loop until t_fine
while time.time()<t_fine:
#check if reed newly activated(the hydrometric reel works like an on and off circuit)
if GPIO.input(32) == 1 and reed_state == 0:
#turn on LED. Set reed_state to 1. Add to counter .
reed_state = 1
giri = giri + 1
#pause to debounce
time.sleep(.01)
#check if reed released
if GPIO.input(32) == 0 and reed_state == 1:
# set reed_state to 0
reed_state = 0
#now that loop has finished, print the final count
return giri
def funz(self): #function to start the measure of speed
c=calcoli()
m=misure()
v=c.velocita(m.mulinello(),c.conta_giri(m.temp_mis()))
v=round(v,4)
l=[m.dist_fond(),v]
app.setLabel("e5",v)
app.hideButton("Inizia misura")
file1=''
def inserisci_mis(self): #insert measure into csv file
global file1,mis_0,l
m=misure()
myFile = open(m.nome_file(),'a')
with myFile:
writer = csv.writer(myFile,lineterminator='\n')
if file1!=m.nome_file():
primaLinea = ["Distanza dalla verticale", "Distanza dalla riva", "Profondità"]+["Dist dal Fondo","Velocità"]*5
writer.writerow(primaLinea)
file1=m.nome_file()
mis_0=float(m.dist_riv())
prof=float(app.getEntry("e4"))
v=app.getLabel("e5")
l.append(prof)
l.append(v)
writer.writerow([m.verticale(),(m.verticale()-mis_0),m.prof_tot()]+l)
l=[]
app=gui() #appJar gui
app.setTitle("Water app")
app.setFont(size=12, family="Calibri")
app.addLabel("l1", "Misure",0,0)
app.addLabel("l2", "Velocità",5,0)
app.addEntry("e1",1,0)
app.addEntry("e2",2,0)
app.addEntry("e3",3,0)
app.addEntry("e4",4,0)
app.addLabel("e5","",6,0)
app.addButton("Inizia misura",funz,6,0)
app.addButton("Inserisci misura",inserisci_mis,4,1)
app.addButton("Altra profondità",misure.cambia_prof,3,1)
app.addButton("Cambia misura",misure.cambia_mis,2,1)
app.addButton("Cambia stazione",misure.cambia_staz,1,1)
app.setEntryDefault("e1", "Nome stazione")
app.setEntryDefault("e2", "Verticale n°")
app.setEntryDefault("e3", "Profondità totale")
app.setEntryDefault("e4", "Distanza dal fondo")
app.setGuiPadding(15, 5)
app.addLabelOptionBox("Mulinello", ['125', '80', '50','30'],5,1)
app.addLabelOptionBox("Secondi misurazione", ['15', '30', '60','120','180'],6,1,2)
app.setLabelFont(size=13,weight="bold")
app.setEntryWidths(["e1","e2","e3","e4"], 20)
app.setEntryRelief("e1", "raised")
app.setEntryRelief("e2", "raised")
app.setEntryRelief("e3", "raised")
app.setEntryRelief("e4", "raised")
#keyboard and number pad
app.addButtons([["1","2","3"],["4","5","4","6"],["7","8","9"],["0",".","O"]],press,1, 2, 3,4)
app.addButtons([["A","B","C","D"],["E","F","G","H"],["I","L","M","N"],["P","Q","R","S"],["T","U","V","Z"]],press,1,5,4,5)
app.go()当您按下字母和数字按钮时,可以回忆起:
def press(Button): #function to write into entries with buttons of the program
if Button=="A":
entry=''
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"A"
app.setEntry(focus,entry)
elif Button=="B":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"B"
app.setEntry(focus,entry)
elif Button=="C":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"C"
app.setEntry(focus,entry)
elif Button=="D":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"D"
app.setEntry(focus,entry)
elif Button=="E":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"E"
app.setEntry(focus,entry)
elif Button=="F":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"F"
app.setEntry(focus,entry)
elif Button=="G":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"G"
app.setEntry(focus,entry)
elif Button=="H":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"H"
app.setEntry(focus,entry)
elif Button=="I":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"I"
app.setEntry(focus,entry)
elif Button=="L":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"L"
app.setEntry(focus,entry)
elif Button=="M":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"M"
app.setEntry(focus,entry)
elif Button=="N":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"N"
app.setEntry(focus,entry)
elif Button=="O":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"O"
app.setEntry(focus,entry)
elif Button=="P":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"P"
app.setEntry(focus,entry)
elif Button=="Q":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"Q"
app.setEntry(focus,entry)
elif Button=="R":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"R"
app.setEntry(focus,entry)
elif Button=="S":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"S"
app.setEntry(focus,entry)
elif Button=="T":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"T"
app.setEntry(focus,entry)
elif Button=="U":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"U"
app.setEntry(focus,entry)
elif Button=="V":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"V"
app.setEntry(focus,entry)
elif Button=="Z":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"Z"
app.setEntry(focus,entry)
elif Button=="0":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"0"
app.setEntry(focus,entry)
elif Button=="1":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"1"
app.setEntry(focus,entry)
elif Button=="2":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"2"
app.setEntry(focus,entry)
elif Button=="3":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"3"
app.setEntry(focus,entry)
elif Button=="4":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"4"
app.setEntry(focus,entry)
elif Button=="5":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"5"
app.setEntry(focus,entry)
elif Button=="6":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"6"
app.setEntry(focus,entry)
elif Button=="7":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"7"
app.setEntry(focus,entry)
elif Button=="8":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"8"
app.setEntry(focus,entry)
elif Button=="9":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"9"
app.setEntry(focus,entry)
elif Button==".":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"."
app.setEntry(focus,entry)这是我的节目。我将appJar.py修改为appJar目录模块,使其不关注按钮(第488行-489-490行都有注释):
#self.topLevel.bind('<Button-1>', lambda e: _setFocus(e))
#self.topLevel.bind('<Button-2>', lambda e: _setFocus(e))
#self.topLevel.bind('<Button-3>', lambda e: _setFocus(e))我想问是否有一种更有效的方法来做键盘和数字垫,因为我认为有更好的方法来做它。或者是一些我可以在我的程序中修改以提高效率的东西。
发布于 2018-04-08 10:41:16
Python有一个正式的风格指南,PEP8。熟悉它不会有什么害处,因为它可以帮助您保持代码的可读性、一致性和可供其他人使用。
把意大利语和英语结合起来很快就会令人困惑。这对我们这些不懂意大利语的人也没有帮助。代码块之间也明显缺乏空格,一个字母的变量名称也没有用。
您的类使用global l,其中有l=[]。我不喜欢全球性的,尤其是在课堂上。请考虑以下几点:
class misure:
def __init__(self):
self.l = []
def nome_file(self):
my_misure = misure()
print(my_misure.l)突然之间,您可以有20个misure实例,因为它们都有自己的列表。类应该尽可能多地保存自己的数据。
以下几点可以大幅度改进:
def press(Button): #function to write into entries with buttons of the program
if Button=="A":
entry=''
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"A"
app.setEntry(focus,entry)
elif Button=="B":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"B"
app.setEntry(focus,entry)
elif Button=="C":
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"C"
app.setEntry(focus,entry)如果该entry应该执行您希望它做的事情,请在第一个if之前创建它。但既然你把它改写了,为什么不把它扔了?这就剩下以下几点:
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"character"
app.setEntry(focus,entry)所有的街区都是这样的,对吧?那为什么不把它放在一个函数里呢?
def we_put_it_in_a_function(character):
focus=app.getFocus()
entry=app.getEntry(focus)
entry=entry+"character"
app.setEntry(focus,entry)突然间,你的巨大的if块看起来是这样的:
def press(Button): #function to write into entries with buttons of the program
if Button=="A":
we_put_it_in_a_function("A"):
elif Button=="B":
we_put_it_in_a_function("B"):
elif Button=="C":
we_put_it_in_a_function("C"):
elif Button=="D":
we_put_it_in_a_function("D"):
elif Button=="E":
we_put_it_in_a_function("E"):
elif Button=="F":
we_put_it_in_a_function("F"):真正的击球手来了,它可以做得更短:
def press(Button): #function to write into entries with buttons of the program
we_put_it_in_a_function(Button):https://codereview.stackexchange.com/questions/191526
复制相似问题