Python Forum
Call local variable of previous function from another function with Python3 & tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call local variable of previous function from another function with Python3 & tkinter
#6
You cannot get a variable from a function because the variables inside a function only exist as long as the function is running.  If you want to use a value from a function you must return the value or save the value someplace where it can be accessed.

Using the return value is not going to work in your case, so you need to save the variable so others can see it.  In a GUI application you could save the password string in a GUI display.  Your output() function inserts the password into generated_pass.  The copy() function could try to extract the password from there.  However I think it would be easier to save the password in a module variable.  In this example I made paStr a module variable that can be seen by any function in the module.  The output() function saves the generated password in paStr.  Notice the use of "global" to indicate that output() will not create a local variable named paStr, but will look outside the scope of output() for a variable with that name.
from tkinter import *

import randompaStr = ''

root = Tk()def randompassword():
    return ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=10))
   
def output():
    global paStr
    paStr = randompassword()
    print(paStr)
   
def copy():
    generated_pass.config(text=paStr)

frame = Frame(root)
frame.pack(fill = BOTH, expand = True)

gen_pass = Button(frame, command=output, text='Generate Password')
gen_pass.pack(fill = BOTH, expand = True)

copy_pass = Button(frame, command=copy, text='Copy Password')
copy_pass.pack(fill = BOTH, expand = True)

generated_pass = Label(frame, text='The password')
generated_pass.pack(fill = BOTH, expand = True)root.mainloop() 
Reply


Messages In This Thread
RE: Call local variable of previous function from another function with Python3 & tkinter - by deanhystad - Oct-12-2020, 09:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pass a variable between tkinter and toplevel windows janeik 10 2,374 Jan-24-2024, 06:44 AM
Last Post: Liliana
  Using Tkinter inside function not working Ensaimadeta 5 5,084 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  GUI Problem / call another function / fill QListwidget flash77 5 893 Jul-30-2023, 04:29 PM
Last Post: flash77
  simple tkinter question function call not opening image gr3yali3n 5 3,461 Aug-02-2022, 09:13 PM
Last Post: woooee
  [PyQt] [Solved]Help getting variable from Function in PyQt Extra 6 1,517 Jul-06-2022, 10:19 PM
Last Post: Extra
  Tkinter won't run my simple function AthertonH 6 3,899 May-03-2022, 02:33 PM
Last Post: deanhystad
  [PyQt] Call a function in FormA from FormB panoss 3 1,909 Jan-30-2022, 07:45 PM
Last Post: panoss
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,896 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,235 Oct-15-2021, 08:01 AM
Last Post: drSlump
  Creating a function interrupt button tkinter AnotherSam 2 5,564 Oct-07-2021, 02:56 PM
Last Post: AnotherSam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020