diff --git a/main/main.py b/main/main.py index 7ec9499..d1222b8 100644 --- a/main/main.py +++ b/main/main.py @@ -5,7 +5,7 @@ from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt, pyqtSignal import configparser -from utils.BasicUtils import get_todo_list, remove_todo_list, change_value +from utils.BasicUtils import get_todo_list, remove_todo_list, change_value, read_ini, set_exit_status from utils.CreateToDo import CreateToDo from utils.QSSLoader import QSSLoader from view.ImportantView import Important @@ -180,8 +180,10 @@ class MainWidget(QWidget): self.stackedWidget.addWidget(todo_list) self.system_listWidget.setItemWidget(item, item.widget) + def change(self): print(1) + # 点击创建新的分组 def create_item(self, new_list='新建列表', uid=None): # 创建一个没有名字的item @@ -206,7 +208,7 @@ class MainWidget(QWidget): self.system_listWidget.takeItem(self.system_listWidget.currentRow()) # 重命名分组 - # todo : 重命名还没做好 + def rename_item(self): # curRow = self.todo_list.currentRow() # item = self.todo_list.item(curRow) @@ -251,6 +253,104 @@ class MainWindow(QWidget): # self.main_widget.resize(50, 200) self.layout.addWidget(self.main_widget) + self.tray_icon = QSystemTrayIcon(self) + self.tray_icon.setIcon(QIcon(os.getcwd() + '/../images/todo_info.svg')) + # # self.style().standardIcon(QStyle.SP_ComputerIcon)) + # + # self.checkbox = QCheckBox('最小化') + # layout.addWidget(self.checkbox) + + ''' + Define and add steps to work with the system tray icon + show - show window + hide - hide window + exit - exit from application + ''' + show_action = QAction("显示", self) + quit_action = QAction("退出", self) + hide_action = QAction("隐藏", self) + show_action.triggered.connect(self.show) + hide_action.triggered.connect(self.hide) + quit_action.triggered.connect(QApplication.instance().quit) + tray_menu = QMenu() + tray_menu.addAction(show_action) + tray_menu.addAction(hide_action) + tray_menu.addAction(quit_action) + self.tray_icon.setContextMenu(tray_menu) + self.tray_icon.show() + + # self.exit_widget = QWidget(self) + # + # self.exit_widget.setLayout(layout) + + # 关闭窗口时弹出确认消息 + + def closeEvent(self, event): + # 创建一个消息盒子(提示框) + quitMsgBox = QMessageBox() + self.save_config = QCheckBox('记住选择') + self.save_config.isChecked() + quitMsgBox.setCheckBox(self.save_config) + # self.save_config.stateChanged.connect(self.save_exit) + quitMsgBox.resize(300, 200) + # 设置提示框的标题 + quitMsgBox.setWindowTitle('确认窗口') + # 设置提示框的内容 + quitMsgBox.setText('你确定退出吗?') + # 创建两个点击的按钮,修改文本显示内容 + buttonY = QPushButton('退出程序') + + buttonN = QPushButton('最小化到托盘') + # 将两个按钮加到这个消息盒子中去,并指定yes和no的功能 + style = """ + /*设置堆栈按钮样式*/ + QPushButton { + /* padding: 3px 20px;*/ + /* text-align:center;*/ + background-color:orange; + border-radius:15px; + } + QPushButton:hover { + /* padding: 3px 20px;*/ + /* text-align:center;*/ + background-color:#dddddd; + border-radius:15px; + } + QPushButton:pressed { + /* padding: 3px 20px;*/ + /* text-align:center;*/ + background-color:#9f9f9f; + border-radius:15px; + } + """ + buttonY.setStyleSheet(style) + buttonN.setStyleSheet(style) + + quitMsgBox.addButton(buttonY, QMessageBox.YesRole) + + quitMsgBox.addButton(buttonN, QMessageBox.NoRole) + exit_status = read_ini('System', 'exitstatus') + if exit_status == 'None': + # self.center(quitMsgBox) + quitMsgBox.exec_() + # 判断返回值,如果点击的是Yes按钮,我们就关闭组件和应用,否则就忽略关闭事件 + if quitMsgBox.clickedButton() == buttonY: + if self.save_config.isChecked(): + set_exit_status('Exit') + event.accept() + else: + if self.save_config.isChecked(): + set_exit_status('Min') + self.hide() + event.ignore() + elif exit_status == 'Exit': + event.accept() + elif exit_status == 'Min': + # print('Min') + self.hide() + event.ignore() + + if __name__ == "__main__": app = QApplication(sys.argv) diff --git a/test/MinimizeToTray.py b/test/MinimizeToTray.py new file mode 100644 index 0000000..f432583 --- /dev/null +++ b/test/MinimizeToTray.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys + +try: + from PyQt5.QtCore import QSize + from PyQt5.QtWidgets import ( + QApplication, QMainWindow, + QLabel, QGridLayout, QWidget, + QCheckBox, QSystemTrayIcon, + QSpacerItem, QSizePolicy, QMenu, QAction, QStyle) +except ImportError: + from PySide2.QtCore import QSize + from PySide2.QtWidgets import ( + QApplication, QMainWindow, + QLabel, QGridLayout, QWidget, + QCheckBox, QSystemTrayIcon, + QSpacerItem, QSizePolicy, QMenu, QAction, QStyle) + + +class MainWindow(QMainWindow): + """ + Сheckbox and system tray icons. + Will initialize in the constructor. + """ + check_box = None + tray_icon = None + + # Override the class constructor + def __init__(self): + # Be sure to call the super class method + QMainWindow.__init__(self) + + self.setMinimumSize(QSize(480, 80)) # Set sizes + self.setWindowTitle("System Tray Application") # Set a title + # Create a central widget + central_widget = QWidget(self) + # Set the central widget + self.setCentralWidget(central_widget) + + grid_layout = QGridLayout(self) # Create a QGridLayout + # Set the layout into the central widget + central_widget.setLayout(grid_layout) + grid_layout.addWidget( + QLabel("Application, which can minimize to Tray", self), 0, 0) + + # Add a checkbox, which will depend on the behavior of the program when the window is closed + self.check_box = QCheckBox('Minimize to Tray') + grid_layout.addWidget(self.check_box, 1, 0) + grid_layout.addItem(QSpacerItem( + 0, 0, QSizePolicy.Expanding, QSizePolicy.Expanding), 2, 0) + + # Init QSystemTrayIcon + self.tray_icon = QSystemTrayIcon(self) + self.tray_icon.setIcon( + self.style().standardIcon(QStyle.SP_ComputerIcon)) + + ''' + Define and add steps to work with the system tray icon + show - show window + hide - hide window + exit - exit from application + ''' + show_action = QAction("Show", self) + quit_action = QAction("Exit", self) + hide_action = QAction("Hide", self) + show_action.triggered.connect(self.show) + hide_action.triggered.connect(self.hide) + quit_action.triggered.connect(QApplication.instance().quit) + tray_menu = QMenu() + tray_menu.addAction(show_action) + tray_menu.addAction(hide_action) + tray_menu.addAction(quit_action) + self.tray_icon.setContextMenu(tray_menu) + self.tray_icon.show() + + # Override closeEvent, to intercept the window closing event + # The window will be closed only if there is no check mark in the check box + def closeEvent(self, event): + if self.check_box.isChecked(): + event.ignore() + self.hide() + self.tray_icon.showMessage( + "Tray Program", + "Application was minimized to Tray", + QSystemTrayIcon.Information, + 2000 + ) + + +if __name__ == "__main__": + app = QApplication(sys.argv) + mw = MainWindow() + mw.show() + sys.exit(app.exec_()) diff --git a/test/SystemTray.py b/test/SystemTray.py new file mode 100644 index 0000000..7a67ae4 --- /dev/null +++ b/test/SystemTray.py @@ -0,0 +1,128 @@ +import os +import sys + +from PyQt5 import QtWidgets +from PyQt5.QtGui import * +from PyQt5.QtCore import * +from PyQt5.QtWidgets import ( + QApplication, QMainWindow, + QLabel, QGridLayout, QWidget, + QCheckBox, QSystemTrayIcon, QFormLayout, + QSpacerItem, QSizePolicy, QMenu, QAction, QStyle, QDialog, QPushButton, QMessageBox) + + +class MessageBox(QMessageBox): + def __init__(self): + super(MessageBox, self).__init__() + save_config = QCheckBox('记住选择') + self.layout().addWidget(save_config) + + +class SystemTray(QWidget): + def __init__(self): + super(SystemTray, self).__init__() + self.resize(500, 400) + # layout = QFormLayout() + # # self.setLayout(layout) + # + # label = QLabel('最小化到系统托盘?') + # layout.addRow(label) + # Init QSystemTrayIcon + self.tray_icon = QSystemTrayIcon(self) + self.tray_icon.setIcon(QIcon(os.getcwd() + '/../images/todo_info.svg')) + # # self.style().standardIcon(QStyle.SP_ComputerIcon)) + # + # self.checkbox = QCheckBox('最小化') + # layout.addWidget(self.checkbox) + + ''' + Define and add steps to work with the system tray icon + show - show window + hide - hide window + exit - exit from application + ''' + show_action = QAction("显示", self) + quit_action = QAction("退出", self) + hide_action = QAction("隐藏", self) + show_action.triggered.connect(self.show) + hide_action.triggered.connect(self.hide) + quit_action.triggered.connect(QApplication.instance().quit) + tray_menu = QMenu() + tray_menu.addAction(show_action) + tray_menu.addAction(hide_action) + tray_menu.addAction(quit_action) + self.tray_icon.setContextMenu(tray_menu) + self.tray_icon.show() + + # self.exit_widget = QWidget(self) + # + # self.exit_widget.setLayout(layout) + + # 关闭窗口时弹出确认消息 + def closeEvent(self, event): + # 创建一个消息盒子(提示框) + quitMsgBox = QMessageBox() + quitMsgBox.resize(300, 200) + # 设置提示框的标题 + quitMsgBox.setWindowTitle('确认窗口') + # 设置提示框的内容 + quitMsgBox.setText('你确定退出吗?') + # 创建两个点击的按钮,修改文本显示内容 + buttonY = QPushButton('退出程序') + + buttonN = QPushButton('最小化到托盘') + # 将两个按钮加到这个消息盒子中去,并指定yes和no的功能 + style = """ +/*设置堆栈按钮样式*/ +QPushButton { +/* padding: 3px 20px;*/ +/* text-align:center;*/ + background-color:orange; + border-radius:15px; +} +QPushButton:hover { +/* padding: 3px 20px;*/ +/* text-align:center;*/ + background-color:#dddddd; + border-radius:15px; +} +QPushButton:pressed { +/* padding: 3px 20px;*/ +/* text-align:center;*/ + background-color:#9f9f9f; + border-radius:15px; +} + """ + buttonY.setStyleSheet(style) + buttonN.setStyleSheet(style) + + quitMsgBox.addButton(buttonY, QMessageBox.YesRole) + + quitMsgBox.addButton(buttonN, QMessageBox.NoRole) + quitMsgBox.exec_() + # 判断返回值,如果点击的是Yes按钮,我们就关闭组件和应用,否则就忽略关闭事件 + if quitMsgBox.clickedButton() == buttonY: + event.accept() + else: + self.hide() + event.ignore() + + # if self.checkbox.isChecked(): + # event.ignore() + # self.hide() + # if self.check_box.isChecked(): + # event.ignore() + # self.hide() + # self.tray_icon.showMessage( + # "Tray Program", + # "Application was minimized to Tray", + # QSystemTrayIcon.Information, + # 2000 + # ) + + +if __name__ == '__main__': + app = QApplication(sys.argv) + main = SystemTray() + main.show() + sys.exit(app.exec_()) diff --git a/utils/BasicUtils.py b/utils/BasicUtils.py index 00974f7..db06733 100644 --- a/utils/BasicUtils.py +++ b/utils/BasicUtils.py @@ -2,6 +2,7 @@ import json import os import platform from datetime import datetime +import configparser def return_work_dir(): @@ -189,6 +190,35 @@ def remove_todo_list(uid): # print(os.path.join(root, name)) os.remove(os.path.join(root, name)) + +def read_ini(section, key): + config_path = return_work_dir() + try: + ini_path = config_path + 'PyQtToDoList.ini' + config = configparser.ConfigParser() + config.read(ini_path) + return config[section][key] + except Exception as e: + print('', e) + return False + + +def set_exit_status(status): + config_path = return_work_dir() + try: + ini_path = config_path + 'PyQtToDoList.ini' + config = configparser.ConfigParser() + config.read(ini_path) + config['System']['exitstatus'] = status + # print(config['S']) + with open(ini_path, 'w') as f: + config.write(f) + return True + except Exception as e: + print('', e) + return False + + # print(read_init_file()) # webdav_hostname = read_init_file() # print(webdav_hostname[2]) @@ -197,3 +227,5 @@ def remove_todo_list(uid): # print(load_myday_important('Important')) # change_myday_important_conf('Important', 'theme', 2) # print(get_myday_important_conf('Important','Theme')) +# print(read_ini('System', 'exitstatus')) +# print(set_exit_status('Min')) diff --git a/utils/CreateConfigure.py b/utils/CreateConfigure.py index dbbb5e3..ec2fbc5 100644 --- a/utils/CreateConfigure.py +++ b/utils/CreateConfigure.py @@ -3,7 +3,6 @@ import platform import os from utils import BasicUtils - # todo : 用来创建软件的配置信息 from utils.BasicUtils import return_work_dir @@ -49,7 +48,7 @@ class CreateConfigure: } config['Account'] = options - config['ToDoList'] = {} + config['System'] = {'exitStatus': 'None'} with open(config_file, 'w') as f: config.write(f) @@ -64,5 +63,4 @@ class CreateConfigure: # elif platform.system() == 'Windows': # return os.getcwd() + '/PyQtToDoList.ini' - # CreateConfigure('NextCloud', 'admin', '19990903@lyp', 'https://cloud.liyp.cc')