mercoledì 6 gennaio 2016

GUI con Python: messaggi

La libreria per fare le GUI si chiama Tkinter 
ESEMPIO:

from Tkinter import *
# if you are working under Python 3, comment the previous line and comment out the following line
#from tkinter import *

root = Tk()

w = Label(root, text="Hello Tkinter!")
w.pack()

root.mainloop()

farà apparire:
Hello Tkinter Windows

sabato 2 gennaio 2016

come comandare da python tramite seriale

riferimento: comandare robot da python

1. serve la libreria PySerial

  • Downloading and Installing the PySerial Library
Head over to this page for detailed PySerial documentation and this page to download the package.
che si usa semplicemente così, per inviare il carattere 'a' al PSOC:

import serial
"""
usare una volta sola per aprire com4
PSOCSerialPort = serial.Serial("COM4", 115200)
"""
PSOCSerialPort.write('a')


2. serve la libreria Tkinter 

per fare l'interfaccia grafica.

import serial
from Tkinter import *

#preparare le definizioni dei comandi
def ledOn():
PSOCSerialPort.write('a')
def ledOff():
PSOCSerialPort.write('A')
#comando da emettere una volta per non avere errore:
#PSOCSerialPort = serial.Serial("COM4", 115200)

appWindow = Tk() # creates the application window (you can use any name)
appWindow.wm_title("LED Control") # displays title at the top left
# insert app widgets here (radiobuttons, checkboxes, buttons, etc.)

var = IntVar() # define var as an integer variable
onButton = Radiobutton(appWindow, text="LED ON", variable = var, value = 1, command=ledOn)
onButton.pack( anchor = W ) # this part adjusts the radiobuttons to the parent window
offButton = Radiobutton(appWindow, text="LED OFF", variable = var, value = 2, command=ledOff)
offButton.pack( anchor = W ) # this part adjusts the radiobuttons to the parent window

appWindow.mainloop() # main loop. From here, the GUI starts updating and responding to user inputs.

verrà creata questa GUI:

che manderà 'a' oppure 'A' a seconda della scelta

domenica 13 dicembre 2015