---
# System prepended metadata

title: PyQt5Designer
tags: [python]

---

# PyQt5Designer

1. 開啟 PyQt5Designer，Tools > External Tools > PyQt5Designer
2. 選擇 Widget 並按「建立」
3. 拉3個Label、3個Line Edit、4個Push Button、1個Text Browser，如下圖

![](https://i.imgur.com/RdXWKxm.png)

4. 修改物件顯示的名稱，如下圖

![](https://i.imgur.com/sBp0Mpi.png)

5. 修改物件的objectName，方便後續編輯，如WebURL、username、password、pingButton、gowebButton、loginButton、logoutButton

![](https://i.imgur.com/sBnD4K0.png)

6. 存檔為 02.ui ，在PyCharm中就可以看到 02.ui 這個檔案

![](https://i.imgur.com/FYwV8oK.png)

7. 在 02.ui 上按右鍵，選擇 External Tools > PyUIC 將ui檔轉為py檔。

![](https://i.imgur.com/y0Jo6PS.png)

![](https://i.imgur.com/dMNb28f.png)

8. 在 02.py 檔最後加入以下程式碼

```
if __name__ == '__main__':
    #固定的，PyQT5程序都需要QApplication對象。sys.argv是命令行參數列表，確保程序可以雙擊運行。
    app = QtWidgets.QApplication(sys.argv)
    #初始化
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_Form()
    ui.setupUi(MainWindow)
    #將窗口控件顯示在螢幕上
    MainWindow.show()
    # 增加
    print("程式開始!")
    #程序運行，sys.exit 方法確保程序完整退出。
    sys.exit(app.exec_())
```

9. 在最上方加入 import sys

![](https://i.imgur.com/rgxDEul.png)

![](https://i.imgur.com/GnUuZ6X.png)

![](https://i.imgur.com/rWa48FK.png)

10. Run > Run 就可以看到成果了

![](https://i.imgur.com/hkV2gmR.png)

11. 同樣的方式，在檔案上方加入

![](https://i.imgur.com/vZrRZbJ.png)

12. Run 之後就成功了。


```
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '02.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
import subprocess
import sys
import webbrowser

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(542, 295)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(20, 40, 47, 12))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(20, 160, 47, 12))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(20, 200, 47, 12))
        self.label_3.setObjectName("label_3")
        self.WebURL = QtWidgets.QLineEdit(Form)
        self.WebURL.setGeometry(QtCore.QRect(80, 30, 113, 20))
        self.WebURL.setObjectName("WebURL")
        self.username = QtWidgets.QLineEdit(Form)
        self.username.setGeometry(QtCore.QRect(80, 150, 113, 20))
        self.username.setObjectName("username")
        self.password = QtWidgets.QLineEdit(Form)
        self.password.setGeometry(QtCore.QRect(80, 200, 113, 20))
        self.password.setObjectName("password")
        self.pingButton = QtWidgets.QPushButton(Form)
        self.pingButton.setGeometry(QtCore.QRect(80, 70, 75, 23))
        self.pingButton.setObjectName("pingButton")
        # 增加
        self.pingButton.clicked.connect(self.aaa)

        self.gowebButton = QtWidgets.QPushButton(Form)
        self.gowebButton.setGeometry(QtCore.QRect(170, 70, 75, 23))
        self.gowebButton.setObjectName("gowebButton")
        #增加
        self.gowebButton.clicked.connect(self.bbb)

        self.loginButton = QtWidgets.QPushButton(Form)
        self.loginButton.setGeometry(QtCore.QRect(80, 240, 75, 23))
        self.loginButton.setObjectName("loginButton")
        # 增加
        self.loginButton.clicked.connect(self.ccc)

        self.logoutButton = QtWidgets.QPushButton(Form)
        self.logoutButton.setGeometry(QtCore.QRect(160, 240, 75, 23))
        self.logoutButton.setObjectName("logoutButton")
        # 增加
        self.logoutButton.clicked.connect(self.ddd)

        self.textBrowser = QtWidgets.QTextBrowser(Form)
        self.textBrowser.setGeometry(QtCore.QRect(260, 20, 256, 192))
        self.textBrowser.setObjectName("textBrowser")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "網址"))
        self.label_2.setText(_translate("Form", "帳號"))
        self.label_3.setText(_translate("Form", "密碼"))
        self.pingButton.setText(_translate("Form", "Ping"))
        self.gowebButton.setText(_translate("Form", "前往網站"))
        self.loginButton.setText(_translate("Form", "登入網芳"))
        self.logoutButton.setText(_translate("Form", "登出網芳"))

    # 增加
    def aaa(self, MainWindow):
        print('ping')
        r = subprocess.run(["ping", self.WebURL.text()], stdout=subprocess.PIPE)
        if (r.returncode == 0):
            self.textBrowser.setText(r.stdout.decode('cp950'))
        else:
            self.textBrowser.setText("網址出錯了!")

    # 增加
    def bbb(self, MainWindow):
        print('openweb')
        webbrowser.open_new(self.WebURL.text())

    # 增加
    def ccc(self, MainWindow):
        print('login')
        username = self.username.text()
        password = self.password.text()
        subprocess.run('net use k: \\\\120.125.118.3\home /user:' + username + ' ' + password)
        self.textBrowser.setText('net use k: \\\\120.125.118.3\home /user:' + username + ' ' + password)

    def ddd(self, MainWindow):
        print('logout')
        subprocess.run('net use * /d /YES')
        self.textBrowser.setText('中斷連接!')

if __name__ == '__main__':
    #固定的，PyQT5程序都需要QApplication對象。sys.argv是命令行參數列表，確保程序可以雙擊運行。
    app = QtWidgets.QApplication(sys.argv)
    #初始化
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_Form()
    ui.setupUi(MainWindow)
    #將窗口控件顯示在螢幕上
    MainWindow.show()
    # 增加
    print("程式開始!")
    #程序運行，sys.exit 方法確保程序完整退出。
    sys.exit(app.exec_())

```

13. 進行封裝為exe

![](https://i.imgur.com/ASXX6xb.png)

14. 完成。

###### tags: `python`