kbyun
kbyun

Reputation: 442

pyqt5 calling function does not give same results

I am trying to develop a GUI to show my robot and obstacles around it. I created a class GUI and it has a member function showObstacle to show obstacle. Since I want to keep all the obstacles in place, I create new obstacle object in a vector to store all the objects.

I called this function in class's init function to test and it was successful.

But when I call this function in different class, it doesn't show the obstacles on the GUI window. In class TCP_Connection (where I get information about the robot), I created myGui class and I called showObstacle function.

After debugging, it turns out it creates the Obstacle Objects whenever it is called, but it doesn't display it on the window

#this is file 1, GUI Class
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQt5.QtCore import Qt
from pynput import keyboard

class App(QWidget):
    def __init__(self, OtherWindow):

        super().__init__()
        self.setGeometry(100,100,440,800)
        self.setWindowTitle("New GUI Interface Window")
        self.currentState = 0
        self.obstCounter = 0
        self.d = []

        #testing showObstacle
        self.showObstacle(205, 305) #this works
        self.showObstacle(210,315)  #this also works
        self.show()

    def showObstacle(self, Obs_x, Obs_y):
        self.obstImage = QtGui.QImage('obsta_edited.png')
        self.pixmap_obst = QtGui.QPixmap(self.obstImage)
        self.d.append("O{0}".format(self.obstCounter))
        self.d[self.obstCounter] = QtWidgets.QLabel(self)
        self.d[self.obstCounter].setPixmap(self.pixmap_obst)
        self.d[self.obstCounter].move(Obs_x, Obs_y)
        self.d[self.obstCounter].adjustSize()
        self.obstCounter += 1

        print(self.d)
        print("Obstacle Run")

this is the calling class

from myGUI import App
from PyQt5.QtWidgets import QWidget,QLabel,QLineEdit, QHBoxLayout,QVBoxLayout,QMainWindow,QPushButton, QFrame, QDesktopWidget, QApplication
from PyQt5.Qt import QStackedLayout
import sys, random

class TCP_Communication:
    def __init__(self):
        super().__init__()
        self.openWindow()

    def openWindow(self):

        self.window = QMainWindow()
        self.myGui = App(self.window)
        self.myGui.showObstacle(215, 320)
        self.stacked_layout = QStackedLayout()
        self.stacked_layout.addWidget(self.myGui)

    def tcp_showObstacle(self,x,y):
        self.myGui.showObstacle(x,y)


if __name__ == '__main__':
    app = QApplication([])
    tcp_com = TCP_Communication()
    sys.exit(app.exec_())

Edited : attached the result ; Suppose to show 4 obstacles but only shows 2

Edited2 : Code is executable now

Suppose to show 4 obstacles but only shows 2

Upvotes: 1

Views: 60

Answers (1)

eyllanesc
eyllanesc

Reputation: 243975

The problem is that the QLabels are not showing, the main widget when displayed at the beginning makes your children show up, but after that it does not make them visible, as it is in your case, so your task is to call their method show() directly:

def showObstacle(self, Obs_x, Obs_y):
    pixmap_obst = QtGui.QPixmap('obsta_edited.png')
    self.d.append("O{0}".format(self.obstCounter))
    label = QtWidgets.QLabel(self)
    label.setPixmap(pixmap_obst)
    label.move(Obs_x, Obs_y)
    label.adjustSize()
    label.show() # <---show QLabel

    self.d[self.obstCounter] = label
    self.obstCounter += 1

    print(self.d)
    print("Obstacle Run")

Upvotes: 2

Related Questions