This question already has answers here:
A QApplication instance already exists
(3 answers)
Closed 3 years ago.
I have main ui, and want to run children ui on click in main ui.
children:
class sessionGenerator(QtWidgets.QMainWindow, designTg.Ui_MainWindow2):
def __init__(self):
super().__init__()
self.setupUi(self)
self.showHelp.clicked.connect(self.showHelpFull)
self.next_1.clicked.connect(self.nextOne)
self.next_2.clicked.connect(self.nextTwo)
self.next_3.clicked.connect(self.nextThree)
# all another code
if I use
app2 = QtWidgets.QApplication(sys.argv)
window2 = sessionGenerator()
window2.show()
app2.exec_()
it appears on a second and throws 0xC0000005
just sessionGenerator runs great itself. But i can't figure out how to run it from another ui
Do not create second main application.
only
window2 = sessionGenerator()
window2.show()
This lines need to be before first app exec.
app.exec_()
Related
This question already has answers here:
In PyQt, what is the best way to share data between the main window and a thread
(1 answer)
Background thread with QThread in PyQt
(7 answers)
Example of the right way to use QThread in PyQt?
(3 answers)
Closed 4 months ago.
I've always wondered about what is the right way to share variable data with QThread and UI data. Well, for example,
#main.py
class MainUI(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
self.workerThread = Thread()
self.workerThread.mySignal.connect(self.function_name)
#worker.py
class Thread(QThread):
mySignal = pyqtSignal(str)
def __init__(self):
super().__init__()
def run(self):
self.mySignal.emit(some_string)
I know this code will emit and share "some_string" variable with MainUI class.
However; I don't think pyqt have an option to share variable data MainUI -> Thread
So, I am writing a code just like
self.workerThread.variable = None
What is desirable method to write a code?
This question already has answers here:
Autocomplete from .ui
(4 answers)
reading UI file and connecting elements such as buttons, text, etc
(1 answer)
Closed 2 years ago.
I am working on pyqt5 project where I am designing the ui in qt designer and writing its python code in pycharm. Ui has a button and label. Once button is clicked, label value is changed. Below is the code:
import sys
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QApplication, QMainWindow
class ROCKET(QMainWindow):
def __init__(self):
super(ROCKET, self).__init__()
loadUi('ui/gui.ui', self)
self.pushButton.clicked.connect(self.btn_click)
def btn_click(self):
self.label.setText("CLICKED")
app = QApplication(sys.argv)
window = ROCKET()
window.show()
app.exec_()
Pycharm gives warning for pushButton and label:
Unresolved attribute reference 'pushButton' for class 'ROCKET'
but if I am running the code, I am getting correct output. How can I remove these warnings and make the code correct.
The fact that you can refer to your widgets as attributes of the main window is a convenience that is offered by PyQt5 via loadUi. PyCharm is complaining because it didn't see you explicitly define the pushButton attribute for your class.
One way you can get around the warning would be to explicitly define the attribute:
self.pushButton = self.findChild(QPushButton, "name_of_push_button")
self.pushButton.clicked.connect(self.btn_click)
This question already has an answer here:
pyqt5 not showing window [duplicate]
(1 answer)
Closed 3 years ago.
I'm getting an unknown error 'Badly placed ()'s' when attempting to build a QT widget .
from PyQt4 import QtGui
import sys
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(50,50,500,300)
window.setWindowTitle("pyqt window")
window.show()
A empty window with "pyqt window" as title
It worked after adding sys.exit(app.exec_()) to the code
This question already has answers here:
How to capture output of Python's interpreter and show in a Text widget?
(5 answers)
Closed 4 years ago.
I wrote a program to perform measurements and currently I launch it via the Spyder IDE. The program is written in Python 3.6.3. The GUI was made using PyQt5 and it should be the main focus of the user, but I also print() many informations in Spyder's console.
In preparation for switching to an .exe instead of a .py, since there will be no console anymore, I would like to add a LineEdit to my interface where all the printing would occur. Ideally it would display both my print()s and the various error messages generated during execution. How do I redirect those prints to a LineEdit?
Most of the information I found during my research was about making a LineEdit some kind of Windows cmd equivalent but examples were overkill compared to what I'm trying to do. Thanks in advance.
A quick and dirty method is to just redefine the builtin print function.
from PyQt5 import QtWidgets
IS_EXE = True # There is probably a way to detect this at runtime
class Window(QtWidgets.QPlainTextEdit):
def __init__(self):
super(Window, self).__init__()
self.setReadOnly(True)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
win = Window()
win.show()
# Replace the builtin print function
if IS_EXE:
print = win.appendPlainText
# Print some stuff
print('foo')
print('bar')
app.exec_()
This question already has answers here:
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 6 months ago.
I have a GUI program,
It auto create buttons from a name list,
and connect to a function prints its name.
but when I run this program, I press all the buttons,
they all return the last button's name.
I wonder why this thing happens. can any one help?
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import logging
logging.basicConfig(level=logging.DEBUG,)
class MainWindow(QWidget):
def init(self):
names = ('a','b','c')
lo = QHBoxLayout(self)
for name in names:
button = QPushButton(name,self)
lo.addWidget(button)
self.connect(button,SIGNAL("clicked()"),
lambda :logging.debug(name))
if __name__=="__main__":
app = QApplication(sys.argv)
m = MainWindow();m.init();m.show()
app.exec_()
result like:
python t.py
DEBUG:root:c
DEBUG:root:c
DEBUG:root:c
I see at least one bug in your code.
Replace:
lambda :logging.debug(name)
By:
lambda name=name: logging.debug(name)
See Why results of map() and list comprehension are different? for details.