“在PyQt中创建一个旨在使本地社区图书馆受益的程序。该程序应该能够在库存清单中添加,编辑和删除图书条目。
包括一个列表小部件,一个行编辑框,一个标签,四个按钮。“
我可以把所有这些东西拖放到PyQt5中,保存为.ui,转换为.py并在IDLE中打开,但是如何让按钮做任何事情?另外,怎么做我让我的IDLE显示这个盒子?
This is what the program should look like
这是我保存.ui文件并转换为.py:
后剩下的代码# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'listoper.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 450)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(260, 180, 256, 192))
self.listWidget.setObjectName("listWidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 230, 171, 91))
self.lineEdit.setObjectName("lineEdit")
self.Enterbookdetails = QtWidgets.QLabel(self.centralwidget)
self.Enterbookdetails.setGeometry(QtCore.QRect(20, 200, 101, 16))
self.Enterbookdetails.setObjectName("Enterbookdetails")
self.Add = QtWidgets.QPushButton(self.centralwidget)
self.Add.setGeometry(QtCore.QRect(10, 330, 75, 23))
self.Add.setObjectName("Add")
self.Delete = QtWidgets.QPushButton(self.centralwidget)
self.Delete.setGeometry(QtCore.QRect(110, 330, 75, 23))
self.Delete.setObjectName("Delete")
self.Edit = QtWidgets.QPushButton(self.centralwidget)
self.Edit.setGeometry(QtCore.QRect(10, 370, 75, 23))
self.Edit.setObjectName("Edit")
self.Deleteall = QtWidgets.QPushButton(self.centralwidget)
self.Deleteall.setGeometry(QtCore.QRect(110, 370, 75, 23))
self.Deleteall.setObjectName("Deleteall")
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.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.Enterbookdetails.setText(_translate("MainWindow", "Enter Book Details:"))
self.Add.setText(_translate("MainWindow", "Add"))
self.Delete.setText(_translate("MainWindow", "Delete"))
self.Edit.setText(_translate("MainWindow", "Edit"))
self.Deleteall.setText(_translate("MainWindow", "Delete All"))
显然我应该使用以下代码,但我的按钮仍然不起作用:
def btn_KtoP_clicked(self):
kilo = float(self.edt_Kilo.text())
pound = (kilo * 2.2)
self.edt_Pounds.setText(str(pound))
def btn_PtoK_clicked(self):
pound = float(self.edt_Pounds.text())
kilo = (pound/2.2)
self.edt_Kilo.setText(str(kilo))
答案 0 :(得分:0)
根据您的上述代码,这应该有效
import sys
from PyQt5 import QtCore, QtWidgets
from YOUR_UI_MODULE import Ui_MainWindow
class sow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.main_ui=Ui_MainWindow()
self.main_ui.setupUi(self)
self.main_ui.Add.clicked.connect(self.doAdd)
self.main_ui.Edit.clicked.connect(self.doEdit)
def doAdd(self):
print("do add")
def doEdit(self):
print("do edit")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = sow()
myapp.show()
sys.exit(app.exec_())
查看PyQT5安装附带的示例并从那里开始。 我的位于C:\ Python34 \ Lib \ site-packages \ PyQt5 \ examples
PyQT有一个参考文档,我的位于C:/Python32/Lib/site-packages/PyQt4/doc/html/classes.html
我建议您在进一步编码之前阅读本教程,以便了解自己在做什么。 http://zetcode.com/gui/pyqt5/firstprograms/