Accessing a Pyqt5 Widget and modules within a package

Asked

Viewed 62 times

0

I am making a program using as GUI the framework PyQt5, using UI files. I’m having trouble accessing the module inside the file .py that I create so that I put the functions inside them and thus better organize my program.

inserir a descrição da imagem aquiinserir a descrição da imagem aqui

The function I want to call is char_1 that’s inside characters.py. In the main program main.py I care for you this way from util.images.characters import checar, but in this way it returns me this error:

AttributeError: partially initialized module 'util.images.characters' has no attribute 'checar' (most likely due to a circular import)

Somehow, it can only access the function when it is inside the file __init__.py of the briefcase images. I wanted to know, in order to organize my program, how I can access this function inside the file characters.py. Below follows the codes "summarized" of the program. OBS: actionAlbedo is the name of the QAction of QMenu of the archive ui/tela.ui.

main.py

from PyQt5 import uic, QtWidgets, QtGui
from util.images.characters import checar

app = QtWidgets.QApplication([])

tela = uic.loadUi('ui/tela.ui')
tela.actionAlbedo.triggered.connect(checar)

tela.show()
app.exec_()

characters.py

import main
from PyQt5 import QtGui


def checar():
    main.tela.lb_background.setPixmap(QtGui.QPixmap('img/background/char_1.png'))

Grateful!

1 answer

0

Here is the mistake:

(most likely due to a circular import)

When you import a file, the code inside it runs. Soon, the file main py. is running the file characters py. which again calls the file main py.. Thus lifting a circular import.

One solution to this would be to pass the variable screen you want to access in the file characters py. as a function parameter.

def checar(tela):
    tela.lb_background.setPixmap(QtGui.QPixmap('img/background/char_1.png'))
  • Alan, this way didn’t work. I tried another way. I deleted directory images and created another python directory called characters. In it, I put it in the file __init__.py the code. main.py placed from util import characters. In charge I put tela.actionAlbedo.triggered.connect(characters.checar). But it still makes this mistake: AttributeError: partially initialized module 'util.characters' has no attribute 'mudar' (most likely due to a circular import). If you want to see the original is https://github.com/LucasRibeiroRJBR/genshin_app in the folder pyqt5. Thank you.

Browser other questions tagged

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