前言
本文給大家分享的是如何通過用PyQt5制作小型桌面應用
PyQt概述
PyQt5是Qt框架的Python/ target=_blank class=infotextkey>Python語言實現,由Riverbank Computing開發,是最強大的GUI庫之一。PyQt提供了一個設計良好的窗口控件集合,每一個PyQt控件都對應一個Qt控件,因此PyQt的API接口與Qt的API接口很接近,但PyQt不再使用QMake系統和Q_OBJECT宏。
PyQt5可以做這些桌面程序。
準備了Python 編寫的 15 個小型桌面應用程序
桌面應用程序
開發工具
Python版本:3.7
相關模塊:
socket模塊
time模塊
sys模塊
threading模塊
PyQt5模塊
環境搭建
安裝Python并添加到環境變量,pip安裝需要的相關模塊即可。
Conda環境
建議安裝anaconda集成環境,簡稱conda環境, 內部默認安裝數據分析(Numpy/Pandas)、爬蟲Scrapy框架、Web框架、PyQt等相關工具。
安裝目錄
drwxr-xr-x 3 Apple staff 96 2 25 2019 Anaconda-Navigator.app drwxr-xr-x 449 apple staff 14368 10 10 18:48 bin drwxr-xr-x 269 apple staff 8608 2 25 2019 conda-meta drwxr-xr-x 3 apple staff 96 2 25 2019 doc drwxr-xr-x 9 apple staff 288 11 26 14:40 envs drwxr-xr-x 6 apple staff 192 2 25 2019 etc drwxr-xr-x 305 apple staff 9760 5 17 2019 include drwxr-xr-x 732 apple staff 23424 2 25 2019 lib drwxr-xr-x 5 apple staff 160 2 25 2019 libexec drwxr-xr-x 3 apple staff 96 2 25 2019 man drwxr-xr-x 68 apple staff 2176 2 25 2019 mkspecs -rw-rw-r-- 1 apple staff 745 2 25 2019 org.freedesktop.dbus-session.plist drwxr-xr-x 15 apple staff 480 2 25 2019 phrasebooks drwxr-xr-x 1086 apple staff 34752 9 29 18:05 pkgs drwxr-xr-x 25 apple staff 800 2 25 2019 plugins drwxr-xr-x 3 apple staff 96 2 25 2019 python.app drwxr-xr-x 27 apple staff 864 2 25 2019 qml drwxr-xr-x 7 apple staff 224 2 25 2019 resources drwxr-xr-x 14 apple staff 448 2 25 2019 sbin drwxr-xr-x 25 apple staff 800 2 25 2019 share drwxr-xr-x 9 apple staff 288 2 25 2019 ssl drwxr-xr-x 290 apple staff 9280 2 25 2019 translations
在 bin目錄下,存在一個Designer.app應用是PyQt的Designer設計器。文件的擴展名是.ui。
因為Conda安裝之后,默認是base環境,所以可以使用Coda命令創建新的開發環境:
conda create -n gui python=python3.7
激活環境
conda activate gui
安裝pyqt5
(gui) > pip install pyqt5==5.10
如果安裝的PyQt5版本高于5.10,部分庫將要單獨安裝,如WebEngine
(gui) > pip install PyQtWebEngine
PyQt5+Socket實現中心化網絡服務
服務器端(完整代碼)
import json import socket import threading import time from data import DataSource class ClientThread(threading.Thread): def __init__(self, client, addr): super(ClientThread, self).__init__() self.client = client self.addr = addr self.login_user = None print('{} 連接成功!'.format(addr)) self.client.send(b'OK 200') def run(self) -> None: while True: b_msg = self.client.recv(1024*8) # 等待接收客戶端信息 if b_msg == b'exit': break # 解析命令 try: self.parse_cmd(b_msg.decode('utf-8')) except: self.client.send(b'Error') self.client.send(b'closing') print('{} 斷開連接'.format(self.addr)) def parse_cmd(self, cmd): print('接收命令-----', cmd) if cmd.startswith('login'): # login username pwd _, name, pwd = cmd.split() for item in datas: if item['name'] == name and item['pwd'] == pwd: self.login_user = item if self.login_user is not None: self.client.send(b'OK 200') else: self.client.send(b'not exist user') else: if self.login_user is None: self.client.send(b'no login session') elif cmd.startswith('add'): # add 100 blance = float(cmd.split()[-1]) self.login_user['blance'] += blance self.client.send(b'OK 200') elif cmd.startswith('sub'): # sub 100 blance = float(cmd.split()[-1]) self.login_user['blance'] -= blance self.client.send(b'OK 200') elif cmd.startswith('get'): self.client.send(json.dumps(self.login_user, ensure_ascii=False).encode('utf-8')) if __name__ == '__main__': datas = DataSource().load() # 創建socket應用服務 server = socket.socket() server.bind(('localhost', 18900)) # 綁定主機IP和Host server.listen() print('中心服務已啟動n等待客戶端連接...') while True: client, addr = server.accept() ClientThread(client, addr).start() time.sleep(0.5)
客戶端(完整代碼)
import socket import threading class CenterClient(): def __init__(self, server, port): super().__init__() self.server = server self.port = port self.isConnected = False self.client = None def connect(self): self.client = socket.socket() self.client.connect((self.server, self.port)) msg = self.client.recv(8*1024) if msg == b'OK 200': print('---連接成功--') self.isConnected = True else: print('---連接失敗---') self.isConnected = False def send_cmd(self, cmd): self.client.send(cmd.encode('utf-8')) data = self.client.recv(8*1024) print('{}命令結果: {}'.format(cmd, data)) if data == b'Error': return '400' return data.decode('utf-8') if __name__ == '__main__': client = CenterClient('localhost', 18900) client.connect() print(client.send_cmd('login disen disen123')) # print(client.send_cmd('add 1000')) # print(client.send_cmd('sub 50')) print(client.send_cmd('get'))
最后
今天的分享到這里就結束了 ,感興趣的朋友也可以去試試哈
覺得我分享的文章不錯的話,給文章點贊(/≧▽≦)/