/tkinter-led-control.py Secret
Last active
November 26, 2021 16:59
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk #for installing tkinter module: pip3 install tkinter | |
import tkinter.font as tkFont | |
import RPi.GPIO as GPIO #import GPIO pins | |
led=12 #led pin (board number) | |
GPIO.setmode(GPIO.BOARD) #select board mode | |
GPIO.setup(led,GPIO.OUT) #led pin as output | |
pwm=GPIO.PWM(led,50) #50hz pwm | |
pwm.start(0) | |
pwm.ChangeDutyCycle(0) #start as led OFF | |
pwm_data=100 | |
def button_on_command(): #when we press ON button this function is running | |
pwm.ChangeDutyCycle(pwm_data) #write pwm data to led pin | |
print("[Led ON]") | |
def button_off_command(): #when we press OFF button this function is running | |
pwm.ChangeDutyCycle(0) #write pwm data to led pin | |
print("[Led OFF]") | |
def button_pwm_command():#when we press SAVE button this function is running | |
global pwm_data | |
pwm_data=slider.get() #get slider's value | |
print("[PWM Data Saved...]") | |
root = tk.Tk() #our main frame called as "root" | |
root.title("Panel") #window name | |
bg=tk.PhotoImage(file="images.png") #backgorund image | |
labelbg=tk.Label(root,image=bg) | |
labelbg.place(x=0,y=0) | |
#setting window size | |
width=180 | |
height=180 | |
screenwidth = root.winfo_screenwidth() | |
screenheight = root.winfo_screenheight() | |
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) | |
root.geometry(alignstr) | |
root.resizable(width=False, height=False) | |
#setting slider | |
slider=tk.Scale(root,from_=0, to=100) | |
slider["bg"] = "#ff7f00" #slider color | |
slider.pack() | |
slider.place(x=10,y=10,width=50,height=160) | |
slider.set(100) #first value is 100 | |
#setting ON button | |
button_on=tk.Button(root) | |
button_on["bg"] = "#76ff0d" | |
ft = tkFont.Font(family='Times',size=18) | |
button_on["font"] = ft | |
button_on["fg"] = "#000000" | |
button_on["justify"] = "center" | |
button_on["text"] = "ON" | |
button_on["relief"] = "groove" | |
button_on.place(x=70,y=10,width=100,height=30) | |
button_on["command"] = button_on_command | |
#setting SAVE button | |
button_pwm=tk.Button(root) | |
button_pwm["bg"] = "#ffff00" | |
ft = tkFont.Font(family='Times',size=18) | |
button_pwm["font"] = ft | |
button_pwm["fg"] = "#000000" | |
button_pwm["justify"] = "center" | |
button_pwm["text"] = "SAVE" | |
button_pwm["relief"] = "groove" | |
button_pwm.place(x=70,y=75,width=100,height=30) | |
button_pwm["command"] = button_pwm_command | |
#setting OFF button | |
button_off=tk.Button(root) | |
button_off["bg"] = "#f25858" | |
ft = tkFont.Font(family='Times',size=18) | |
button_off["font"] = ft | |
button_off["fg"] = "#000000" | |
button_off["justify"] = "center" | |
button_off["text"] = "OFF" | |
button_off["relief"] = "groove" | |
button_off.place(x=70,y=140,width=100,height=30) | |
button_off["command"] = button_off_command | |
#which command runs and update window | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment