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
  Using Tkinter inside function not working Ensaimadeta 5 4,861 Dec-03-2023, 01:50 PM
Last Post: deanhystad
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 2,971 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  Tkinter won't run my simple function AthertonH 6 3,742 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,737 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,418 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,920 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter get function finndude 2 2,891 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,236 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  Python3 tkinter radiobutton problem Nick_tkinter 14 5,832 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  tkinter python button position problem Nick_tkinter 3 3,486 Jan-31-2021, 05:15 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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