May-18-2020, 09:00 PM
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()