ihm avec python tk
ihm avec python tk
REM: Exemples en python 3
Recopie d’un Message
|
|
#! /usr/bin/python3
from tkinter import *
########################################################################
# FUNCTIONS
########################################################################
def onPushCopy():
var = strSource.get() # var=sourceEntry.get()
print(var)
strDest.set(var) # destEntry.delete(0,END) # destEntry.insert(0,var)
sourceEntry.delete(0,END)
########################################################################
# UI
########################################################################
if __name__ == "__main__" :
ui=Tk()
ui.title("COPY STRING")
ui.geometry("200x200")
labelSource=Label(ui, text="Source :", font=("Arial", 10), fg="black")
labelSource.pack()
strSource=StringVar()
sourceEntry=Entry(ui, textvariable=strSource)
sourceEntry.pack()
copyButton=Button(ui, text="COPY", font=("Arial",10, "bold"), bg="seagreen3", fg="black", bd=3, relief=RAISED, command=onPushCopy)
copyButton.pack()
labelSource=Label(ui, text="Destination", font=("Arial", 10), fg="black")
labelSource.pack()
strDest=StringVar()
destEntry=Entry(ui, textvariable=strDest)
destEntry.pack()
ui.mainloop() # MAIN LOOP
########################################################################
Chronomètre
|
|
#! /usr/bin/python3
from tkinter import *
import time
########################################################################
# CLASS Chrono
########################################################################
class Chrono(Frame):
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self._start = 0.0
self._elapsedtime = 0.0
self._running = 0
self.timestr = StringVar()
self.makeWidgets()
def makeWidgets(self): # Make the time label
l = Label(self, textvariable=self.timestr)
self._setTime(self._elapsedtime)
l.pack(fill=X, expand=NO, pady=2, padx=2)
def _update(self): # Update the label with elapsed time
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._timer = self.after(50, self._update)
def _setTime(self, elap): # Minutes:Seconds:Hundreths
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
def Start(self): # Start the stopwatch, ignore if running
if not self._running:
self._start = time.time() - self._elapsedtime
self._update()
self._running = 1
def Stop(self):
if self._running:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._running = 0
def Reset(self):
self._start = time.time()
self._elapsedtime = 0.0
self._setTime(self._elapsedtime)
########################################################################
# MAIN
########################################################################
if __name__ == "__main__" :
ui = Tk()
ui.title("Timer")
chron = Chrono(ui)
chron.pack(side=TOP)
Button(ui, text='Start', command=chron.Start).pack(side=LEFT)
Button(ui, text='Stop', command=chron.Stop).pack(side=LEFT)
Button(ui, text='Reset', command=chron.Reset).pack(side=LEFT)
Button(ui, text='Quit', command=ui.quit).pack(side=LEFT)
ui.mainloop()
########################################################################
Python-TK et laison série UART (RS232)
|
|
#! /usr/bin/python3
import serial
import time
from tkinter import *
port='/dev/ttyACM0'
baudrate=115200
serBuffer = ""
########################################################################
# FUNCTIONS
########################################################################
def onPushSend():
var=strToSend.get()
ser.write(var.encode())
#ser.write(b'4ab2') # write a string
def receive():
reading=[]
while (ser.inWaiting()>0):
if (ser.inWaiting() > 0):
reading.append(ser.read(1).decode())
time.sleep(0.001)
strReceived.set(reading)
ser.flush()
ui.after(1,receive)
########################################################################
# MAIN
########################################################################
if __name__ == "__main__" :
try:
ser = serial.Serial(port, baudrate, bytesize=8, parity='N', stopbits=1, timeout=None, rtscts=False, dsrdtr=False)
print("serial port " + ser.name + " opened")
except Exception:
print("error open serial port: " + port)
exit()
ui=Tk()
ui.title("SERIAL")
ui.geometry("200x200")
labelToSendMes=Label(ui, text="Message to Send", font=("Arial", 10), fg="black")
labelToSendMes.pack()
strToSend=StringVar()
toSendEntry=Entry(ui, textvariable=strToSend)
toSendEntry.pack()
copyButton=Button(ui, text="SEND", font=("Arial",10, "bold"), bg="seagreen3", fg="black", bd=3, relief=RAISED, command=onPushSend)
copyButton.pack()
labelReceivedMes=Label(ui, text="Received Message", font=("Arial", 10), fg="black")
labelReceivedMes.pack()
strReceived=StringVar()
receivedEntry=Entry(ui, textvariable=strReceived)
receivedEntry.pack()
ui.after(1,receive)
ui.mainloop() # MAIN LOOP
########################################################################
Installation
$ sudo apt-get install python python3-tk python3-serial