GUI:创建一个窗口,在窗口上可以显示实时时间,并写有标题“现在是北京时间”,用PYqt编写
- User
GUI:创建一个窗口,在窗口上可以显示实时时间,并写有标题“现在是北京时间”,用PYqt编写
- Assistant
以下为使用 PyQt5 编写 GUI 窗口的代码:
import sys from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow from PyQt5.QtCore import QTimer, Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('现在是北京时间') self.setGeometry(100, 100, 300, 100) self.label = QLabel(self) self.label.setAlignment(Qt.AlignCenter) self.label.setStyleSheet('font-size: 20pt') self.label.setGeometry(0, 0, 300, 100) timer = QTimer(self) timer.timeout.connect(self.updateTime) timer.start(1000) def updateTime(self): current_time = QDateTime.currentDateTime() time_str = current_time.toString('yyyy-MM-dd hh:mm:ss') self.label.setText(time_str) if __name__ == '__main__': app = QApplication(sys.argv) main_window = MainWindow() main_window.show() sys.exit(app.exec_())执行代码后会弹出一个窗口,窗口上方为现在是北京时间的标题,下方显示实时时间。updateTime 函数使用 QTimer 每秒钟更新一次时间。注意,QDateTime 需要先导入 PyQt5.QtCore 才能使用。
- User
GUI:创建一个窗口,在窗口上可以显示实时时间,并写有标题“现在是北京时间”,同时在窗口上用一张图片作为背景,用PYqt编写
- Assistant
以下为使用 PyQt5 编写 GUI 窗口的代码,其中使用了 QLabel 和 QPixmap 协同实现背景图片的设置:
import sys from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow from PyQt5.QtGui import QPixmap from PyQt5.QtCore import QTimer, Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('现在是北京时间') self.setGeometry(100, 100, 300, 100) pixmap = QPixmap('bg.jpg') bg_label = QLabel(self) bg_label.setPixmap(pixmap) bg_label.resize(pixmap.width(), pixmap.height()) self.label = QLabel(self) self.label.setAlignment(Qt.AlignCenter) self.label.setStyleSheet('font-size: 20pt; color: white') self.label.setGeometry(0, 0, 300, 100) timer = QTimer(self) timer.timeout.connect(self.updateTime) timer.start(1000) def updateTime(self): current_time = QDateTime.currentDateTime() time_str = current_time.toString('yyyy-MM-dd hh:mm:ss') self.label.setText(time_str) if __name__ == '__main__': app = QApplication(sys.argv) main_window = MainWindow() main_window.show() sys.exit(app.exec_())执行代码后会弹出一个带背景图片的窗口,窗口上方为现在是北京时间的标题,下方显示实时时间。bg.jpg 是图片文件的路径,可以根据实际情况修改。注意,QDateTime 需要先导入 PyQt5.QtCore 才能使用。
- User
GUI:创建一个窗口,在窗口的正中央可显示实时时间,并写有标题“现在是北京时间”,同时在窗口上用一张图片作为背景,用PYqt编写
- Assistant
以下为使用 PyQt5 编写 GUI 窗口的代码,其中使用了 QGridLayout 和 setAlignment 方法将 QLabel 居中显示:
import sys from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QWidget, QGridLayout from PyQt5.QtGui import QPixmap from PyQt5.QtCore import QTimer, Qt, QDateTime class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('现在是北京时间') self.setGeometry(100, 100, 300, 100) central_widget = QWidget(self) self.setCentralWidget(central_widget) grid_layout = QGridLayout(central_widget) grid_layout.setAlignment(Qt.AlignCenter) pixmap = QPixmap('bg.jpg') bg_label = QLabel(self) bg_label.setPixmap(pixmap) bg_label.resize(pixmap.width(), pixmap.height()) grid_layout.addWidget(bg_label) self.label = QLabel(self) self.label.setAlignment(Qt.AlignCenter) self.label.setStyleSheet('font-size: 20pt; color: white') grid_layout.addWidget(self.label) timer = QTimer(self) timer.timeout.connect(self.updateTime) timer.start(1000) def updateTime(self): current_time = QDateTime.currentDateTime() time_str = current_time.toString('yyyy-MM-dd hh:mm:ss') self.label.setText(time_str) if __name__ == '__main__': app = QApplication(sys.argv) main_window = MainWindow() main_window.show() sys.exit(app.exec_())执行代码后会弹出一个带背景图片的窗口,窗口中央显示实时时间,窗口上方为现在是北京时间的标题。bg.jpg 是图片文件的路径,可以根据实际情况修改。注意,QDateTime 需要先导入 PyQt5.QtCore 才能使用。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.