Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 newbie
#1
Hi, I just started learning programming in Python3 on my own. I am wondering if someone be so kind to look at my code see if it make sense?
What this code does is taking sensor data from an Arduino, send data to Pi via serial
to dsplay data to the UI. the UI also has a button or two to control the relays from the Pi. I got the Label to display sensor data now. The button works fine as well. Any suggest to help me write better Pyphon code will be much appreciated.

import serial, time #random
from tkinter import *
from time import sleep

#Prepare GPIO
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False) #disable annoying warning meassages
GPIO.setup(40,GPIO.OUT)
#initial is on
GPIO.output(40,GPIO.HIGH)

#define the rearData function
def readData():
    #data = "Random number {}".format(random.randint(1, 99))
    data = ser.readline()
    #time.sleep(1)
    return data
    
#define toggle buttonfunction
def toggle():
    '''
    use
    t_btn.config('text')[-1]
    to get the present state of the toggle button
    '''
    if t_btn.config('text')[-1] == 'True':
        t_btn.config(text='False')
        GPIO.output(40,GPIO.LOW)
        
    else:
        t_btn.config(text='True')
        GPIO.output(40,GPIO.HIGH)

root = Tk()
root.geometry("800x400+0+0")
ser = serial.Serial('/dev/ttyUSB0', 9600)
var = StringVar()
var.set('hello')

dataLabel = Label(root, textvariable = var)
dataLabel.place(x=300, y=300)
#l.pack()
# Add a toggle button
t_btn = Button(root, text = "On/Off", command=toggle)
t_btn.place(x=380, y=350)

def updateValue(string_var, root_window):
    data = readData()
    string_var.set(data)
    root_window.after(1000, updateValue, string_var, root_window)


while True:
    #sleep(1) # Need this to slow the changes down
    #call readData()
    #data = readData()
    #print (str(data))
    #var.set(data)
    root.after(1000, updateValue, var, root)
    root.mainloop()
    
Reply


Messages In This Thread
Python3 newbie - by tony1812 - Jul-26-2017, 01:46 PM
RE: Python3 newbie - by nilamo - Jul-26-2017, 04:20 PM
RE: Python3 newbie - by tony1812 - Jul-27-2017, 09:18 AM
RE: Python3 newbie - by sparkz_alot - Jul-27-2017, 01:14 PM
RE: Python3 newbie - by Larz60+ - Jul-27-2017, 01:53 PM
RE: Python3 newbie - by tony1812 - Jul-27-2017, 03:20 PM
RE: Python3 newbie - by Larz60+ - Jul-27-2017, 05:16 PM
RE: Python3 newbie - by tony1812 - Jul-27-2017, 05:48 PM
RE: Python3 newbie - by Larz60+ - Jul-27-2017, 06:32 PM
RE: Python3 newbie - by tony1812 - Jul-27-2017, 07:47 PM
RE: Python3 newbie - by Larz60+ - Jul-27-2017, 08:01 PM
RE: Python3 newbie - by tony1812 - Jul-28-2017, 12:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,986 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020