Im trying to make a random name generator using an imported list and tkinter. I can get a random name its just that it doesn't change the name when the Generate new name button is pressed. Also im new to python, so anything helps lol
Thoughts?
import random
from namelist import namel1ist
import tkinter as tk
name1 = (random.choice(namel1ist) " " random.choice(namel1ist))
print(name1)
root = tk.Tk()
tk.Button(root, text='Generate new name',command=name1).pack()
w = tk.Label(root, text=name1)
w.pack()
tk.mainloop()
CodePudding user response:
You need to define the command as its own function, that changes the text value of the label.
At the moment you only call random.choice(namel1ist)
once, when the label is created. Like this the text will be set.
CodePudding user response:
frankly i dont know what the namelist
module does, but heres a working example that you can work around with:
import random
import tkinter as tk
names = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def random_name():
name = random.choice(names) " " random.choice(names)
w.config(text=name)
return name
root = tk.Tk()
random_button = tk.Button(root, text='Generate new name', command=random_name)
random_button.pack()
w = tk.Label(root, text=(random.choice(names) " " random.choice(names)))
w.pack()
tk.mainloop()