gpt4 book ai didi

python - QCalendarWidget 呈现较小

转载 作者:行者123 更新时间:2023-12-01 00:34:57 29 4
gpt4 key购买 nike

我正在尝试使用QCalendarWidget,但它没有按预期在用户界面中呈现。我看到的示例显示了类似日历选择器的对象,但在我的例子中,我得到了一个非常小的字段渲染。用户界面如下所示:

QCalendarWidget

这是我第一次使用它,所以我不确定是否遗漏了步骤。关于我可能做错了什么有什么想法吗?这是正在使用的完整代码:

from PyQt5.QtWidgets import QMainWindow, QCalendarWidget, QLabel
from PyQt5 import QtCore, QtWidgets, QtGui


import sys

class Example(QMainWindow):

def __init__(self):
super(Example, self).__init__()

self.initUI()

def initUI(self):

cal = QCalendarWidget(self)
cal.setGridVisible(True)
cal.move(20, 20)
cal.clicked[QtCore.QDate].connect(self.showDate)

self.lbl = QLabel(self)
date = cal.selectedDate()
self.lbl.setText(date.toString())
self.lbl.move(20, 200)

self.setGeometry(100,100,300,300)
self.setWindowTitle('Calendar')
self.show()

def showDate(self, date):

self.lbl.setText(date.toString())

def main():

app = QtWidgets.QApplication(sys.argv)
mainWin = Example()
mainWin.show()
sys.exit( app.exec_() )

if __name__ == '__main__':
main()

最佳答案

在QMainWindow的centralWidget中使用布局,例如QVBoxLayout:

import sys

from PyQt5 import QtCore, QtGui, QtWidgets


class Example(QtWidgets.QMainWindow):
def __init__(self):
super(Example, self).__init__()

self.initUI()

def initUI(self):

cal = QtWidgets.QCalendarWidget(gridVisible=True)
cal.clicked.connect(self.showDate)

self.lbl = QtWidgets.QLabel()
date = cal.selectedDate()
self.lbl.setText(date.toString())

central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
lay.addWidget(cal)
lay.addWidget(self.lbl)

self.setGeometry(100, 100, 300, 300)
self.setWindowTitle("Calendar")

@QtCore.pyqtSlot(QtCore.QDate)
def showDate(self, date):
self.lbl.setText(date.toString())


def main():

app = QtWidgets.QApplication(sys.argv)
mainWin = Example()
mainWin.show()
sys.exit(app.exec_())


if __name__ == "__main__":
main()

enter image description here

关于python - QCalendarWidget 呈现较小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57874616/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com