Search code examples
pythonuser-interfacetkinterwindowoutput

How to make my output appear in tkinter GUI instead of shell?


I'm pretty new to coding so bear with me if my code looks like crap (which is probably does.) I just want my output to appear in the GUI I made instead of in the shell. How do I modify my code to make this happen?

import random
from tkinter import *

# Attributes
age = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
       "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
       "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58",
       "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
       "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96",
       "97", "98", "99", "100", ]
country = ["United States", "Brazil", "Mexico", "China", "Japan", "Canada", "France", "Germany"]
male_name = ["Joe", "Eden", "Diego", "Anthony", "Jarod", "Kique", "Austin", "Hunter"]
female_name = ["Haley", "Ariana", "Sarah", "Jackie", "Serena"]
gender_male = "Male"
gender_female = "Female"

# Random Generation
def print_start_life(event):
    Text(window, text=print(random.choice(age)), font=("Arial Bold", 16))
    Text(window, text=print(random.choice(country)), font=("Arial Bold", 16))
    Text(window, text=print(random.choice(male_name or female_name)), font=("Arial Bold", 16))
    if male_name:
        Text(window, text=print(gender_male), font=("Arial Bold", 16))
    elif female_name:
        Text(window, text=print(gender_female), font=("Arial Bold", 16))


# GUI
window = Tk()
window.title("Random Life")
window.geometry('800x500')
lbl = Label(window, text="Do you want to play Random Life?", font=("Arial Bold", 25))
lbl.grid(column=0, row=0)

btn = Button(window, text="Yes")
btn.bind("<Button-1>", print_start_life)
btn.grid(column=1, row=0)
window.mainloop()


Solution

  • There are several issues in your code. The problem you stated is due to calling print(...) in text=print(...). You don't need to call print(...), just assign the argument of print(...) to text is enough.

    Also you should not recreate Text widgets every time you click Yes button. You should create Label widgets to display the random result once and then change their text in print_start_life() function.

    Below is a modified version of your code:

    import random
    from tkinter import *
    
    # Attributes
    age = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
           "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
           "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58",
           "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
           "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96",
           "97", "98", "99", "100", ]
    country = ["United States", "Brazil", "Mexico", "China", "Japan", "Canada", "France", "Germany"]
    male_name = ["Joe", "Eden", "Diego", "Anthony", "Jarod", "Kique", "Austin", "Hunter"]
    female_name = ["Haley", "Ariana", "Sarah", "Jackie", "Serena"]
    gender_male = "Male"
    gender_female = "Female"
    
    # Random Generation
    def print_start_life(event=None):
        selected_age.config(text=random.choice(age))
        selected_country.config(text=random.choice(country))
        name = random.choice(male_name+female_name)
        selected_name.config(text=name)
        gender.config(text=gender_male if name in male_name else gender_female)
    
    # GUI
    window = Tk()
    window.title("Random Life")
    window.geometry('800x500')
    lbl = Label(window, text="Do you want to play Random Life?", font=("Arial Bold", 25))
    lbl.grid(column=0, row=0)
    
    btn = Button(window, text="Yes", command=print_start_life)
    #btn.bind("<Button-1>", print_start_life)
    btn.grid(column=1, row=0)
    
    frm = Frame(window)
    font1 = ("Arial", 16)
    font2 = ("Arial Bold", 16)
    Label(frm, text='Age:', font=font1).grid(row=0, column=0, sticky=E)
    selected_age = Label(frm, font=font2)
    selected_age.grid(row=0, column=1, sticky=W)
    
    Label(frm, text='Country:', font=font1).grid(row=1, column=0, sticky=E)
    selected_country = Label(frm, font=font2)
    selected_country.grid(row=1, column=1, sticky=W)
    
    Label(frm, text='Name:', font=font1).grid(row=2, column=0, sticky=E)
    selected_name = Label(frm, font=font2)
    selected_name.grid(row=2, column=1, sticky=W)
    
    Label(frm, text="Gender:", font=font1).grid(row=3, column=0, sticky=E)
    gender = Label(frm, font=font2)
    gender.grid(row=3, column=1, sticky=W)
    
    frm.grid(row=1, sticky=W)
    
    window.mainloop()