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)
#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


Messages In This Thread
RE: Silly sentence generator (how to print the sentence in the app) - by pav1983 - May-18-2020, 08:11 PM

Forum Jump:

User Panel Messages

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