Help Tkinter opening a Python application

Asked

Viewed 42 times

0

I made a separate menu to find my applications and it opens normally, but if it closes I can’t open it again, just by closing the menu application.

from tkinter import *

def Cadcordenadores():
      from Cadcordenador import Application

def Cadalunos():
      from Cadalunos import Application

root = Tk()
root.title('App')
root.geometry('800x600')
menubar = Menu(root)

filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label="Cordenadores", command = Cadcordenadores)
filemenu.add_separator()
filemenu.add_command(label = "Alunos", command = Cadalunos)

1 answer

0

Modules are executed when they are imported, only once. See what happens, for example, in this line:

import Cadcordenador

This command compiles and executes the full contents of the module Cadcordenador. The entire module will be executed - Functions and classes will be defined and any code in the module body will pass execution.

Then all variables created by the module are automatically stored by python in a type object module which gets into sys.modules['Cadcordenador'] - that’s kind of a "cache".

If in the future, some other part of the code runs the command again:

import Cadcordenador

This time as sys.modules you already have the key Cadcordenador, the module does not run again. Instead, it returns the object that was already generated in the first execution.

So the correct way to run the same code twice is to put it into a function. This should be done inside your module Cadcordenador. For example, if your module is like this:

# Cadcordenador.py

class Application:
    ....

app = Application()
root.mainloop()

In this example the mainloop() is in the main body of the module and will only run once when it is imported. You must change and put the code you want to repeat within a function:

class Application:
    ....

def run():
    app = Application()
    root.mainloop()

if __name__ == '__main__':
    run()

As you can see, the part that starts the application is now inside the function run();

The if __name__ == '__main__' serves to protect the automatic execution of the code when it is imported. In other words, it makes the function run() is called automatically only when this module Cadcordenador is running directly, and not imported. When it is imported, the variable __name__ will be 'Cadcordenador' and not '__main__'.

With this, when using in your other module

 import Cadcordenador

Nothing will happen because the function run() not yet rolled, however, is already set. You can rotate it as many times as you want:

 Cadcordenador.run()
  • Good afternoon, I did as explained and the module Cadalunos.py runs, but when I try to call in the other module the function it gives the message: Exception in Tkinter callback Traceback (Most recent call last): File "D: Users t010038655 Appdata Local Programs Python Python36-32 lib Tkinter_init_.py", line 1699, in call Return self.func(*args) File "D: Users t010038655 Documents test App.py", line 13, in Cadalunos Cadalunos.run() Attributeerror: 'Function' Object has no attribute 'run'

  • It looks like it has a function with the same name as the module being defined in its code, @Wellingtonramos

Browser other questions tagged

You are not signed in. Login or sign up in order to post.