I have 3 QLineEdit widgets that are readonly and listen to keyevents. Well I want to let the user to choose what QLineEdit he is going to use(because it's going to be a shortcut setting window). It appears when in readonly mode the widget is non-clickable. Is there a way to make it clickable or do the same thing without readonly?
I made the code in Qt Designer.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(392, 290)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(140, 70, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent1
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(140, 150, 113, 21))
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent2
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(140, 220, 113, 21))
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent3
self.lineEdit_3.setObjectName("lineEdit_3")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def newOnkeyPressEvent1(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit.setText(str(keyname))
def newOnkeyPressEvent2(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit_2.setText(str(keyname))
def newOnkeyPressEvent3(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit_3.setText(str(keyname))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "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_())
You need to determine which lineedit is under focus with self.centralwidget.focusWidget(). Here is full working code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(392, 290)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(140, 70, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(140, 150, 113, 21))
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(140, 220, 113, 21))
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit_3.setObjectName("lineEdit_3")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def newOnkeyPressEvent(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
lineEdit = self.centralwidget.focusWidget()
lineEdit.setText(str(keyname))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "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_())
Related
Here is my code for changing the item height of items in list
ui.symbolsList.insertItem(ui.symbolsList.count(), ui.addSymbolText.text())
count = ui.symbolsList.count()
for i in range(count):
item = ui.symbolsList.item(i)
item.setSizeHint(QSize(item.sizeHint().width(), 60))
This is not changing the height of items in QListWidget. I also tried creating the item first with custom height and then adding that item to the list.
What else do I need to do?
Following is the working code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QSize
class Ui_MainWindow(object):
def submit(self):
self.listWidget.insertItem(self.listWidget.count(), self.lineEdit.text())
count = self.listWidget.count()
for i in range(count):
item = self.listWidget.item(i)
item.setSizeHint(QSize(item.sizeHint().width(), 60))
self.lineEdit.setText("")
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(170, 110, 256, 192))
self.listWidget.setObjectName("listWidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(170, 70, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(300, 70, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.pushButton.clicked.connect(self.submit)
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_())
The problem is that the default sizeHint() is QSize(-1, -1) which is invalid so when creating a new QSize with width -1 makes it invalid causing it not to be used when painting the item. The solution is not to use the width of the default sizeHint() but to set the width to 0:
item.setSizeHint(QSize(0, 60))
So am testing a bit with MDI area, the problem is that when I try to Open a Subwindow inside the MDI this happens.
Any way to make the Little Subwindow bigger enough to be readble?
Am doing Everything inside an only test file.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(488, 392)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(280, 290, 91, 51))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.clicked.connect(self.Open)
self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
self.mdiArea.setGeometry(QtCore.QRect(60, 20, 261, 211))
self.mdiArea.setObjectName("mdiArea")
self.subwindow = QtWidgets.QWidget()
self.subwindow.setObjectName("subwindow")
self.pushButton = QtWidgets.QPushButton(self.subwindow)
self.pushButton.setGeometry(QtCore.QRect(100, 80, 56, 17))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 488, 18))
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 Open(self):
self.mdiArea.addSubWindow(self.subwindow)
self.subwindow.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_3.setText(_translate("MainWindow", "Open"))
self.subwindow.setWindowTitle(_translate("MainWindow", "Subwindow"))
self.pushButton.setText(_translate("MainWindow", "YAY!"))
class mywindow(QtWidgets.QMainWindow):
def __init__(self):
super(mywindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
app = QtWidgets.QApplication([])
application = mywindow()
application.show()
app.exec()
You have to resize the QMdiSubWindow:
def Open(self):
sw = self.mdiArea.addSubWindow(self.subwindow)
self.subwindow.show()
sw.resize(640, 480)
This question already has an answer here:
How to record a pressed key combination in the PyQT5 dialog window
(1 answer)
Closed 3 years ago.
I am trying to get a readonly QLineEdit in a second window to show the name of the key pressed. For example if I press key a it shows a Key and if I press the spacebar it shows SpaceBar Key. I searched the net for solutions but most of them were about how to close a window when a certain key is pressed.
I have a working sample made with tkinter linked below.
https://gist.github.com/sangonkim/e397c420d93485f3896f9440bbcc77aa
I made 2 simple windows with QDesigner for the question.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(347, 259)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 110, 121, 28))
self.pushButton.clicked.connect(self.connect)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def connect(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow2()
self.ui.setupUi(self.window)
self.window.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "To next window"))
class Ui_MainWindow2(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(384, 261)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(130, 100, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Main_Window = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(Main_Window)
Main_Window.show()
sys.exit(app.exec_())
I've tried adding
def keyinput(self, event):
print(event.key())
to Ui_MainWindow2see if I can capture the key I an typing. But it does not work.
And also tried
def keyinput(self, event):
self.lineEdit.setText(str(event.key()))
And failed.
into setupUi add:
MainWindow.keyPressEvent = self.newOnkeyPressEvent
and define function:
def newOnkeyPressEvent(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit.setText(str(keyname))
full code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(347, 259)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 110, 121, 28))
self.pushButton.clicked.connect(self.connect)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def connect(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow2()
self.ui.setupUi(self.window)
self.window.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "To next window"))
class Ui_MainWindow2(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(384, 261)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(130, 100, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
MainWindow.keyPressEvent = self.newOnkeyPressEvent
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
def newOnkeyPressEvent(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit.setText(str(keyname))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Main_Window = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(Main_Window)
Main_Window.show()
sys.exit(app.exec_())
from PyQt5 import QtCore, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(200, 40, 81, 20))
self.label.setObjectName("label")
self.lineedit = QtWidgets.QLineEdit(self.centralwidget)
self.lineedit.setGeometry(QtCore.QRect(270, 40, 113, 20))
self.lineedit.setObjectName("lineedit")
MainWindow.setCentralWidget(self.centralwidget)
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", "label1:"))
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 want to have a QInputDialog under my lineedit, so I can choose from "yes" or "no"from the input dialog, how to I put it into my code?
ALso how do I get the value of the inputdialog?
Thanks!
Try it:
from PyQt5 import QtCore, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(200, 40, 81, 20))
self.label.setObjectName("label")
self.lineedit = QtWidgets.QLineEdit(self.centralwidget)
self.lineedit.setGeometry(QtCore.QRect(270, 40, 113, 20))
self.lineedit.setObjectName("lineedit")
# +++
self.btn = QtWidgets.QPushButton(self.centralwidget)
self.btn.setGeometry(QtCore.QRect(200, 70, 81, 20))
self.btn.setObjectName("btn")
self.btn.clicked.connect(self.getTextInputDialog)
MainWindow.setCentralWidget(self.centralwidget)
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", "label1:"))
self.btn.setText(_translate("MainWindow", "InputDialog"))
# +++
def getTextInputDialog(self):
text, okPressed = QtWidgets.QInputDialog.getText(None,
"Get text",
"Your name:",
QtWidgets.QLineEdit.Normal,
"")
if okPressed and text != '':
print(text)
ui.lineedit.setText(text)
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_())
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(9576, 698)
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(9)
MainWindow.setFont(font)
MainWindow.setFocusPolicy(QtCore.Qt.StrongFocus)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 221, 421))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.RButton = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton.setFont(font)
self.RButton.setObjectName("RButton")
self.verticalLayout.addWidget(self.RButton)
self.RButton2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton2.setFont(font)
self.RButton2.setObjectName("RButton2")
self.verticalLayout.addWidget(self.RButton2)
self.RButton3 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton3.setFont(font)
self.RButton3.setObjectName("RButton3")
self.verticalLayout.addWidget(self.RButton3)
self.Button = QtWidgets.QPushButton(self.centralwidget)
self.Button.setGeometry(QtCore.QRect(390, 150, 351, 221))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(24)
self.Button.setFont(font)
self.Button.setFocusPolicy(QtCore.Qt.NoFocus)
self.Button.setAutoDefault(False)
self.Button.setObjectName("Button")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 957, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.RButton.setText(_translate("MainWindow", "버튼1"))
self.RButton2.setText(_translate("MainWindow", "버튼2"))
self.RButton3.setText(_translate("MainWindow", "버튼3"))
self.Button.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_())
when button clicked program is turn off...
I need help
The problem is caused because QMessageBox.about() requires as a first parameter a widget but self, that is Ui_MainWindow, is not a widget. Qt Designer does not generate a widget but creates a class that fills a widget. It is also not recommended to modify the class generated by Qt Designer, it is appropriate to create a class that inherits from the appropriate widget and use the design class
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())