This is an old revision of the document!


Raspberry Pi näitekoodid

Lihtsad näited Python shell

5+9
 
print („Tere Kevad”)
 
56/5+99-14*9
 
print („Tere Kevad”)
 
3 > 7
 
for x in range (1,10):
	print (x)

Lihtsad näited Python skript

kaal = float(input("Kui palju sa umbes kaalud? "))
if kaal > 100:
   print("Tõenäoliselt oled natuke ülekaaluline.")
elif kaal >50:
   print("OK või käib küll")
else:
   print ("Oled alakaaluline või paremal juhul laps")

Python graafiline kasutajaliides

import tkinter as tk
 
class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.pack()
        self.createWidgets()
 
    def createWidgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Olen aknaga programm\n(vajuta siia)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")
 
        self.QUIT = tk.Button(self, text="SULGE", fg="red",command=root.destroy)
        self.QUIT.pack(side="bottom")
 
    def say_hi(self):
        print("Tere kevad!")
 
root = tk.Tk()
app = Application(master=root)
root.geometry("250x150+300+300")
app.mainloop()

Valvenäide

import RPi.GPIO as GPIO
import time
import datetime
 
sensorPin = 7
 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
 
prevState = False
currState = False
 
f = open('andur.log', 'w',0)
 
 
while True:
    time.sleep(0.1)
    prevState = currState
    currState = GPIO.input(sensorPin)
    if currState != prevState:
        newState = "1" if currState else "0"
        aeg=datetime.datetime.now()
        print "%s, %s" % (aeg,newState)
        #f.write(str(aeg)+","+newState+'\n')
        f.write(newState+'\n')
raspi.1400148770.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0