0

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

martineau
  • 119,623
  • 25
  • 170
  • 301

3 Answers3

0

I have never used tkinter, but I am pretty sure this is the answer:

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

It looks to me like your text object is just calling the print function, rather than assigning a string to the text object.

Daniel Paczuski Bak
  • 3,720
  • 8
  • 32
  • 78
0

You cannot use print to output to a Tkinter GUI.

The Text() you are also using is creating a text box with content in it that can be configured in several ways and also edited in the GUI, I think you are more wanting to just output your results. Either way, I coded up Text() to work as well as a alternate method using Label() to output a non-editable version. You will however have to find a way with Label to clear the previous output each time as you can see in outputs that they stack on each other.

I only wrote in age and country to save a bit of time on my end

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):
    age_display = Text(window, font=("Arial Bold", 16), width = 2, height = 1)
    age_display.insert(INSERT, str(random.choice(age)))
    age_display.grid(column = 0, row = 1)

    country_display = Label(window, text=str(random.choice(country)), font=("Arial Bold", 16))
    country_display.grid(column = 0, row = 2)



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

How to format your Python code

How to use Text() in depth

How to use Label() in depth

Latronis
  • 61
  • 3
0

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()
acw1668
  • 40,144
  • 5
  • 22
  • 34