1

(Main.py)

from tkinter import * 
from second import numbers 

a1=0 
LABEL = tk.Label(text=f"Your value is:{a}", font=("Arial", 24, "bold"))
LABEL.grid(column=19, row=6)

(second.py)

class numbers  

    def function(self):
        root=Toplevel()
        my_canvas=tk.Canvas(500,500).pack()
             def function1():
                 e= min. + max. 
                 a=e
        return a #if I do this I cannot see my canvas,if I use return inside function1 nothing happens

#I can use a to change label as such;

                 global LABEL
                 LABEL = tk.Label(text=f"Your value is:{a}", font=("Arial", 24, "bold"))
                 LABEL.grid(column=19, row=6)

Even though I can change the "LABEL" using global,I cannot change "a1" variable with "a" at the same time in tkinter.

When I try this solution:

Python nested functions variable scoping

solution is using return function, if I do this I cannot display my canvas

My purpose is using "a" 's value and changing "a1"

  • Could you provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), and be a little more clear in your question? Right now, it's hard to tell exactly what you're asking. Providing any error messages would also help, along with any complete attempts you have made to resolve the issue. For example, you mention "your canvas", but nowhere in your code is there mention of a canvas. – Sylvester Kruin Oct 07 '21 at 22:18
  • `c+d=e` is not a valid statement. `numbers` is a class defined in `second.py` and so `import numbers` is also invalid (should be `from second import numbers` instead). – acw1668 Oct 08 '21 at 02:38
  • Hello @acw1668 thanks – ugur çapan Oct 08 '21 at 05:22
  • Hello Sylvester, thanks I added additional information. I hope it s much more clear now :) – ugur çapan Oct 08 '21 at 06:14

1 Answers1

0

You can encapsulate the window and add a method that makes the changes you want.

import tkinter as tk

class App(tk.Tk):

    def increment(self, inc=1):
        self.num += inc
        self.label.config(text=f'{self.num}')

    def __init__(self):
        tk.Tk.__init__(self)

        self.num = 0

        self.label = tk.Label(text=f'{self.num}')
        self.button = tk.Button(
            text='Increment', command=self.increment
        )
        self.label.pack()
        self.button.pack()

if __name__ == '__main__':
    app = App()
    app.increment(inc=10)
    app.mainloop()
SrPanda
  • 854
  • 1
  • 5
  • 9
  • Hello Alex thank you for your answer but my calculation ends inside of an inner function. I can reach variables if my functions are like this but as you see my above question I want to use "a" inside of "a1". – ugur çapan Oct 08 '21 at 05:51
  • 1
    Make the `app = App()` in a global state, like in flask, then you can call `app.increment(inc=10)` from anywhere. You can also uso `from file import app` to have access to it in a different file. – SrPanda Oct 08 '21 at 14:15
  • even I will not be able to ask questions again due to my ban :) finally, I solved my problem by creating json file then I saved "a" inside the file then I read the json :) – ugur çapan Oct 08 '21 at 21:31
  • Hello Alex, my code is really so much complex so I don t have such a skill that is importing your code inside mine :) – ugur çapan Oct 08 '21 at 21:35
  • But you get the idea. It is never a bad a idea to refactor if you have a espagueti. – SrPanda Oct 08 '21 at 22:30