I decided to split my program into several files. One of them is gui.py. It contains a function that builds a menu:

def mainWinMenu(root, heads): menuBar = Menu(root) menuFile = Menu(menuBar, tearoff=0) menuAbout = Menu(menuBar, tearoff=0) menuBar.add_cascade(menu=menuFile, label="Файл") menuBar.add_cascade(menu=menuAbout, label="Справка") ... return menuBar 

And there is a main file. Here is his excerpt:

 from tkinter import * from tkinter.ttk import * from tkinter.messagebox import * from socket import socket, AF_INET, SOCK_STREAM from multiprocessing import Process, Pipe from queries import * from gui import * import pickle import sqlite3 import datetime as dt import os, sys ... root = Tk() root.title("Клиент системы управления задачами") upMenu = mainWinMenu(root, headsru) table = taskTable(root, headsru, result) root.config(menu=upMenu) root.mainloop() 

When you run the main file, an error occurs:

Traceback (most recent call last): File "D: \ GDrive \ python \ tasker \ taskerClient \ tasker_lite.py", line 27, in upMenu = mainWinMenu (root, headsru) File "D: \ GDrive \ python \ tasker \ taskerClient \ gui.py ", line 5, in mainWinMenu menuBar = Menu (root) NameError: name 'Menu' is not defined

When it was all in one file, there was no problem. The function starts to operate in the module itself, and not basically. How can I fix this?

  • one
    Import Menu to gui.py - gil9red
  • Now I transferred all the imports to gui.py and it worked. Is it possible to somehow avoid this? So that all data is substituted in the final file. - Skotinin
  • 2
    Modules need to import! And - advice - do not use the "import *" construct! - Xyanight 7:49 pm
  • Got it. Thank. - Skotinin
  • @Skotinin issue, please, the answer - gil9red

1 answer 1

Now I transferred all the imports to gui.py and it worked. According to the observations, splitting a working file into several components is a dubious idea. Since you will need to import not only modules from the library, but also modules from other files, even if the current file is not the main one. In other words, only the arguments do not need to be imported into the source file.

For example, in the table.py file you described the table, in the button.py file you described the button on this table, in the butt_func.py file you described the action of the button, and main.py is the main file. It happens that in table.py you need to import not only the button, but also its action. Just import all the elements in main.py can not.

PS answer issued :)