[Tkinter] Silly sentence generator (how to print the sentence in the app) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Silly sentence generator (how to print the sentence in the app) (/thread-26918.html) |
Silly sentence generator (how to print the sentence in the app) - pav1983 - May-18-2020 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() RE: Silly sentence generator (how to print the sentence in the app) - menator01 - May-18-2020 Some links to get you started https://realpython.com/python-gui-tkinter/ https://python-forum.io/Thread-Tkinter-Very-Basic-Example-Only-Hello-World RE: Silly sentence generator (how to print the sentence in the app) - Yoriz - May-18-2020 Use a TK label to display the required text. RE: Silly sentence generator (how to print the sentence in the app) - pav1983 - May-18-2020 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() RE: Silly sentence generator (how to print the sentence in the app) - Yoriz - May-18-2020 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() RE: Silly sentence generator (how to print the sentence in the app) - pav1983 - May-19-2020 Thanks for showing another way to do that. |