Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make a windows Notification Manager based on PyQt5

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to make a windows notification manager based on PyQt5". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Python framework win10toast, which can be used to do the message notification function of windows. Some event notification functions can be achieved by setting the interval between notifications, such as reminding us to drink water on time when we plunge into the coding process.

The interface layout is still pyqt5's ui design, using the interface to directly set the content and time you want to prompt can give us a regular notice.

The related parts of UI are these commonly used component packages.

From PyQt5.QtGui import * # UI interface related from PyQt5.QtCore import * # Core component package from PyQt5.QtWidgets import * # UI layout related modules

Interface theme related modules, here is the use of black module theme.

From qdarkstyle import load_stylesheet_pyqt5

Apply the relevant modules.

Import sysimport os

The only special of the following modules is that the win10toast module is used for windows notifications, and one uses a timer in the python thread.

From win10toast import ToastNotifier # Import system Notification object import time # system time Module import datetimefrom threading import Timer # timer

First of all, write out the relevant parts of the layout and interface components of the UI interface, the interface is also relatively simple, using two kinds of layout, one is the Form form layout, the other is the vertical layout.

Class WinNotify (QWidget): def _ _ init__ (self): super (WinNotify Self). _ init__ () self.init_ui () def init_ui (self): self.setWindowTitle ('windows Notification Manager official account: [Python concentration Camp]') self.setWindowIcon (QIcon ('Notification .ico') self.setFixedWidth (550C) self.notify_subject_label = QLabel () self.notify_subject_label.setText ('notify the Lord Question') self.notify_subject_text = QLineEdit () self.notify_subject_text.setPlaceholderText ('enter notification subject') self.notify_current_label = QLabel () self.notify_current_label.setText ('notification content') self.notify_current_text = QLineEdit () self.notify_current_text.setPlaceholderText ('enter notification content') self .notice _ time_label = QLabel () self.notify_time_label.setText ('notification interval') self.notify_time_combox = QComboBox () self.notify_time_combox.addItems (['10 | minutes') '30 | minutes','45 | minutes','60 | minutes' Self.notify_icon_path = QLineEdit () self.notify_icon_path.setPlaceholderText ('notification icon (* .ico)') self.notify_icon_btn = QPushButton () self.notify_icon_btn.setText ('select icon') self.notify_icon_btn.clicked.connect (self.notify_icon_btn_click) self.start_btn = QPushButton () self.start_btn.setText ('Open notification!') Self.start_btn.clicked.connect (self.start_btn_click) form = QFormLayout () form.addRow (self.notify_subject_label, self.notify_subject_text) form.addRow (self.notify_current_label, self.notify_current_text) form.addRow (self.notify_time_label, self.notify_time_combox) form.addRow (self.notify_icon_path Self.notify_icon_btn) vbox = QVBoxLayout () vbox.addLayout (form) vbox.addWidget (self.start_btn) self.thread_ = WorkThread (self) self.setLayout (vbox) def notify_icon_btn_click (self): file = QFileDialog.getOpenFileName (self, os.getcwd (), 'Open Picture' 'ICO File (* .ico) 'print (file [0]) self.notify_icon_path.setText (file [0]) def start_btn_click (self): self.start_btn.setEnabled (False) self.thread_.start ()

When the main function starts the application, the black theme is added to the layout of the app.

App.setStyleSheet (load_stylesheet_pyqt5 ())

The thread runs the relevant part and writes the child thread by inheriting the QThead class.

Class WorkThread (QThread): def _ _ init__ (self,parent=None): super (WorkThread Self). _ init__ (parent) self.parent = parent self.notify = ToastNotifier () self.working = True def _ del__ (self): self.working = False self.wait () def run (self): self.show_toast () def show_toast (self): notify_head = self.parent.notify_subject_text.text () Notify_text = self.parent.notify_current_text.text () notify_ico = self.parent.notify_icon_path.text () notify_sen = self.parent.notify_time_combox.currentText (). Split ('|') [0] notify_sen = int (notify_sen) * 60 print ('current time:% s'% (datetime.datetime.now (). Strftime ('% YMY%) MMI% d% HVA% MRV% S')) self.notify.show_toast (f "{notify_head}") F "{notify_text}", duration=5, threaded=True, icon_path=notify_ico) while self.notify.notification_active (): time.sleep (0.005) timer = Timer (notify_sen, self.show_toast) timer.start () "how to make a windows Notification Manager based on PyQt5" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report