我不太确定如何在使用QtDesigner设置的网格中显示定制的小部件,如果您能以这种方式设置我的代码有什么不便之处,请多多关照,并向您解释我为什么我不能那样做。
我试图从我创建的用于导入小部件的功能中添加小部件,但是效果不佳。在下面执行以下代码也不起作用。我不确定如何将小部件添加到我的网格并将我的网格设置为父网格,同时将新的小部件设置为子小部件。
SELECT DATE_FORMAT('2019-11-05','%Y%m%d') as time
我希望我的窗口小部件(这是一个运行的日期和时间时钟)出现在网格中,因为我将在要创建的其他窗口中使用该窗口小部件,并想知道如何将其应用于其他窗口。我尝试运行以下代码,但错误from PyQt5 import QtCore, QtGui, QtWidgets
from SelectDateTimeFinal import Ui_SelectDateTime
from CurrentDateAndDigitalClock import Ui_widget_currentDate_digitalClock
class Ui_MainWindow(object):
def openCurrentDateAndDigitalClock(self):
self.widget = QtWidgets.QWidget()
self.Ui = Ui_widget_currentDate_digitalClock()
self.Ui.setupUi(self.widget)
#NEED HEP HERE
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(950, 565)
MainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_Store3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Store3.setGeometry(QtCore.QRect(60, 400, 321, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_Store3.setFont(font)
self.pushButton_Store3.setObjectName("pushButton_Store3")
self.pushButton_Store2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Store2.setGeometry(QtCore.QRect(580, 330, 331, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_Store2.setFont(font)
self.pushButton_Store2.setObjectName("pushButton_Store2")
self.pushButton_Store1 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Store1.setGeometry(QtCore.QRect(60, 330, 321, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_Store1.setFont(font)
self.pushButton_Store1.setObjectName("pushButton_Store1")
self.pushButton_Store4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Store4.setGeometry(QtCore.QRect(580, 400, 331, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_Store4.setFont(font)
self.pushButton_Store4.setObjectName("pushButton_Store4")
self.pushButton_Reset = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Reset.setGeometry(QtCore.QRect(10, 60, 271, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_Reset.setFont(font)
self.pushButton_Reset.setObjectName("pushButton_Reset")
self.pushButton_SetDateTime = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_SetDateTime.setGeometry(QtCore.QRect(10, 20, 271, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_SetDateTime.setFont(font)
self.pushButton_SetDateTime.setObjectName("pushButton_SetDateTime")
self.pushButton_SetDateTime.clicked.connect(self.openSelectDateTimeWindow)
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(330, 20, 281, 101))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout_SelectedDateTime = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout_SelectedDateTime.setContentsMargins(0, 0, 0, 0)
self.gridLayout_SelectedDateTime.setObjectName("gridLayout_SelectedDateTime")
self.label_SelectedDateTime = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_SelectedDateTime.setText("")
self.label_SelectedDateTime.setAlignment(QtCore.Qt.AlignCenter)
self.label_SelectedDateTime.setObjectName("label_SelectedDateTime")
self.gridLayout_SelectedDateTime.addWidget(self.label_SelectedDateTime, 0, 0, 1, 1)
self.gridLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget_2.setGeometry(QtCore.QRect(640, 20, 291, 101))
self.gridLayoutWidget_2.setObjectName("gridLayoutWidget_2")
self.gridLayout_CurrentDateTime = QtWidgets.QGridLayout(self.gridLayoutWidget_2)
self.gridLayout_CurrentDateTime.setContentsMargins(0, 0, 0, 0)
self.gridLayout_CurrentDateTime.setObjectName("gridLayout_CurrentDateTime")
self.gridLayout_CurrentDateTime.addChildWidget(self.openCurrentDateAndDigitalClock)
# NEED HELP HERE
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Canteen System"))
self.pushButton_Store3.setText(_translate("MainWindow", "PushButton"))
self.pushButton_Store2.setText(_translate("MainWindow", "PushButton"))
self.pushButton_Store1.setText(_translate("MainWindow", "PushButton"))
self.pushButton_Store4.setText(_translate("MainWindow", "PushButton"))
self.pushButton_Reset.setText(_translate("MainWindow", "Reset to Current Date and Time"))
self.pushButton_SetDateTime.setText(_translate("MainWindow", "Set Date and Time"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
timer = QtCore.QTimer()
timer.timeout.connect(Ui_widget_currentDate_digitalClock.showDate)
timer.timeout.connect(Ui_widget_currentDate_digitalClock.showTime)
timer.start(100)
sys.exit(app.exec_())
不断出现。
答案 0 :(得分:0)
我不确定是否能看到Ui_widget_currentDat_digitalClock
的代码,但我怀疑这是
timer.timeout.connect(Ui_widget_currentDate_digitalClock.showDate)
timer.timeout.connect(Ui_widget_currentDate_digitalClock.showTime)
应该类似于
timer.timeout.connect(ui.Ui.showDate)
timer.timeout.connect(ui.Ui.showTime)
在Ui_MainWindow.setupUi
中,此行
self.gridLayout_CurrentDateTime.addChildWidget(self.openCurrentDateAndDigitalClock)
应该类似于
self.openCurrentDateAndDigitalClock() # this sets up self.widget
self.gridLayout_CurrentDateTime.addWidget(self.widget, 0, 0, 1, 1)
答案 1 :(得分:0)
pyuic生成的输出永远不能用作代码的基础。实际上,它应该单独使用,主要是因为您可能需要在某个时候更改GUI,这使得将新代码与之前编辑的现有代码进行集成非常困难。
如PyQt documentation中所述,您应该导入该代码,并将其用作应用程序的起点,从中创建窗口和小部件的实际类,并添加所需的连接和程序逻辑
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from ui_mainwindow import Ui_MainWindow
from CurrentDateAndDigitalClock import Ui_widget_currentDate_digitalClock
class DigitalClock(QWidget, Ui_widget_currentDate_digitalClock):
def __init__(self):
super(DigitalClock, self).__init__()
self.setupUi(self)
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.digitalClock = DigitalClock()
self.gridLayout_CurrentDateTime.addWidget(self.digitalClock)
self.pushButton_SetDateTime.clicked.connect(self.openSelectDateTimeWindow)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
此外,您永远不应使用addChildWidget
,它是QLayout子类使用的内部函数。请改用特定布局的addWidget
方法。
请注意,由于您尚未共享用于自定义小部件的代码,因此我没有添加计时器连接;恐怕会出现一些问题,因为您已将其连接到 class 方法showDate
和showTime
,而连接则需要一定程度的GUI交互(包括使用计时器自动更新某些文本)应该使用 instance 类方法。