Home > OS >  Set object to a variable in another file
Set object to a variable in another file

Time:10-03

I want to call a Entry in my main class from a function in another python file. I discovered, it works with create a list and append it but not with just "=" why is that so? and how does it work?

main.py:

from variable import *

number.append(5)
number2 = 5

printer()
printer2()

variable.py:

number = []
number2 = None

def printer():
    print(number[0])

def printer2():
    print(number2)

result:

5
None

Another thing is i call the widgets that i not created with a for loop by example:

self.frame_pm.winfo_children()[0]

when i print this i get:

.!frame2.!entry

can´t i call it direct? like:

.!frame2.!entry.config(bg="yellow")

CodePudding user response:

Concentrating on this question: but not with just "=" why is that so?, lets look at your code:

from variable import *

This line makes four new variables for main.py. Its like this code:

import variable
number = variable.number
number2 = variable.number2

printer = variable.printer
printer2 = variable.printer2

Concentrating just on number2 above, when your main.py executes this: number2 = 5 it will just affect the variable number2 local to this module and not variable.number2.

  • Related