Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

LISTING 18.7 The script1807.py File


Click here to view code image


#!/usr/bin/python3
from tkinter import *
class Application(Frame):
"""Build the basic window frame template"""
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.label1 = Label(self, text='Enter the text to convert:')
self.label1.grid(row=0, column=0, sticky =W)
self.text1 = Text(self, width=20, height=10)
self.text1.grid(row=1, column=0)
self.text1.focus_set()
self.button1 = Button(self, text='Convert', command=self.convert)
self.button1.grid(row=2, column=0)
self.button2 = Button(self, text='Clear', command=self.clear)
self.button2.grid(row=2, column=1)

def convert(self):
varText = self.text1.get("1.0", END)
varReplaced = varText.upper()
self.text1.delete("1.0", END)
self.text1.insert(END, varReplaced)
def clear(self):
self.text1.delete("1.0", END)
self.text1.focus_set()
root = Tk()
root.title = 'Text widget test'
root.geometry('300x250')
app = Application(root)
app.mainloop()

When you run the script1807.py file, you get a window that shows the Text widget and the two
buttons. You can then enter larger blocks of text into the Text widget, click the Convert button to
convert the text to all uppercase, and click the Clear button to delete the text.


Using a Listbox Widget


The Listbox widget provides a listing of multiple values for your application user to choose from.
When you create the Listbox widget, you can specify how the user selects items in the list with the
selectmode parameter, as shown here:


Click here to view code image


self.listbox1 = Listbox(self, selectmode=SINGLE)
Free download pdf