Python Forum

Full Version: Tkinter - I have problem after import varaible or function from aGUI to script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?

[Image: jgIdx.png]

--------------

After Run a.py the app from aGUI.py opened why?

[Image: C1BW5.png]

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()
Thank you for suggested I have the basic problem can you read it : https://python-forum.io/Thread-Tkinter-T...#pid110939