Tkinter App to Get Random Words or Sentences

Filed Under: Python
Random Words Or Sentences Python

In this tutorial, we will be developing a Tkinter application that will generate random words and sentences for the user depending on the choices the user makes.

Before jumping to the code implementation, I would like to mention the uses the application has in real-life scenarios.

First and foremost, the app can come in handy if you want to conduct an impromptu spell bee competition, and you aren’t exactly good with words.

Also, if you are in a debate, and you’re out of valid points to make, and your opponent’s got one over you, you could maybe make up a sentence with the help of the application.

Implementing a Random Word Generator GUI App in Python

Now that you are aware of how helpful this application can be, let’s start developing the application from scratch.

Creating and Designing the Application Window

To begin working on the application, we first have to import tkinter module (if already installed on the system ). Next, we will assign our window as the variable app.

Now that we have the application window, we will start customizing the window. We will start by changing the window’s title, using the title method. Next, we can determine the size of the application window using the geometry method. We can also prevent the window from resizing using the resizable method.

You can also change the background color of the window in Tkinter using configure(bg=”) method. You can provide a HEX equivalent value for color as the value to the bg parameter.

from tkinter import *
app = Tk()

app.title("Random Words/Sentences Generator")
app.geometry('480x380+50+50')
app.resizable(False, False)
app.configure(bg='#4A235A')
 
app.mainloop()
Random Words And Sentences Window Output 1
Random Words And Sentences Window Output 1

We will be adding the following widgets to our application:

  1. Label
  2. Buttons
  3. Two radio buttons to choose between words or sentences
  4. TextBox

Let’s add all the widgets in a single block of code. I will be explaining how the widgets are added in a few points after the code.

from tkinter import *
app = Tk()

app.title("Random Words/Sentences Generator")
app.geometry('480x380+50+50')
app.resizable(False, False)
app.configure(bg='#4A235A')


# Labels for Initial Text in the application    
Title = Label(text="Welcome to Random Words and Sentence Generator!",bg="black",fg="white",font=("Helvetica", 14)).place(x=10, y=3) 
Description_Text = """You need to choose between random words or sentences and then the 
generate button will generate random words/sentences. 
You can copy the same using the Copy Button."""
Description = Label(text=Description_Text,bg="#FFC300",
                    fg="black",font=("Helvetica", 10)).place(x=30, y=40)

choice_text = Label(text="Choose between random words \n or sentences",bg="#4A235A",fg="white",
                    font=("Helvetica", 10)).place(x=5, y=100)

# Radio Buttons
var = IntVar()
word_rbtn = Radiobutton(app,text="Words", variable=var, 
                        value=1,font=("Helvetica", 10),
                        bg="#4A235A",fg="white")
sent_rbtn = Radiobutton(app,text="Sentences", variable=var, 
                        value=2,font=("Helvetica", 10),
                        bg="#4A235A",fg="white")
word_rbtn.place(x=210,y=102)
sent_rbtn.place(x=300,y=102)

#Buttons
confirm_btn = Button(text="Generate!",font=("Helvetica",10),width=20)
confirm_btn.place(x=50, y=140)
exit_btn = Button(text="Exit Application!",font=("Helvetica",10),width=20)
exit_btn.place(x=240, y=140)
copy_btn = Button(text="Copy to Clipboard!",font=("Helvetica",10),width=20)
copy_btn.place(x=150, y=330)

#TextBox for Output
result = Text(height = 8, width = 55,state='disabled')
result.place(x=15,y=180)

app.mainloop()

We will be adding text on the screen using the Label widget and adding some custom attributes to the text as well. Buttons are one of the simplest sections of any GUI programmed application and we can add them using the Button widgets.

We would also add some radio buttons using the Radiobutton widget and a textbox to display the result with the help of Text widget.

Random Words And Sentences Window Output 2
Random Words And Sentences Window Output 2

Adding Functionality to the Application

We will first create a function that will make sure to register the choice made by the user. If the user wants to generate random words or sentences. The code for the function is below.

random = "word"
def selected():
    i = int(var.get())
    global random
    if(i==2):
        random= "sentence"
    else:
        random="word"

Next, we will create a function to exit the application. It’s a common function and you might have seen it before as well. The code is below which can help to exit the application window.

def exit_application():
    global app
    app.destroy()

To generate the random words or sentences, we will be making use of the wonderwords library of python programming language. Wonderwordscontains various functions which are used for generating random words and sentences.

The random words or sentences generated are displayed in the textbox widget using the code below.

from wonderwords import RandomWord,RandomSentence
final_text = ""
def Generate():
    final_text = ""
    if(random=="word"):
        R_word_obj = RandomWord()
        for i in range(5):
            x = R_word_obj.word()
            final_text+=x+"\n"
    
    elif(random=="sentence"):
        R_sent_obj = RandomSentence()
        for i in range(5):
            x = R_sent_obj.sentence()
            final_text+=x+"\n"
    
    result.configure(state='normal')
    result.delete('1.0',"end")
    result.insert('1.0', final_text)
    result.configure(state='disabled')

The final function that we need is a function that will copy the random words and sentences we generated to the system clipboard. The code for the same is below.

def copy_to_clip():
    final_text = result.get("1.0", 'end-1c')
    app.clipboard_clear()
    app.clipboard_append(final_text)

The last step is to add the respective functions in the command attribute of the respective button objects we have in our application.

from tkinter import *
app = Tk()

app.title("Random Words/Sentences Generator")
app.geometry('480x380+50+50')
app.resizable(False, False)
app.configure(bg='#4A235A')


# Labels for Initial Text in the application    
Title = Label(text="Welcome to Random Words and Sentence Generator!",bg="black",fg="white",font=("Helvetica", 14)).place(x=10, y=3) 
Description_Text = """You need to choose between random words or sentences and then the 
generate button will generate random words/sentences. 
You can copy the same using the Copy Button."""
Description = Label(text=Description_Text,bg="#FFC300",
                    fg="black",font=("Helvetica", 10)).place(x=30, y=40)

choice_text = Label(text="Choose between random words \n or sentences",bg="#4A235A",fg="white",
                    font=("Helvetica", 10)).place(x=5, y=100)

# Radio Buttons
var = IntVar()
word_rbtn = Radiobutton(app,text="Words", variable=var, 
                        value=1,font=("Helvetica", 10),
                        bg="#4A235A",fg="white",command=selected)
sent_rbtn = Radiobutton(app,text="Sentences", variable=var, 
                        value=2,font=("Helvetica", 10),
                        bg="#4A235A",fg="white",command=selected)
word_rbtn.place(x=210,y=102)
sent_rbtn.place(x=300,y=102)

#Buttons
confirm_btn = Button(text="Generate!",font=("Helvetica",10),
                     width=20,command=Generate)
confirm_btn.place(x=50, y=140)
exit_btn = Button(text="Exit Application!",font=("Helvetica",10),
                  width=20,command=exit_application)
exit_btn.place(x=240, y=140)
copy_btn = Button(text="Copy to Clipboard!",font=("Helvetica",10),
                  width=20,command=copy_to_clip)
copy_btn.place(x=150, y=330)

#TextBox for Output
result = Text(height = 8, width = 55,state='disabled')
result.place(x=15,y=180)

app.mainloop()

The Complete Code for the Tkinter Application

from tkinter import *
app = Tk()

app.title("Random Words/Sentences Generator")
app.geometry('480x380+50+50')
app.resizable(False, False)
app.configure(bg='#4A235A')

random = "word"
def selected():
    i = int(var.get())
    global random
    if(i==2):
        random= "sentence"
    else:
        random="word"
        
def exit_application():
    global app
    app.destroy()

from wonderwords import RandomWord,RandomSentence
final_text = ""
def Generate():
    final_text = ""
    if(random=="word"):
        R_word_obj = RandomWord()
        for i in range(5):
            x = R_word_obj.word()
            final_text+=x+"\n"
    
    elif(random=="sentence"):
        R_sent_obj = RandomSentence()
        for i in range(5):
            x = R_sent_obj.sentence()
            final_text+=x+"\n"
    
    result.configure(state='normal')
    result.delete('1.0',"end")
    result.insert('1.0', final_text)
    result.configure(state='disabled')

def copy_to_clip():
    final_text = result.get("1.0", 'end-1c')
    app.clipboard_clear()
    app.clipboard_append(final_text)


# Labels for Initial Text in the application    
Title = Label(text="Welcome to Random Words and Sentence Generator!",bg="black",fg="white",font=("Helvetica", 14)).place(x=10, y=3) 
Description_Text = """You need to choose between random words or sentences and then the 
generate button will generate random words/sentences. 
You can copy the same using the Copy Button."""
Description = Label(text=Description_Text,bg="#FFC300",
                    fg="black",font=("Helvetica", 10)).place(x=30, y=40)

choice_text = Label(text="Choose between random words \n or sentences",bg="#4A235A",fg="white",
                    font=("Helvetica", 10)).place(x=5, y=100)

# Radio Buttons
var = IntVar()
word_rbtn = Radiobutton(app,text="Words", variable=var, 
                        value=1,font=("Helvetica", 10),
                        bg="#4A235A",fg="white",command=selected)
sent_rbtn = Radiobutton(app,text="Sentences", variable=var, 
                        value=2,font=("Helvetica", 10),
                        bg="#4A235A",fg="white",command=selected)
word_rbtn.place(x=210,y=102)
sent_rbtn.place(x=300,y=102)

#Buttons
confirm_btn = Button(text="Generate!",font=("Helvetica",10),
                     width=20,command=Generate)
confirm_btn.place(x=50, y=140)
exit_btn = Button(text="Exit Application!",font=("Helvetica",10),
                  width=20,command=exit_application)
exit_btn.place(x=240, y=140)
copy_btn = Button(text="Copy to Clipboard!",font=("Helvetica",10),
                  width=20,command=copy_to_clip)
copy_btn.place(x=150, y=330)

#TextBox for Output
result = Text(height = 8, width = 55,state='disabled')
result.place(x=15,y=180)

app.mainloop()

Some Sample Outputs

Random Words And Sentences Window Output 3
Random Words And Sentences Window Output 3
Random Words And Sentences Window Output 4
Random Words And Sentences Window Output 4

Conclusion

Go ahead and try this out! You can generate hundreds of new random words and sentences very effortlessly with your own random word generator app 🙂

close
Generic selectors
Exact matches only
Search in title
Search in content