Python Forum
[Tkinter] Silly sentence generator (how to print the sentence in the app)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Silly sentence generator (how to print the sentence in the app)
#1
Dear python Community, I am having a hard time trying to figure out how to print the result in the app for my silly sentence generator. I was able to write the app and have the generate the silly sentences without using tkinter. However, I cannot seem to figure out how to get it to work with the GUI. I've done a lot of research and I have looked through the Forum, but I've had no luck figuring it out. Here is the code. I have a lot to learn with tkinter yet. As of right now, I get no error. Nothing pops up at all on the app.

import random
import names
from tkinter import *

window = Tk()
window.title('Silly Sentence Generator')
window.geometry('500x500')

#names.get_full_name()
#names.get_full_name(gender='male')
#names.get_first_name()
#names.get_first_name(gender='female')
#names.get_last_name()

first = (names.get_first_name())
second = ("likes", "wants", "prefers" )
third = ("a hamburger", "a roast beef sandwich", "chicken tenders")

def sentencegen():
    firrst = (first)
    seccond = random.choice(second)
    thirrd = random.choice(third)

def phrase():
    phrase = ((firrst) + " " + (seccond) + " " + (thirrd) + ".")


ssg = Button(text='Generate', command=sentencegen)
ssg.pack()
result=phrase


window.mainloop()
Reply
#2
Some links to get you started
https://realpython.com/python-gui-tkinter/
https://python-forum.io/Thread-Tkinter-V...ello-World
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Use a TK label to display the required text.
Reply
#4
It's not perfect, but at least the functionality is there.

import tkinter as tk
import random
import names

#names.get_full_name()
#names.get_full_name(gender='male')
#names.get_first_name()
#names.get_first_name(gender='female')
#names.get_last_name()

def random_sentence():
    first_names = [names.get_first_name()]
    action = ['bites', 'eats', 'laughs at', 'chides', 'showers',]
    thing = ['metal', 'cheese burgers', 'McLean Deluxes']
    full_sentence = '{} {} {}'.format(names.get_first_name(), random.choice(action), random.choice(thing))
    return full_sentence

def update_label_and_entry():
    new_random_sentence = random_sentence()
    label.config(text=new_random_sentence)
    entry.delete(0, tk.END) # delete content from 0 to end
    entry.insert(0, new_random_sentence) # insert new_random_name at position 0

root = tk.Tk()
label = tk.Label(root)
label.pack()
entry = tk.Entry(root)
entry.pack()
button = tk.Button(root, text="New random sentence", command=update_label_and_entry)
button.pack()
root.mainloop()
Reply
#5
The same code using a class and a replacement for names (as I don't have it)
import tkinter as tk
import random
# import names


def get_first_name():
    return 'Python forum'


def random_sentence():
    first_name = get_first_name()
    action = random.choice(('bites', 'eats', 'laughs at', 'chides', 'showers'))
    thing = random.choice(('metal', 'cheese burgers', 'McLean Deluxes'))
    return f'{first_name} {action} {thing}'


class MainFrame(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.label = tk.Label(self)
        self.label.pack()
        self.entry = tk.Entry(self)
        self.entry.pack()
        self.button = tk.Button(
            self, text="New random sentence",
            command=self.update_label_and_entry)
        self.button.pack()

    def update_label_and_entry(self):
        new_random_sentence = random_sentence()
        self.label.config(text=new_random_sentence)
        self.entry.delete(0, tk.END)
        self.entry.insert(0, new_random_sentence)


mainframe = MainFrame()
mainframe.mainloop()
Reply
#6
Thanks for showing another way to do that.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020