Blog Pages

1/9/18

Raspberry Pi 3 GUI - Part 4 - Control Button and Text with Tkinter

Button and Text using Tkinter and Python

In this example the button aligns with the text using the ".grid" commands.
Pressing the ON/OFF button exits the program.

(1) Enter all code into your Python editor window
#! /usr/bin/env python

from Tkinter import*

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

# create text item
text_box_1=Label(window, relief='sunken', width=15)

# create button
led_button = \
Button(window, text='ON/OFF', command=exit)

# set initial values here
text_box_1.configure(text='RED LED ON')

# set location of window items here
led_button.grid(row=1, column=1,padx=10)
text_box_1.grid(row=1, column=3,padx=10 )


window.mainloop()

(2)
My code looks like this:

trenton blizzard tkinter python
















(3)
Save to your desktop. Name your file tkinter4.py
Enter chmod 755 tkinter4.py into your terminal window. Press Enter.
Enter ./tkinter4.py into your terminal window. Press Enter.

(4)
Desktop screen capture (using scrot) showing button with text aligned with it.
Pressing the "ON/OFF" button closes the program.

blizzard tkinter python screen capture

(5)
UP NEXT? TWO MORE BUTTONS WITH ALIGNED TEXT

Thanks for reading,
Trenton



No comments:

Post a Comment