Display the output of a function in a tkinter label widget

Viewed 711

I've been reading a lot of stack overflow lately, and I 'd like to thank everyone who's active here for their contributions. This community has helped me a lot in my learning python!

I am trying to put together a pretty simple name generator program, in which every time you press a button in the tkinter GUI a new name is generated into a label widget.

The generator works with the random module picking words from pre-compiled lists and putting them together in combinations, the structures of which are defined in the function 'generate_name'.

I've gotten the program to work fine in console, but I can't figure out how to get it to work with tkinter.

I'm hoping to build a tkinter GUI where you press a button and the output of the 'generate_name' will be displayed on a tkinter label. However, I can't seem to achieve this, and I can only get the output of the function to be displayed in the console.

I've read many posts related to similar problems but I just can't seem to get my head around this. I've tried many alternative approaches to this, and although I have been able to get the outputs of simpler functions to be displayed, e.g. ones where you do simple math equations with variables, I simply can't get the randomly generated name to appear anywhere but the console.

Hopefully I've been able to articulate my problem clearly.

Here is a simplified version of the code I'm working on:

from tkinter import *
import random

#Lists of words that will be used by the generate_name() function

wordclass1 = (
'word1',
'word2',
'word3',
)

wordclass2 = (
'word4',
'word5',
'word6',
)

wordclass3 = (
'word7',
'word8',
'word9',
)

#These functions do the actual random generation of the names.

def name1():
    full_name = random.choice(wordclass1) + " " + random.choice(wordclass2)
    print(full_name)

def name2():
    full_name = random.choice(wordclass2) + " " + random.choice(wordclass3)
    print(full_name)

def name3():
    full_name = random.choice(wordclass1) + " " + random.choice(wordclass2) + " " + random.choice(wordclass3)
    print(full_name)

#This function randomly picks the individual name that should be displayed on the tkinter label

def generate_name():
    list = (name1, name2, name3)
    return(random.choice(list)())
    name = random.choice(list)

#This function is supposed to display the text on the tkinter label

def write():
    label = Label(root, text=(generate_name()))
    label.pack()

root = Tk()

button = Button(root, text="Generate a name!", command=write)
button.pack()

root.mainloop()

Thank you in advance!

I am still very much a beginner and I'm sure there's a bunch of things wrong with my code but any advice would be highly appreciated!

1 Answers

The way to do this is to initially create and empty Label and then put something new into it whenever the Button's clicked (using the universal widget method named config()). I also changed how some of your other functions worked, notably generate_name() to get everything working properly — I think most of the changes will be obvious.

from tkinter import *
import random

#Lists of words that will be used by the generate_name() function

wordclass1 = (
'word1',
'word2',
'word3',
)

wordclass2 = (
'word4',
'word5',
'word6',
)

wordclass3 = (
'word7',
'word8',
'word9',
)

#These functions do the actual random generation of the names.

def name1():
    full_name = random.choice(wordclass1) + " " + random.choice(wordclass2)
    return full_name

def name2():
    full_name = random.choice(wordclass2) + " " + random.choice(wordclass3)
    return full_name

def name3():
    full_name = random.choice(wordclass1) + " " + random.choice(wordclass2) + " " + random.choice(wordclass3)
    return full_name

#This function randomly picks the individual name that should be displayed on the tkinter label

def generate_name():
    return random.choice([name1, name2, name3])()

#This function is supposed to display the text on the tkinter label

def write():
    label.config(text=generate_name())

root = Tk()

label = Label(root, text='')
label.pack()
button = Button(root, text="Generate a name!", command=write)
button.pack()

root.mainloop()

Related