我想知道是否要建立一个新的窗口屏幕。 我已经创建了两个.py文件
我将popup.py导入Main.py。
当我单击SearchButton_2的按钮时,我希望popup.py窗口作为弹出窗口打开。
如何更改代码?
此文件名为Main.py,它是第一个文件。
import popup
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.setEnabled(True)
Dialog.resize(991, 471)
Dialog.setAutoFillBackground(False)
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setBold(False)
font.setWeight(50)
font1 = QtGui.QFont()
font1.setFamily("Agency FB")
font1.setBold(True)
font1.setWeight(75)
self.ListTable = QtWidgets.QTableWidget(Dialog)
self.ListTable.setGeometry(QtCore.QRect(0, 40, 991, 431))
self.ListTable.setMaximumSize(QtCore.QSize(1191, 16777215))
self.ListTable.setSizeIncrement(QtCore.QSize(0, 0))
self.ListTable.setBaseSize(QtCore.QSize(0, 0))
self.ListTable.setFont(font)
self.ListTable.setMouseTracking(False)
self.ListTable.setTabletTracking(False)
self.ListTable.setAcceptDrops(False)
self.ListTable.setToolTipDuration(-1)
self.ListTable.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.ListTable.setAutoScroll(True)
self.ListTable.setAutoScrollMargin(16)
self.ListTable.setShowGrid(True)
self.ListTable.setRowCount(0)
self.ListTable.setColumnCount(5)
self.ListTable.setObjectName("ListTable")
self.ListTable.setColumnWidth(0,280)
self.ListTable.setColumnWidth(1,190)
self.ListTable.setColumnWidth(2,190)
self.ListTable.setColumnWidth(3,190)
self.ListTable.setColumnWidth(4,150)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignTop)
font.setStyleStrategy(QtGui.QFont.PreferDefault)
item.setFont(font)
self.ListTable.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignVCenter)
item.setFont(font)
self.ListTable.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignTop)
item.setFont(font)
self.ListTable.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignVCenter)
item.setFont(font)
self.ListTable.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignTop)
item.setFont(font)
self.ListTable.setHorizontalHeaderItem(4, item)
self.SaveButton = QtWidgets.QPushButton(Dialog)
self.SaveButton.setGeometry(QtCore.QRect(10, 10, 40, 22))
self.SaveButton.setObjectName("SaveButton")
self.SearchButton_2 = QtWidgets.QPushButton(Dialog)
self.SearchButton_2.setGeometry(QtCore.QRect(60, 10, 60, 22))
self.SearchButton_2.setObjectName("SearchButton_2")
#self.SearchButton_2.clicked.connect()
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.ListTable.setHorizontalHeaderLabels(["111","222","333","444","555"])
self.SaveButton.setText(_translate("Dialog", "save"))
self.SearchButton_2.setText(_translate("Dialog", "search"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
Pialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
此文件名为popup.py,是第二个文件。 popup.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Popup(object):
def popup_ui(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(580, 100)
self.SettingTime = QtWidgets.QLabel(Dialog)
self.SettingTime.setGeometry(QtCore.QRect(10, 70, 61, 20))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(10)
font.setBold(True)
font.setWeight(50)
font1 = QtGui.QFont()
font1.setFamily("Agency FB")
font1.setPointSize(10)
font1.setBold(True)
font1.setWeight(75)
self.StartTime = QtWidgets.QDateTimeEdit(Dialog)
self.StartTime.setFont(font)
self.StartTime.setObjectName("SettingTime")
self.StartTime.setGeometry(QtCore.QRect(100, 70, 194, 22))
self.StartTime.setDateTime(QtCore.QDateTime.currentDateTime())
self.StartTime.setObjectName("StartTime")
self.SettingTime2 = QtWidgets.QLabel(Dialog)
self.SettingTime2.setFont(font)
self.SettingTime2.setObjectName("SettingTime2")
self.SettingTime2.setGeometry(QtCore.QRect(300, 72, 16, 16))
self.EndTime = QtWidgets.QDateTimeEdit(Dialog)
self.EndTime.setFont(font)
self.EndTime.setObjectName("EndTime")
self.EndTime.setGeometry(QtCore.QRect(320, 70, 194, 22))
self.EndTime.setDateTime(QtCore.QDateTime.currentDateTime())
self.Category = QtWidgets.QComboBox(Dialog)
self.Category.setGeometry(QtCore.QRect(90, 10, 76, 22))
self.Category.setObjectName("Category")
self.Category.addItem("")
self.Category.addItem("")
self.Category.addItem("")
self.Category.addItem("")
self.Category.addItem("")
self.Category.addItem("")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(170, 10, 321, 21))
self.lineEdit.setObjectName("lineEdit")
self.Artifacts = QtWidgets.QComboBox(Dialog)
self.Artifacts.setGeometry(QtCore.QRect(10, 10, 76, 22))
self.Artifacts.setObjectName("Artifacts")
self.Artifacts.addItem("")
self.Artifacts.addItem("")
self.Artifacts.addItem("")
self.Artifacts.addItem("")
self.Artifacts.addItem("")
self.SearchButton = QtWidgets.QPushButton(Dialog)
self.SearchButton.setGeometry(QtCore.QRect(500, 10, 75, 22))
self.SearchButton.setCheckable(True)
self.SearchButton.setChecked(True)
self.SearchButton.setObjectName("SearchButton")
self.FileSize = QtWidgets.QLineEdit(Dialog)
self.FileSize.setGeometry(QtCore.QRect(100, 40, 321, 21))
self.FileSize.setObjectName("FileSize")
self.SettingTime_2 = QtWidgets.QLabel(Dialog)
self.SettingTime_2.setGeometry(QtCore.QRect(10, 40, 91, 20))
self.SettingTime_2.setFont(font)
self.SettingTime_2.setObjectName("SettingTime_2")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.SettingTime.setText(_translate("Dialog", "111"))
self.Category.setItemText(0, _translate("Dialog", "222"))
self.Category.setItemText(1, _translate("Dialog", "333"))
self.Category.setItemText(2, _translate("Dialog", "444"))
self.Category.setItemText(3, _translate("Dialog", "555"))
self.Category.setItemText(4, _translate("Dialog", "666"))
self.Category.setItemText(5, _translate("Dialog", "777"))
self.SettingTime2.setText(_translate("Dialog", "~"))
self.Artifacts.setItemText(0, _translate("Dialog", "AAA"))
self.Artifacts.setItemText(1, _translate("Dialog", "BBB"))
self.Artifacts.setItemText(2, _translate("Dialog", "CCC"))
self.Artifacts.setItemText(3, _translate("Dialog", "DDD"))
self.Artifacts.setItemText(4, _translate("Dialog", "EEE"))
self.SearchButton.setText(_translate("Dialog", "FFF"))
self.SettingTime_2.setText(_translate("Dialog", "GGG"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Popup()
ui.popup_ui(Dialog)
Dialog.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
首先,您应该不首先更改代码。由pyuic
生成的文件应该始终用作脚本中的导入模块,并且应该从不进行手动修改。详细了解using Designer。
因此,首先,再次生成这些文件。然后,您必须创建一个新脚本,该脚本将成为程序的“主”脚本,并从此处创建逻辑。以下示例暗示用pyuic重新创建的文件分别命名为first.py
和popup.py
。
from PyQt5 import QtWidgets
from first import Ui_Dialog
from popup import Popup
class MainWindow(QtWidgets.QDialog, Ui_Dialog):
def __init__(self):
super().__init__()
self.setupUi(self)
self.SearchButton_2.clicked.connect(self.showPopup)
def showPopup(self):
popup = PopupWindow(self)
popup.exec_()
class PopupWindow(QtWidgets.QDialog, Popup):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())