Python Forum
[Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script
#1
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.
Reply
#2
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()
Reply
#3
Thank you for suggested I have the basic problem can you read it : https://python-forum.io/Thread-Tkinter-T...#pid110939
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter get function finndude 2 103 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 327 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  Python3 tkinter radiobutton problem Nick_tkinter 14 436 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  tkinter python button position problem Nick_tkinter 3 438 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  Using Tkinter inside function not working Ensaimadeta 3 325 Dec-23-2020, 02:57 PM
Last Post: deanhystad
  Stop import from executing function, but allow the function to execute from tk btn. MrBitPythoner 4 282 Dec-08-2020, 10:00 PM
Last Post: MrBitPythoner
  function in new window (tkinter) Dale22 7 491 Nov-24-2020, 11:28 PM
Last Post: Dale22
Star [Tkinter] How to perform math function in different page of Tkinter GUI ravaru 2 380 Oct-23-2020, 05:46 PM
Last Post: deanhystad
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 475 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  [Tkinter] ClockIn/Out tkinter problem Maryan 2 350 Oct-12-2020, 03:42 AM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020