代码
import random
import sys
import threading
import time
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
now = lambda: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
titles: str = "Python2 Python3 Java Go PHP JavaScript C C++ C#"
titles: list = titles.split(" ")
titles_d = {key: 0 for key in titles}
ico_file_path = "../qt/ico/Lamborghini.ico"
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.init_ui()
def init_ui(self):
self.resize(1000, 600)
self.setWindowIcon(QIcon(ico_file_path))
self.now_btn = QPushButton(self)
self.now_btn.setGeometry(300, 100, 400, 100)
self.now_btn.setToolTip("此按钮只显示当前时间,点击无效")
self.now_btn.setFont(QFont("Monaco", 22))
t1 = threading.Thread(target=self.show_now_time)
t2 = threading.Thread(target=self.random_window_title)
t1.setDaemon(True)
t2.setDaemon(True)
t1.start()
t2.start()
self.btn = QPushButton("退出", self)
self.btn.setToolTip("退出窗口程序")
self.btn.setGeometry(450, 400, 100, 30)
self.btn.clicked.connect(self.when_btn_click)
def random_window_title(self):
while True:
window_title = random.choice(titles)
titles_d[window_title] += 1
self.setWindowTitle("{} 共计出现{}次".format(window_title, titles_d[window_title]))
time.sleep(1)
def show_now_time(self):
while True:
self.now_btn.setText(now())
time.sleep(1)
def when_btn_click(self):
print("Bye Bye")
QApplication.instance().quit()
def mousePressEvent(self, mouse):
print("鼠标按下")
def mouseReleaseEvent(self, mouse):
print("鼠标释放")
def closeEvent(self, event):
reply = QMessageBox.question(self, "useless", "不爱我了对不对?")
print(reply)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())