I have GUI Tkinter code as aGUI.py :
import os
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("APP")
scripte = ttk.Button(root, text="Run")
scripte.grid(row=3, column=2, columnspan=2, padx=20, pady=20)
db = 6 #this varaible export to script called a.py
def runApp():
print('app running')
scripte.config(command=runApp)
root.mainloop()
-------------
script code as a.py
from aGUI import db
number = db
print(number)
After Open App from aGUI.py and click in Run button to print db varaible from a.py , this button open app why?
--------------
After Run a.py the app from aGUI.py opened why?
Any suggested.
When you import a module it is executed. A common way to avoid this is:
if __name__ == '__main__':
# Put code you don't want to execute when imported here
You could rewrite your script to look like this:
db = 6 #this varaible export to script called a.py
if __name__ == '__main__':
import os
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("APP")
scripte = ttk.Button(root, text="Run")
scripte.grid(row=3, column=2, columnspan=2, padx=20, pady=20)
def runApp():
print('app running')
scripte.config(command=runApp)
root.mainloop()
But it is more common to write it like this:
import os
from tkinter import *
from tkinter import ttk
db = 6 #this varaible export to script called a.py
def runApp():
print('app running')
def make_panel(title):
root = Tk()
root.title(title)
scripte = ttk.Button(root, text="Run")
scripte.grid(row=3, column=2, columnspan=2, padx=20, pady=20)
scripte.config(command=runApp)
if __name__ == '__main__':
make_panel('APP')
root.mainloop()