Blog Pages

1/9/18

Raspberry Pi 3 GUI - Part 5 - 3 Control Buttons and Text with Tkinter

3 Control Buttons with aligned text using Tkinter


This program creates 3 buttons that will be used to control 3 LED's. The text is aligned to the buttons using ".grid" and will eventually display the state of the LED.

(1) Enter this code into your Python 3 window

#! /usr/bin/env python

from Tkinter import*

window = Tk()
window.title( 'tkinter5.py')
window.geometry('500x300')

# create text items
red_led_text = Label(window, relief='sunken', width=15)
yellow_led_text = Label(window, relief='sunken',width=15)
green_led_text = Label(window, relief='sunken', width=15)

# create buttons for controlling LED's
red_led_button = Button(window)
yellow_led_button = Button(window)             
green_led_button = Button(window)

# set initial button text
red_led_button.configure(text='RED (ON / OFF)', command=exit)
yellow_led_button.configure(text='YEL (ON / OFF)', command=exit)
green_led_button.configure(text='GRN (ON / OFF)', command=exit)

# set initial text values
red_led_text.configure(text='RED LED IS OFF')
yellow_led_text.configure(text='YEL LED IS OFF')
green_led_text.configure(text='GRN LED IS OFF')

# set location of buttons
red_led_button.grid(row=1, column=1,padx=10)
yellow_led_button.grid(row=2, column=1,padx=10)
green_led_button.grid(row=3, column=1,padx=10)
       
# location of text                         
red_led_text.grid(row=1, column=3,padx=10 )
yellow_led_text.grid(row=2, column=3,padx=10 )
green_led_text.grid(row=3, column=3,padx=10 )

window.mainloop()

(2)
My code looks like this:
blizzard tkinter python code

(3)
Save your code as tkinter5.py to your desktop.
Change directory in your terminal window using cd Desktop.
Enter chmod 755 tkinter5.py in your terminal window. Press Enter
Enter ./tkinter5.py in your terminal window. Press Enter

(4)
Screen capture showing window with 3 buttons and aligned text.

blizzard tkinter python desktop

(5)
Next Up - Control and LED with a Button using GPIO.

 Thanks for reading,
Trenton

No comments:

Post a Comment