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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

1
2
3
4
5
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:
1
2
if __name__ == '__main__':
    # Put code you don't want to execute when imported here
You could rewrite your script to look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
  Multiply function using Tkinter ady1583 7 11,449 Feb-09-2025, 02:58 PM
Last Post: frankjoesmith
  Using Tkinter inside function not working Ensaimadeta 5 8,661 Dec-03-2023, 01:50 PM
Last Post: deanhystad
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 7,013 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  Tkinter won't run my simple function AthertonH 6 6,811 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 7,405 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 8,497 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 7,738 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter get function finndude 2 3,967 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 13,174 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  Python3 tkinter radiobutton problem Nick_tkinter 14 9,651 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter

Forum Jump:

User Panel Messages

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