[Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script (/thread-25981.html) |
Tkinter - I have problem after import varaible or function from aGUI to script - johnjh - Apr-17-2020 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. RE: Tkinter - I have problem after import varaible or function from aGUI to script - deanhystad - Apr-17-2020 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 hereYou 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() RE: Tkinter - I have problem after import varaible or function from aGUI to script - johnjh - Apr-17-2020 Thank you for suggested I have the basic problem can you read it : https://python-forum.io/Thread-Tkinter-Tkinter-I-need-to-read-file-excel-from-GUI-app-to-script-file?pid=110939#pid110939 |