Hi
I am building password generator with python and tkinter as GUI. The function copy should copy paStr variable which reside in another function called def output. I couldn't find any workaround to reach paStr variable and make it possible for the copy function to copy paStr to clipboard.
Any help and hints are veeeery appreciated.
Using Python 3.8, VS Code, OS: Windows 10 pro
copy_pass button is in relation with def copy
paStr variable holds the generated passwords and it's the variable of def output
I deleted 2/3 of the code and pasted the needed part only.
For smooth troubleshooting in chars length insert 8 and in how many password insert 3.
I suggest to try the code on your preferable interpreter, it is impossible to troubleshoot it from your smartphone or with random thoughts without testing.
Thank you very much
Stay Safe
My code:

I am building password generator with python and tkinter as GUI. The function copy should copy paStr variable which reside in another function called def output. I couldn't find any workaround to reach paStr variable and make it possible for the copy function to copy paStr to clipboard.
Any help and hints are veeeery appreciated.
Using Python 3.8, VS Code, OS: Windows 10 pro
copy_pass button is in relation with def copy
paStr variable holds the generated passwords and it's the variable of def output
I deleted 2/3 of the code and pasted the needed part only.
For smooth troubleshooting in chars length insert 8 and in how many password insert 3.
I suggest to try the code on your preferable interpreter, it is impossible to troubleshoot it from your smartphone or with random thoughts without testing.
Thank you very much
Stay Safe
My code:
import tkinter as tk import tkinter.font as tkFont from tkinter import * from tkinter import ttk import random import string root = tk.Tk() root.title("P-GEN") width=600 height=550 screenwidth = root.winfo_screenwidth() screenheight = root.winfo_screenheight() alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) root.geometry(alignstr) root.resizable(width=False, height=False) passLen = IntVar() passNum = IntVar() def randompassword(): uchars = int(passLen.get() / 4) lchars = int(passLen.get() / 4) dchars = int(passLen.get() / 4) schars = int(passLen.get() / 4) str_uchars, str_lchars, str_dchars, str_schars = '', '', '', '' for i in range(uchars): str_uchars += random.SystemRandom().choice(string.ascii_uppercase) for i in range(lchars): str_uchars += random.SystemRandom().choice(string.ascii_lowercase) for i in range(dchars): str_uchars += random.SystemRandom().choice(string.digits) for i in range(schars): str_uchars += random.SystemRandom().choice(string.punctuation) random_str = str_uchars + str_lchars + str_dchars + str_schars random_str = ''.join(random.sample(random_str, len(random_str))) l = list(random_str) random.shuffle(l) result = ''.join(l) return str(result) def output(): n = 0 while n < passNum.get(): paStr = randompassword() + '\n' print(paStr) n=n+1 generated_pass.insert('1.0', paStr) ''' def copy(): # Iam stucked here, any hints please to return the paStr varaible from the previous function (def output) so I can make it possible to be copied to clipboard. I tried this but as I said, could not find how to reach local variable of the previous function. copy_pass.clipboard_clear() copy_pass.clipboard_append(paStr) copy_pass.update() ''' title_bar=tk.Label(root) title_bar["bg"] = "#393d49" ft = tkFont.Font(family='Times',size=18) title_bar["font"] = ft title_bar["fg"] = "#33ed3f" title_bar["justify"] = "center" title_bar["text"] = "P-GEN" title_bar["relief"] = "flat" title_bar.place(x=0,y=0,width=600,height=31) enter_chars=tk.Label(root) ft = tkFont.Font(family='Times',size=10) enter_chars["font"] = ft enter_chars["fg"] = "#333333" enter_chars["justify"] = "left" enter_chars["text"] = "Enter Chars Length" enter_chars["relief"] = "flat" enter_chars.place(x=20,y=160,width=169,height=30) pass_length=tk.Entry(root) pass_length["bg"] = "#d2f4d4" pass_length["borderwidth"] = "3px" ft = tkFont.Font(family='Times',size=10) pass_length["font"] = ft pass_length["fg"] = "#333333" pass_length["justify"] = "center" pass_length["text"] = "Entry" pass_length["relief"] = "sunken" pass_length.place(x=220,y=160,width=142,height=30) pass_length["textvariable"] = passLen enter_passN=tk.Label(root) ft = tkFont.Font(family='Times',size=10) enter_passN["font"] = ft enter_passN["fg"] = "#333333" enter_passN["justify"] = "left" enter_passN["text"] = "Enter How Many Passwords" enter_passN["relief"] = "flat" enter_passN.place(x=20,y=210,width=169,height=31) pass_num=tk.Entry(root) pass_num["bg"] = "#d2f4d4" pass_num["borderwidth"] = "3px" ft = tkFont.Font(family='Times',size=10) pass_num["font"] = ft pass_num["fg"] = "#333333" pass_num["justify"] = "center" pass_num["text"] = "Entry" pass_num["relief"] = "sunken" pass_num.place(x=220,y=210,width=142,height=30) pass_num["textvariable"] = passNum gen_pass=tk.Button(root) gen_pass["activebackground"] = "#7ff14e" gen_pass["bg"] = "#efefef" ft = tkFont.Font(family='Times',size=10) gen_pass["font"] = ft gen_pass["fg"] = "#000000" gen_pass["justify"] = "center" gen_pass["text"] = "Generate Passwords" gen_pass["relief"] = "raised" gen_pass.place(x=20,y=270,width=142,height=30) gen_pass["command"] = output generated_pass=tk.Text(root) generated_pass["bg"] = "#d2f4d4" generated_pass["borderwidth"] = "3px" ft = tkFont.Font(family='Times',size=10) generated_pass["font"] = ft generated_pass["fg"] = "#333333" generated_pass["relief"] = "sunken" generated_pass.place(x=220,y=270,width=346,height=192) copy_pass=tk.Button(root) copy_pass["activebackground"] = "#7ff14e" copy_pass["bg"] = "#efefef" ft = tkFont.Font(family='Times',size=10) copy_pass["font"] = ft copy_pass["fg"] = "#000000" copy_pass["justify"] = "center" copy_pass["text"] = "Copy Passwords" copy_pass["relief"] = "raised" copy_pass.place(x=20,y=320,width=142,height=30) #copy_pass["command"] = copy if __name__ == "__main__": root.mainloop()