Python Quotes Changer Program tkinter GUI

By | March 24, 2020

Topic: Python Quotes Changer Program tkinter GUI

The Widgets Used in Quotes Changing Program

This program uses mainly two widgets:

  1. Message widget – to display quotes
  2. Spinbox widget – to provide 1 to 5 values to display quote number 1 to 5.

when the user selects a new number value from Spinbox widget, the quote is changed in message widget according to the number selected.

What You will Learn in Python Quotes Changing Program?

  1. The use of tkinter GUI module to create GUI application
  2. Create and set different options of Message Widget of tkinter package
  3. Create and set different options of Spinbox Widget in Python 3 tkinter module
  4. The logic to show a quote in message widget
  5. How to get selected value from Spinbox
  6. Conversion of selected value which is a string into int value
  7. Creating and using a Python List of strings / quotes
Python Quotes Changer Program tkinter GUI

Python Quotes Changer Program tkinter GUI

# Python 3 tkiner GUI Program to display the use
# of Message weidget and Spinbox Widget
# It uses Spinbox values as input to change
# a quote displayed in Message widget
# in colorful background color, font size, font color

from tkinter import*

def main():
    main_window = Tk()

    main_window.title('Message: www.EasyCodeBook.com')
    main_window.geometry("600x500")

    quote_list=["A real friend is one who walks in when the rest of the world walks out.",
               "Real friends are hard to find, difficult to leave and impossible to forget.",
                "A single rose can be my garden… a single friend, my world.",
                "I don’t need a friend who changes when I change and who nods when I nod; my shadow does that much better.",
                "Be slow to fall into friendship; but when thou art in, continue firm & constant."]
    
                

    ind=0          
    str_quote = quote_list[ind]
    msg = Message(main_window, text = str_quote,
                 padx=20, pady=20, bd=5, relief = GROOVE)
    msg.config(bg='green', fg='white', font=('verdana', 30))
    
    msg.pack()
    def move_quote():
        ind = int(spinbox1.get())
        str_quote = quote_list[ind-1]
        msg.config(text=str_quote)
        
    
    
    spinbox1 = Spinbox(main_window, from_=1, to=5,
                       command=move_quote,font=('verdana',30),width=10)
    
    spinbox1.pack(side=BOTTOM)
    mainloop()

main()

How Quotes Changer Python GUI Program Works

These 4 lines describe the Quotes changer program
 in the form of comments

# Python 3 tkiner GUI Program to display the use
# of Message weidget and Spinbox Widget
# It uses Spinbox values as input to change
# a quote displayed in Message widget
# in colorful background color, font size, font color

Here we import tkinter GUI

from tkinter import*

Here we define a main() function
def main():
    [ create a main window]
    main_window = Tk()
    [ set title of main window ]
    main_window.title('Message: www.EasyCodeBook.com')
    [ set size of main window ]
    main_window.geometry("600x500")
    [ create a list of 5 quotes strings with index 0 to 4]
    quote_list=["A real friend is one who walks in when the rest of the world walks out.",
               "Real friends are hard to find, difficult to leave and impossible to forget.",
                "A single rose can be my garden… a single friend, my world.",
                "I don’t need a friend who changes when I change and who nods when I nod; my shadow does that much better.",
                "Be slow to fall into friendship; but when thou art in, continue firm & constant."]
    
                
    [Put first quote at index 0 in a string variable] 
    ind=0          
    str_quote = quote_list[ind]
    [ create a message widget ]
    msg = Message(main_window, text = str_quote,
                 padx=20, pady=20, bd=5, relief = GROOVE)
    [ set back ground color, foregrond color (font color)
      font name verdana, font size of message widget]
    msg.config(bg='green', fg='white', font=('verdana', 30))
    [ Place message widget in main window]
    msg.pack()
    [ define a user defined function move_quote()]
       
    def move_quote():
        [ get selected value from Spinbox, make it integer]
        ind = int(spinbox1.get())
        [ get the quote string at that selected index
          ind-1 since if value is 5 then actual index=4
           index of list is from 0 to 4 and Spinbox values 1 to 5
         ] 
        
        str_quote = quote_list[ind-1]
         
        [ put that selected quote in message widget now]
        msg.config(text=str_quote)
        
    
    [create a spinbox with values from 1 to 5
      note that parameter is from_=1 , to=5]
    spinbox1 = Spinbox(main_window, from_=1, to=5,
                       command=move_quote,font=('verdana',30),width=10)
    [Place Spinbox widget on bottom side of main window]
    spinbox1.pack(side=BOTTOM)
    [ Start the main loop, to start GUI of Python Quotes Changer
      GUI program using tkinter GUI package]
    mainloop()

[ call the main() function to start execution]
main()

Leave a Reply

Your email address will not be published. Required fields are marked *