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


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

Forum Jump:

User Panel Messages

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