工具:cursor
prompt:用pyqt帮我写个番茄钟
然后ai就开始写了
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QLCDNumber
from PyQt5.QtCore import QTimer, Qt
class TomatoClock(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Create labels
self.timer_label = QLabel('25:00')
self.timer_label.setAlignment(Qt.AlignCenter)
self.timer_label.setStyleSheet('font-size: 80pt')
self.status_label = QLabel('Work')
self.status_label.setStyleSheet('color: red')
# Create buttons
self.start_button = QPushButton('Start')
self.stop_button = QPushButton('Stop')
self.reset_button = QPushButton('Reset')
# Create timer
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.update_timer)
# Create LCD number
self.lcd = QLCDNumber()
self.lcd.setDigitCount(2)
self.lcd.display(25)
# Create layouts
vbox = QVBoxLayout()
hbox = QHBoxLayout()
# Add widgets to layouts
vbox.addWidget(self.timer_label)
vbox.addWidget(self.status_label)
vbox.addWidget(self.lcd)
hbox.addWidget(self.start_button)
hbox.addWidget(self.stop_button)
hbox.addWidget(self.reset_button)
vbox.addLayout(hbox)
# Set layout
self.setLayout(vbox)
# Set window properties
self.setWindowTitle('Tomato Clock')
self.setGeometry(100, 100, 300, 200)
# Connect buttons to functions
self.start_button.clicked.connect(self.start_timer)
self.stop_button.clicked.connect(self.stop_timer)
self.reset_button.clicked.connect(self.reset_timer)
def start_timer(self):
self.timer.start()
def stop_timer(self):
self.timer.stop()
def reset_timer(self):
self.timer.stop()
self.timer_label.setText('25:00')
self.status_label.setText('Work')
self.status_label.setStyleSheet('color: red')
self.lcd.display(25)
def update_timer(self):
time_left = self.timer_label.text()
minutes, seconds = time_left.split(':')
if int(seconds) > 0:
seconds = str(int(seconds) - 1)
elif int(minutes) > 0:
minutes = str(int(minutes) - 1)
seconds = '59'
else:
self.timer.stop()
if self.status_label.text() == 'Work':
self.status_label.setText('Break')
self.status_label.setStyleSheet('color: green')
self.timer_label.setText('05:00')
self.lcd.display(5)
else:
self.status_label.setText('Work')
self.status_label.setStyleSheet('color: red')
self.timer_label.setText('25:00')
self.lcd.display(25)
self.timer_label.setText(f'{minutes.zfill(2)}:{seconds.zfill(2)}')
if __name__ == '__main__':
app = QApplication(sys.argv)
clock = TomatoClock()
clock.show()
sys.exit(app.exec_())
最后打包文件,
1 安装PyInstaller库:可以使用pip安装PyInstaller库,命令为pip install pyinstaller
。
-
打开命令行窗口并导航到TomatoClock应用程序的目录。
-
运行以下命令将应用程序打包成单个可执行文件:
pyinstaller --onefile --noconsole main.py
这将在dist文件夹中创建一个可执行文件,而不会弹出命令行窗口。