1

I got a problem using variables from one function onto another.

This is my code:

import tkinter as tk
def form ():
 textVar = tk.StringVar()
 entry0 = tk.Entry(self, textvariable = textVar).pack()
def toPrint():
 texto = textVar.get()
 print(texto)
def button():
 button0 = tk.Button(text="Summit", command = toPrint).pack()

Now the variable being called in toPrint() is local from form(), therefore I can not use it without using global, but that is causing issues with the rest of my code since I'm using form() more than once, is there any other way to solve it?

I would appreciate if the explanation is simple, I'm still a beginner.

I already have searched for this in SO, but I did not manage to understand the answers.

asked Feb 1, 2018 at 1:08
4
  • Create a class containing the fields you need to share Commented Feb 1, 2018 at 1:11
  • Ah, you're starting to discover the magic of OOP :-) See Best way to structure a tkinter application. Commented Feb 1, 2018 at 1:12
  • Possible duplicate of Best way to structure a tkinter application Commented Feb 1, 2018 at 1:20
  • 1
    how about you return textVar from 'form', and call 'form' from 'toPrint' ? Commented Feb 1, 2018 at 1:20

1 Answer 1

2

I am a foreigner to English, so firlstly I apologized for my impolite or wrong use of English word. Just want to say I really do not mean it.

And for this question, maybe you could try to put these in the same class. And try to make the variables you want to called several times the attribute of the class.

For example:

class myclass():
 def __init__ (self):
 self.textVar = tk.StringVar()
 self.entry0 = tk.Entry(self, textvariable = textVar).pack()
 def toPrint(self):
 texto = self.textVar.get()
 print(texto)
answered Feb 1, 2018 at 1:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.