SO, I have two file:
FOLDER
|_ generator.py
|_ gui.py
in generation.py
there is a function that creates a random username:
import string
import random
def generate_username():
username = "".join(random.sample(string.ascii_letters, 8))
return username
if __name__ == '__main__':
generate_username()
in gui.py
there is a function that creates a Tkinter window with a label and a Entry:
from tkinter import *
from generator import generate_username
def generate_window():
top = Tk()
label = Label(top, text="User Name")
label.pack( side = LEFT)
user_name = Entry(top, bd =5)
user_name.pack(side = RIGHT)
button_1 = Button(
command=lambda: generate_username(),
)
top.mainloop()
if __name__ == '__main__':
generate_window()
My goal is to fill the Entry in the gui.py
with the username generated in generate_username
function in the other script when the button is pressed (hope I expressed myself well).
So basically the button has two jobs:
- run the
generate_username
funtion - get the generated variable and write it on the Entry
I really have no idea hope to do it, I'm not even sure if it is possible. Does anyone have any idea?
CodePudding user response:
Yes it is possible. You can also use the Entry
widget as an argument for your generate_username
function in the other file (so that you don't need to return
). Hope this helps.
from tkinter import *
import random
import string
def generate_username(entry):
username = "".join(random.sample(string.ascii_letters, 8))
entry.delete(0, END)
entry.insert(0, username)
def generate_window():
top = Tk()
label = Label(top, text="User Name")
label.pack(side = LEFT)
user_name = Entry(top, bd =5)
user_name.pack(side ='left')
button_1 = Button(text="Generate",
command=lambda: generate_username(user_name),
)
button_1.pack(side='right')
top.mainloop()
if __name__ == '__main__':
generate_window()