因为要做一个船模的控制界面,因此写了以下的程序
from tkinter import *
class boatGui():
def __init__(self):
self.root=Tk()
self.root.title("BOAT GUI")
# #s0gfdp
#接收信号#s0g0dp
self.strSingnal="#s0g000"
self.singnal=list(self.strSingnal)
self.test=StringVar()
self.Fjingdu=0.0
self.Fweidu=0.0
self.jingdu=StringVar()
self.weidu=StringVar()
self.test.set(self.strSingnal)
self.jingdu.set(str(self.Fjingdu))
self.weidu.set(str(self.Fweidu))
pumbButton=Button(self.root,text="PUMP",font=('Arial', 20),width=8,command=self.pumb)
upButton=Button(self.root,text="UP",font=('Arial', 20),width=8,command=self.up)
#downButton=Button(self.root,text="DOWN",font=('Arial', 20),width=8,command=self.down)
leftButton=Button(self.root,text="LEFT",font=('Arial', 20),width=8,command=self.left)
rightButton=Button(self.root,text="RIGHT",font=('Arial', 20),width=8,command=self.right)
stopButton=Button(self.root,text="STOP",font=('Arial', 20),width=8,command=self.stop)
readyButton=Button(self.root,text="Ready",font=('Arial', 20),width=8,command=self.ready)
chargeButton=Button(self.root,text="Charge",font=('Arial', 20),width=8,command=self.charge)
pumbButton=Button(self.root,text="Pumb",font=('Arial', 20),width=8,command=self.pumb)
gpsButton=Button(self.root,text="GPS",font=('Arial', 20),width=8,command=self.gps)
dataButton=Button(self.root,text="Data",font=('Arial', 20),width=8,command=self.data)
testLabel=Label(self.root,textvariable=self.test,font=('Arial',20),width=8)
jingduLabel=Label(self.root,textvariable=self.jingdu,font=('Arial',20),width=8)
weiduLabel=Label(self.root,textvariable=self.weidu,font=('Arial',20),width=8)
stopButton.grid(column=0,row=0)
gpsButton.grid(column=1,row=0)
readyButton.grid(column=0,row=1)
dataButton.grid(column=1,row=1)
chargeButton.grid(column=0,row=2)
upButton.grid(column=2,row=2)
pumbButton.grid(column=0,row=3)
leftButton.grid(column=1,row=3)
rightButton.grid(column=3,row=3)
testLabel.grid(column=3,row=1)
jingduLabel.grid(column=2,row=0)
weiduLabel.grid(column=3,row=0)
#downButton.grid(column=2,row=4)
self.root.bind('<KeyPress-A>',self.left)
self.root.bind('<KeyPress-D>',self.right)
self.root.bind('<KeyPress-W>',self.up)
self.root.mainloop()
def sendSingnal(self):
return self.strSingnal
def updateSin(self):
self.jingdu.set(str(self.Fjingdu))
self.weidu.set(str(self.Fweidu))
self.strSingnal=''.join(self.singnal)
self.test.set(self.strSingnal)
self.sendSingnal()
def ready(self):
self.singnal[2]='r'
self.updateSin()
def stop(self):
self.singnal[2]='s'
self.updateSin()
def pumb(self):
self.singnal[6]='p'
self.updateSin()
def charge(self):
self.singnal[2]='c'
self.updateSin()
def gps(self):
self.singnal[3]='g'
self.updateSin()
def up(self,TEMP=''):
self.singnal[4]='u'
print('up\n')
self.updateSin()
#def down(self):
# self.singnal[4]='d'
# self.updateSin()
def left(self,TEMP=''):
self.singnal[4]='l'
self.updateSin()
print("left\n")
def right(self,TEMP=''):
self.singnal[4]='r'
self.updateSin()
print("right\n")
def data(self):
self.singnal[5]='d'
self.updateSin()
boat=boatGui()