Solved Threading don't creating buttons in frame
-
I created a function to create a button, but, when I call it by python thread doesn't work. btw, when I call it directly it works.
Is possible to create buttons/items using a Threading?
import sys from PySide2 import QtCore, QtGui, QtWidgets from PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtWidgets import * import threading class Ui_MainWindow(object): def setupUi(self, MainWindow): if not MainWindow.objectName(): MainWindow.setObjectName(u"MainWindow") MainWindow.resize(800, 600) self.centralwidget = QWidget(MainWindow) self.centralwidget.setObjectName(u"centralwidget") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QMenuBar(MainWindow) self.menubar.setObjectName(u"menubar") self.menubar.setGeometry(QRect(0, 0, 800, 21)) MainWindow.setMenuBar(self.menubar) self.statusbar = QStatusBar(MainWindow) self.statusbar.setObjectName(u"statusbar") MainWindow.setStatusBar(self.statusbar) QMetaObject.connectSlotsByName(MainWindow) MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None)) # call thread self.run() # starting thread and calling another function def run(self): print('run?') thread = threading.Thread(target=self.execth, args=()) thread.daemon = True thread.start() # creating the button def execth(self): print('execth?') self.pushButton_2 = QPushButton(self.centralwidget) self.pushButton_2.setObjectName(u"pushButton_2") self.pushButton_2.setGeometry(QRect(340, 270, 111, 23)) self.pushButton_2.setText(QCoreApplication.translate("MainWindow", u"background", None)) class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self): super(MainWindow, self).__init__() self.setupUi(self) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
-
@Black-Cat QObjects are not thread-safe, and even more so, QWidgets (which inherit from QObject) should not be created or modified in a thread other than the main thread. On the other hand I don't see any advantage of creating GUI in another thread. What are the benefits of using threads to create GUI elements?
So: Is it possible to create buttons/items using a Threading? No, you can't and shouldn't.
-
@eyllanesc because in my original application I have a function that creation some buttons and when the function is turn on my app freezes by some second
-
@Black-Cat
Try not to use threads, and if you do they cannot create or do any UI stuff.If you really have to use them, emit a signal for the UI to create widgets if necessary.
-
@Black-Cat Creating some elements of the GUI should not freeze the application so there are 2 options: 1) You are creating many buttons How many buttons are you creating? 2) There is another part of your application that is not creating buttons that is freezing the window.
-
only freeze in this process, basically:
1 - connect in a socket
2 - if socket response create a green button
3 - if socket not response create a red button(have 1 socket)
-
@Black-Cat So what is freezing the application is the sockets, not the creation of buttons. So you have the following options:
- Execute the sockets in the secondary thread and use the signals to update the GUI in the main thread.
- Use QTcpSocket (or similar class) since its methods are asynchronous.
-
@Black-Cat How do you connect using sockets? I hope you do not use any blocking API? Qt sockets are asynchronous, so nothing should block if you use them properly.
-
@jsulm I don't using the QT socket, currently i'm using the socket of python... The freeze was because when a server socket is down it try much of 6 attemps to connect, and it freeze my software.
Btw I created the QTitens in normal mode and put the connections of socket in thread..... and don't freeze anymore
-
@Black-Cat
It sounds quite possible that the only reason you need threads is because you are choosing to use Python sockets (blocking?) in the first place! If that is the case, you should reconsider. -