I have a pyqt5 code to print the dictionary output inside the QTextEdit
#code:-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(565, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.text_output = QtWidgets.QTextEdit(self.centralwidget)
self.text_output.setGeometry(QtCore.QRect(20, 10, 501, 251))
self.text_output.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.text_output.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.text_output.setObjectName("text_output")
self.btn_dict = QtWidgets.QPushButton(self.centralwidget)
self.btn_dict.setGeometry(QtCore.QRect(30, 280, 101, 21))
self.btn_dict.setObjectName("btn_dict")
self.btn_clear = QtWidgets.QPushButton(self.centralwidget)
self.btn_clear.setGeometry(QtCore.QRect(180, 280, 101, 21))
self.btn_clear.setObjectName("btn_clear")
self.btn_exit = QtWidgets.QPushButton(self.centralwidget)
self.btn_exit.setGeometry(QtCore.QRect(310, 280, 101, 21))
self.btn_exit.setObjectName("btn_exit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 565, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.btn_dict.setText(_translate("MainWindow", "PrintDictionary"))
self.btn_clear.setText(_translate("MainWindow", "Clear"))
self.btn_exit.setText(_translate("MainWindow", "Exit"))
def dictionary(self):
new_dict = {
'Name' : 'Ron',
'Age' : '21',
'city' : 'NY'
}
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
So instead of printing output on console , i want to the output inside QTextEdit when clicked on PrintDictionary button as well as how to clear QTextEdit when clicked on clear button or exit application when clicked on exit button
I'm new to PYQT5 so please help me out.
Thanks
First of all, the dictionary function does not necessarily have to be a member of the class, and since you do not pass the self to it, I assume that you have badly indentured your code in that part.
Going to the point, you must use the clicked signal and call a lambda function that performs the action through the append and clear method of QTextEdit.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
[...]
def dictionary(self):
new_dict = {
'Name' : 'Ron',
'Age' : '21',
'city' : 'NY'
}
return new_dict
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
ui.btn_dict.clicked.connect(lambda: ui.text_output.append(str(ui.dictionary())))
ui.btn_clear.clicked.connect(ui.text_output.clear)
MainWindow.show()
sys.exit(app.exec_())
Related
i have a QLineedit which i want to print out the user input once the button is pressed but it keeps throwing out this error
Process finished with exit code -1073740791 (0xC0000409)
from PyQt5.QtWidgets import QMessageBox
from PyQt5 import QtCore, QtGui, QtWidgets
import webbrowser
def subscribe_clicked():
print(sub.text())
msg = QMessageBox()
msg.setWindowTitle("Subscribe")
msg.setText("You will now recieve updates")
msg.setIcon(QMessageBox.Question)
msg.exec_()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(723, 785)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.join = QtWidgets.QLabel(self.centralwidget)
self.join.setObjectName("join")
self.gridLayout_2.addWidget(self.join, 1, 0, 1, 1)
self.sub = QtWidgets.QLineEdit(self.centralwidget)
self.sub.setObjectName("sub")
self.subscribe = QtWidgets.QPushButton(self.centralwidget)
self.subscribe.clicked.connect(subscribe_clicked)
self.subscribe.setObjectName("subscribe")
self.gridLayout_2.addWidget(self.subscribe, 3, 0, 1, 1)
self.sub.raise_()
self.subscribe.raise_()
self.sub.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 723, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
if i put in a manual entry it prints fine , but it wont call the information from the QlineEdit text box
The reason for error is in function
def subscribe_clicked():
print(sub.text())
msg = QMessageBox()
msg.setWindowTitle("Subscribe")
msg.setText("You will now recieve updates")
msg.setIcon(QMessageBox.Question)
msg.exec_()
In line print(sub.text()) the sub is undefined since it doesn't exist out of Ui_MainWindow. Therefore it throws an error:
Process finished with exit code -1073740791 (0xC0000409)
We can fix the problem by passing sub which is a QLineEdit in MainWindow class to subscribe_clicked() function. By doing the following:
Pass parameter to function with lambda by changing:
self.subscribe.clicked.connect(subscribe_clicked)
To
self.subscribe.clicked.connect(lambda:subscribe_clicked(self.sub))
Add parameter to function
def subscribe_clicked(sub):
print(sub.text())
msg = QMessageBox()
msg.setWindowTitle("Subscribe")
msg.setText("You will now recieve updates")
msg.setIcon(QMessageBox.Question)
msg.exec_()
Full Code:
from PyQt5.QtWidgets import QMessageBox
from PyQt5 import QtCore, QtGui, QtWidgets
import webbrowser
def subscribe_clicked(sub):
print(sub.text())
msg = QMessageBox()
msg.setWindowTitle("Subscribe")
msg.setText("You will now recieve updates")
msg.setIcon(QMessageBox.Question)
msg.exec_()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(723, 785)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.join = QtWidgets.QLabel(self.centralwidget)
self.join.setObjectName("join")
self.gridLayout_2.addWidget(self.join, 1, 0, 1, 1)
self.sub = QtWidgets.QLineEdit(self.centralwidget)
self.sub.setObjectName("sub")
self.subscribe = QtWidgets.QPushButton(self.centralwidget)
self.subscribe.clicked.connect(lambda:subscribe_clicked(self.sub))
self.subscribe.setObjectName("subscribe")
self.gridLayout_2.addWidget(self.subscribe, 3, 0, 1, 1)
self.sub.raise_()
self.subscribe.raise_()
self.sub.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 723, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I created a back button in new window and next button in main window. After I clicked the next button to show a new window, it was successfully hides the current main window but when I clicked back button on the new window, the application automatically closes itself. Any idea?
The code in first mainWindow.py file:
from PyQt5 import QtCore, QtGui, QtWidgets
try:
from secondWindow import Ui_SecondWindow
except:
import secondWindow
class Ui_firstWindow(object):
def secondWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_SecondWindow()
self.ui.setupUi(self.window)
self.window.show()
if self.window.show():
self.hide
def setupUi(self, firstWindow):
firstWindow.setObjectName("firstWindow")
firstWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(firstWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_next = QtWidgets.QPushButton(self.centralwidget)
self.btn_next.setGeometry(QtCore.QRect(160, 100, 521, 291))
self.btn_next.setObjectName("btn_next")
######To Open second window###########
self.btn_next.clicked.connect(self.secondWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(360, 420, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
firstWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(firstWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
firstWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(firstWindow)
self.statusbar.setObjectName("statusbar")
firstWindow.setStatusBar(self.statusbar)
self.retranslateUi(firstWindow)
QtCore.QMetaObject.connectSlotsByName(firstWindow)
def retranslateUi(self, firstWindow):
_translate = QtCore.QCoreApplication.translate
firstWindow.setWindowTitle(_translate("firstWindow", "MainWindow"))
self.btn_next.setText(_translate("firstWindow", "Next"))
self.label.setText(_translate("firstWindow", "First Window"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
firstWindow = QtWidgets.QMainWindow()
ui = Ui_firstWindow()
ui.setupUi(firstWindow)
firstWindow.show()
sys.exit(app.exec_())
The codes in second secondWindow.py file:
from PyQt5 import QtCore, QtGui, QtWidgets
try:
from mainWindow import Ui_firstWindow
except:
import mainWindow
class Ui_SecondWindow(object):
###function to go back the first window######
# #pyqtSlot()
def openBackFirstWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_firstWindow()
self.ui.setupUi(self.window)
self.window.show()
if self.window.show():
self.hide
def setupUi(self, SecondWindow):
SecondWindow.setObjectName("SecondWindow")
SecondWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(SecondWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_back = QtWidgets.QPushButton(self.centralwidget)
self.btn_back.setGeometry(QtCore.QRect(220, 100, 401, 281))
self.btn_back.setObjectName("btn_back")
###To go back to first windo######
self.btn_back.clicked.connect(self.openBackFirstWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(370, 440, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
SecondWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(SecondWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
SecondWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(SecondWindow)
self.statusbar.setObjectName("statusbar")
SecondWindow.setStatusBar(self.statusbar)
self.retranslateUi(SecondWindow)
QtCore.QMetaObject.connectSlotsByName(SecondWindow)
def retranslateUi(self, SecondWindow):
_translate = QtCore.QCoreApplication.translate
SecondWindow.setWindowTitle(_translate("SecondWindow", "MainWindow"))
self.btn_back.setText(_translate("SecondWindow", "Back"))
self.label.setText(_translate("SecondWindow", "Second Window"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
SecondWindow = QtWidgets.QMainWindow()
ui = Ui_SecondWindow()
ui.setupUi(SecondWindow)
SecondWindow.show()
sys.exit(app.exec_())
When i clicked the back button in second window, the application stopped working and exits itself. Any idea?
Do not modify the code generated by Qt Designer but create another class that inherits
from the appropriate widget and use the initial class to fill it.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_firstWindow(object):
def setupUi(self, firstWindow):
firstWindow.setObjectName("firstWindow")
firstWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(firstWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_next = QtWidgets.QPushButton(self.centralwidget)
self.btn_next.setGeometry(QtCore.QRect(160, 100, 521, 291))
self.btn_next.setObjectName("btn_next")
# ######To Open second window###########
# self.btn_next.clicked.connect(self.secondWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(360, 420, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
firstWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(firstWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
firstWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(firstWindow)
self.statusbar.setObjectName("statusbar")
firstWindow.setStatusBar(self.statusbar)
self.retranslateUi(firstWindow)
QtCore.QMetaObject.connectSlotsByName(firstWindow)
def retranslateUi(self, firstWindow):
_translate = QtCore.QCoreApplication.translate
firstWindow.setWindowTitle(_translate("firstWindow", "MainWindow"))
self.btn_next.setText(_translate("firstWindow", "Next"))
self.label.setText(_translate("firstWindow", "First Window"))
class Ui_SecondWindow(object):
def setupUi(self, SecondWindow):
SecondWindow.setObjectName("SecondWindow")
SecondWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(SecondWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_back = QtWidgets.QPushButton(self.centralwidget)
self.btn_back.setGeometry(QtCore.QRect(220, 100, 401, 281))
self.btn_back.setObjectName("btn_back")
# ###To go back to first windo######
# self.btn_back.clicked.connect(self.openBackFirstWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(370, 440, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
SecondWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(SecondWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
SecondWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(SecondWindow)
self.statusbar.setObjectName("statusbar")
SecondWindow.setStatusBar(self.statusbar)
self.retranslateUi(SecondWindow)
QtCore.QMetaObject.connectSlotsByName(SecondWindow)
def retranslateUi(self, SecondWindow):
_translate = QtCore.QCoreApplication.translate
SecondWindow.setWindowTitle(_translate("SecondWindow", "MainWindow"))
self.btn_back.setText(_translate("SecondWindow", "Back"))
self.label.setText(_translate("SecondWindow", "Second Window"))
class SecondWindow(QtWidgets.QMainWindow, Ui_SecondWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
class MainWindow(QtWidgets.QMainWindow, Ui_firstWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
######To Open second window###########
self.btn_next.clicked.connect(self.secondWindow)
def secondWindow(self):
self.window = SecondWindow()
###To go back to first windo######
self.window.btn_back.clicked.connect(self.openBackFirstWindow)
self.window.show()
self.hide()
###function to go back the first window######
def openBackFirstWindow(self):
self.show()
self.window.hide()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
firstWindow = MainWindow()
firstWindow.show()
sys.exit(app.exec_())
I wanted to open a new window by clicking on the pushbutton and type a number in the new window. After that when clicking on the "Ok" Button, the second window will be closed and the written number will be shown in the label, which was existed in the initial window. I wrote the following code but it writes 0 in the label and doesnt update.
The first Window:
from PyQt5 import QtCore, QtWidgets
from load3D import load3
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(316, 284)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 60, 191, 121))
self.label.setStyleSheet("background-color: rgb(255, 255, 255);")
self.label.setText("")
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(60, 190, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 316, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked['bool'].connect(self.get_value)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def get_value(self):
length = self.load3load()
self.label.setText(str(length))
def load3load(self):
self.MainWindow = QtWidgets.QMainWindow()
self.x = 0
self.ui = load3(self.x)
self.ui.setupUi(self.MainWindow)
self.MainWindow.show()
return self.ui.x
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Click"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
And The second window:
from PyQt5 import QtCore, QtGui, QtWidgets
class load3(object):
def __init__(self, x):
self.x = 0
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(283, 340)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 40, 71, 31))
font = QtGui.QFont()
font.setPointSize(18)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(90, 47, 113, 20))
self.lineEdit.setObjectName("lineEdit")
font = QtGui.QFont()
font.setPointSize(18)
self.buttonBox = QtWidgets.QDialogButtonBox(self.centralwidget)
self.buttonBox.setGeometry(QtCore.QRect(120, 260, 156, 23))
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 283, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.accepted.connect(MainWindow.close)
self.buttonBox.rejected.connect(MainWindow.close)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def accept(self):
self.x = self.lineEdit.text()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Image properties"))
self.label.setText(_translate("MainWindow", "X:"))
Any thoughts how to make it work ?
First of all, you should never edit the files generated with pyuic. They are intended to be used only as imported module. Read more about using Designer to understand how use them properly.
Second: if you need an input from the user from another window, you should use a QDialog, not a QMainWindow.
Then, if you only need a simple input value, use QInputDialog, possibly from one of its static methods, in your case getInt() will suffice.
Finally, the reason for which your code doesn't work is that after creating and showing the window you immediately get the x value, but after that the function returns immediately, since there's nothing "blocking" it (so it won't wait for any input from the user). That's what QDialogs are for: they wait from some input from the user before returning.
If you don't have the .ui files anymore, recreate them, then generate again the python files with pyuic and leave them there.
Supposing you've created a file named ui_mainwindow.py for the main window and ui_inpudialog.py for a dialog with a spinbox (not a line edit, since you need a numeric value) and a buttonbox:
from PyQt5 import QtWidgets
from ui_mainwindow import Ui_MainWindow
from ui_inputdialog import Ui_Dialog
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.get_value)
def get_value(self):
dialog = InputDialog(self)
# this will show the dialog and wait for the user to accept or reject it
if dialog.exec():
# get the value from the dialog
self.label.setText(str(dialog.getValue()))
class InputDialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
def getValue(self):
# return the current value of the spinbox
return self.spinBox.value()
Alternatively, if you don't need specific customization of the input dialog, just use QInputDialog as suggested before:
from PyQt5 import QtWidgets
from ui_mainwindow import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.get_value)
def get_value(self):
length = QtWidgets.QInputDialog.getInt(self, 'Insert value', 'Value',
min=0, max=100)
self.label.setText(str(length))
This question already has an answer here:
Get text from qtextedit and assign it to a variable
(1 answer)
Closed 2 years ago.
I need get text from QTextEdit, but have such trouble: Traceback (most recent call last):
File "main.py", line 28, in otpravit_naz
textboxValue = self.textEdit.text()
AttributeError: 'MyWin' object has no attribute 'textEdit'
This is my code(main.py):
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from test import *
import socket
sock = socket.socket()
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.otpravit.clicked.connect(self.otpravit_naz)
def mbox(self, body, title='Error'):
dialog = QMessageBox(QMessageBox.Information, title, body)
dialog.exec_()
def otpravit_naz(self):
print("1")
textboxValue = self.textEdit.text()
print(textboxValue)
#sock.connect(('192.168.1.16', 9090))
sock.connect(("192.168.1.45", 9090))
sock.send(b'textboxValue')
sock.close()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = MyWin()
myapp.show()
sys.exit(app.exec_())
And ui form:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(517, 283)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(60, 20, 421, 201))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(23)
self.verticalLayout.setObjectName("verticalLayout")
self.textEdit = QtWidgets.QTextEdit(self.verticalLayoutWidget)
self.textEdit.setObjectName("textEdit")
self.verticalLayout.addWidget(self.textEdit)
self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label.setLineWidth(0)
self.label.setMidLineWidth(0)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.list = QtWidgets.QComboBox(self.verticalLayoutWidget)
self.list.setObjectName("list")
self.list.addItem("")
self.list.addItem("")
self.list.addItem("")
self.verticalLayout.addWidget(self.list)
self.otpravit = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.otpravit.setObjectName("otpravit")
self.verticalLayout.addWidget(self.otpravit)
self.verticalLayout.setStretch(0, 20)
self.verticalLayout.setStretch(1, 20)
self.verticalLayout.setStretch(2, 20)
self.verticalLayout.setStretch(3, 20)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 517, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Предмет:"))
self.list.setItemText(0, _translate("MainWindow", "Русский"))
self.list.setItemText(1, _translate("MainWindow", "Литература"))
self.list.setItemText(2, _translate("MainWindow", "Английский"))
self.otpravit.setText(_translate("MainWindow", "Отправить"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
If it very stupid troble, pls don't kick me
You're "installing" the GUI on the self.ui object, so every widget that is on the ui is actually accessible as self.ui.someWidget.
Also, QTextEdit doesn't have a text() property, but toPlainText():
def otpravit_naz(self):
print("1")
textboxValue = self.ui.textEdit.toPlainText()
print(textboxValue)
I suggest you to never edit the files generated with pyuic, but always use them as imported modules; read more on using Designer; also, be careful to set the main layout on the central widget, not on its children, and add everything to that layout, otherwise the children widgets could be hidden whenever the window is resized.
I converted ui file from Pyqt5 designer:
Testing1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(380, 180, 112, 34))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 31))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Test1.py < is meant to add methods etc. etc.
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from testing1 import Ui_MainWindow
class myprog(Ui_MainWindow):
def __init__ (self, dialog):
Ui_MainWindow.__init__(self)
self.setupUi(dialog)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog = QtWidgets.QDialog()
test1 = myprog(dialog)
dialog.show()
sys.exit(app.exec_())
If I run Testin1.py it's all ok, but if run second script, test1.py, i get following message:
> builtins.AttributeError: 'QDialog' object has no attribute
> 'setCentralWidget'
I am really confused what to do, I would really really appreciate, if you could help me solve this problem. Any help is more than welcome.
In Testing1.py file, you create a MainWindow.
So in your Test1.py file, you should change the code
dialog = QtWidgets.QDialog()
to
dialog = QtWidgets.QMainWindow()