Skip to content

[Python] 我無聊使用了 PyQt5 製作一個簡單的魔物獵人世界: Iceborne 屬性表

Last Updated on 2021-04-10 by Clay

最近幾天 Monster Hunter World: Iceborne 發售了(其實已經發售一段時間了),最近沉浸在獵龍的樂趣裡頭,真的是什麼都不想管了,哈哈哈哈。

如果你想看我當初遊玩心得的話,傳送門在這裡: 魔物獵人《世界》Iceborne —— 發售首日至今心得

本來一直想要做個魔物獵人配裝器的,但是苦於網路上目前沒有相關的資料(聽說有日文的),我一直猶豫不決要不要自己動手整理資料。(因為這真的很累 XDDD)

昨天,察覺到一直玩的朋友會慢慢找 WIKI 上的魔物弱點屬性,我就開始想:何必這麼辛苦了?一直切網頁很慢欸。

於是,我自己隨便地做出了個魔物弱點查詢的 GUI,雖然很醜,但是我個人認為比切換 WIKI 頁面快了很多,也暫時得到了朋友的認證 XDDD

那麼,我想我今天就來分享怎麼製作魔物獵人的弱點屬性查詢吧!最終目標還是做出魔物獵人的配裝器,不過可能就等我有空再說吧~

事先聲明,我最終做出來的弱點查詢其實很醜:

點擊圖示之後,會跳出新的視窗,我用的是網路上找到的魔物圖鑑:(之後我會全部換成自己的高清版,這裡只是示範。)

這裡我們拿碎龍來示範,效果如上。

那麼,以下開始介紹我怎麼製作這個簡單的魔物弱點屬性查詢。

如果你想學習 Python,那也許可以參考我之前寫過的系列:Python 基本教學

如果你想學習怎麼使用 Python 開發簡單的 GUI 界面,也許可以參考這裡:PyQt5 基本教學


製作界面

首先,我使用 Qt Designer 創造了一個這樣的界面,主要是使用 QLabel 來顯示魔物名字、使用 QPushButton 來安排不同怪物的弱點屬性顯示。

Code 如下,(將 Qt Designer 輸出的 .ui 檔使用 PyUIC 轉成 Python 檔),事先聲明,因為我預設拉了很多元件,所以可能會有點冗長,基本上可以跳過不看沒有關係。只需要知道我裡頭的元件有 QLabel 以及 QPushButton 共 53 組。

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

# Form implementation generated from reading ui file 'MonsterWeakness.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(919, 890)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(10, 0, 91, 51))
        self.label.setText("")
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 50, 91, 81))
        self.pushButton.setText("")
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(100, 50, 91, 81))
        self.pushButton_2.setText("")
        self.pushButton_2.setObjectName("pushButton_2")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(100, 0, 91, 51))
        self.label_2.setText("")
        self.label_2.setObjectName("label_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(190, 50, 91, 81))
        self.pushButton_3.setText("")
        self.pushButton_3.setObjectName("pushButton_3")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(190, 0, 91, 51))
        self.label_3.setText("")
        self.label_3.setObjectName("label_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(280, 50, 91, 81))
        self.pushButton_4.setText("")
        self.pushButton_4.setObjectName("pushButton_4")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(280, 0, 91, 51))
        self.label_4.setText("")
        self.label_4.setObjectName("label_4")
        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(370, 50, 91, 81))
        self.pushButton_5.setText("")
        self.pushButton_5.setObjectName("pushButton_5")
        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(370, 0, 91, 51))
        self.label_5.setText("")
        self.label_5.setObjectName("label_5")
        self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_6.setGeometry(QtCore.QRect(460, 50, 91, 81))
        self.pushButton_6.setText("")
        self.pushButton_6.setObjectName("pushButton_6")
        self.label_6 = QtWidgets.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(460, 0, 91, 51))
        self.label_6.setText("")
        self.label_6.setObjectName("label_6")
        self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_7.setGeometry(QtCore.QRect(550, 50, 91, 81))
        self.pushButton_7.setText("")
        self.pushButton_7.setObjectName("pushButton_7")
        self.label_7 = QtWidgets.QLabel(self.centralwidget)
        self.label_7.setGeometry(QtCore.QRect(550, 0, 91, 51))
        self.label_7.setText("")
        self.label_7.setObjectName("label_7")
        self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_8.setGeometry(QtCore.QRect(640, 50, 91, 81))
        self.pushButton_8.setText("")
        self.pushButton_8.setObjectName("pushButton_8")
        self.label_8 = QtWidgets.QLabel(self.centralwidget)
        self.label_8.setGeometry(QtCore.QRect(640, 0, 91, 51))
        self.label_8.setText("")
        self.label_8.setObjectName("label_8")
        self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_9.setGeometry(QtCore.QRect(730, 50, 91, 81))
        self.pushButton_9.setText("")
        self.pushButton_9.setObjectName("pushButton_9")
        self.label_9 = QtWidgets.QLabel(self.centralwidget)
        self.label_9.setGeometry(QtCore.QRect(730, 0, 91, 51))
        self.label_9.setText("")
        self.label_9.setObjectName("label_9")
        self.pushButton_10 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_10.setGeometry(QtCore.QRect(820, 50, 91, 81))
        self.pushButton_10.setText("")
        self.pushButton_10.setObjectName("pushButton_10")
        self.label_10 = QtWidgets.QLabel(self.centralwidget)
        self.label_10.setGeometry(QtCore.QRect(820, 0, 91, 51))
        self.label_10.setText("")
        self.label_10.setObjectName("label_10")
        self.pushButton_11 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_11.setGeometry(QtCore.QRect(10, 190, 91, 81))
        self.pushButton_11.setText("")
        self.pushButton_11.setObjectName("pushButton_11")
        self.label_11 = QtWidgets.QLabel(self.centralwidget)
        self.label_11.setGeometry(QtCore.QRect(10, 140, 91, 51))
        self.label_11.setText("")
        self.label_11.setObjectName("label_11")
        self.pushButton_12 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_12.setGeometry(QtCore.QRect(100, 190, 91, 81))
        self.pushButton_12.setText("")
        self.pushButton_12.setObjectName("pushButton_12")
        self.label_12 = QtWidgets.QLabel(self.centralwidget)
        self.label_12.setGeometry(QtCore.QRect(100, 140, 91, 51))
        self.label_12.setText("")
        self.label_12.setObjectName("label_12")
        self.pushButton_13 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_13.setGeometry(QtCore.QRect(190, 190, 91, 81))
        self.pushButton_13.setText("")
        self.pushButton_13.setObjectName("pushButton_13")
        self.pushButton_14 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_14.setGeometry(QtCore.QRect(280, 190, 91, 81))
        self.pushButton_14.setText("")
        self.pushButton_14.setObjectName("pushButton_14")
        self.pushButton_15 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_15.setGeometry(QtCore.QRect(370, 190, 91, 81))
        self.pushButton_15.setText("")
        self.pushButton_15.setObjectName("pushButton_15")
        self.label_13 = QtWidgets.QLabel(self.centralwidget)
        self.label_13.setGeometry(QtCore.QRect(190, 140, 91, 51))
        self.label_13.setText("")
        self.label_13.setObjectName("label_13")
        self.label_14 = QtWidgets.QLabel(self.centralwidget)
        self.label_14.setGeometry(QtCore.QRect(280, 140, 91, 51))
        self.label_14.setText("")
        self.label_14.setObjectName("label_14")
        self.pushButton_16 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_16.setGeometry(QtCore.QRect(460, 190, 91, 81))
        self.pushButton_16.setText("")
        self.pushButton_16.setObjectName("pushButton_16")
        self.pushButton_17 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_17.setGeometry(QtCore.QRect(550, 190, 91, 81))
        self.pushButton_17.setText("")
        self.pushButton_17.setObjectName("pushButton_17")
        self.label_15 = QtWidgets.QLabel(self.centralwidget)
        self.label_15.setGeometry(QtCore.QRect(370, 140, 91, 51))
        self.label_15.setText("")
        self.label_15.setObjectName("label_15")
        self.label_16 = QtWidgets.QLabel(self.centralwidget)
        self.label_16.setGeometry(QtCore.QRect(460, 140, 91, 51))
        self.label_16.setText("")
        self.label_16.setObjectName("label_16")
        self.pushButton_18 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_18.setGeometry(QtCore.QRect(640, 190, 91, 81))
        self.pushButton_18.setText("")
        self.pushButton_18.setObjectName("pushButton_18")
        self.pushButton_19 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_19.setGeometry(QtCore.QRect(730, 190, 91, 81))
        self.pushButton_19.setText("")
        self.pushButton_19.setObjectName("pushButton_19")
        self.label_17 = QtWidgets.QLabel(self.centralwidget)
        self.label_17.setGeometry(QtCore.QRect(550, 140, 91, 51))
        self.label_17.setText("")
        self.label_17.setObjectName("label_17")
        self.label_18 = QtWidgets.QLabel(self.centralwidget)
        self.label_18.setGeometry(QtCore.QRect(640, 140, 91, 51))
        self.label_18.setText("")
        self.label_18.setObjectName("label_18")
        self.label_19 = QtWidgets.QLabel(self.centralwidget)
        self.label_19.setGeometry(QtCore.QRect(730, 140, 91, 51))
        self.label_19.setText("")
        self.label_19.setObjectName("label_19")
        self.pushButton_20 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_20.setGeometry(QtCore.QRect(820, 190, 91, 81))
        self.pushButton_20.setText("")
        self.pushButton_20.setObjectName("pushButton_20")
        self.label_20 = QtWidgets.QLabel(self.centralwidget)
        self.label_20.setGeometry(QtCore.QRect(820, 140, 91, 51))
        self.label_20.setText("")
        self.label_20.setObjectName("label_20")
        self.pushButton_21 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_21.setGeometry(QtCore.QRect(10, 330, 91, 81))
        self.pushButton_21.setText("")
        self.pushButton_21.setObjectName("pushButton_21")
        self.label_21 = QtWidgets.QLabel(self.centralwidget)
        self.label_21.setGeometry(QtCore.QRect(10, 280, 91, 51))
        self.label_21.setText("")
        self.label_21.setObjectName("label_21")
        self.pushButton_22 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_22.setGeometry(QtCore.QRect(100, 330, 91, 81))
        self.pushButton_22.setText("")
        self.pushButton_22.setObjectName("pushButton_22")
        self.label_22 = QtWidgets.QLabel(self.centralwidget)
        self.label_22.setGeometry(QtCore.QRect(100, 280, 91, 51))
        self.label_22.setText("")
        self.label_22.setObjectName("label_22")
        self.pushButton_23 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_23.setGeometry(QtCore.QRect(190, 330, 91, 81))
        self.pushButton_23.setText("")
        self.pushButton_23.setObjectName("pushButton_23")
        self.pushButton_24 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_24.setGeometry(QtCore.QRect(280, 330, 91, 81))
        self.pushButton_24.setText("")
        self.pushButton_24.setObjectName("pushButton_24")
        self.pushButton_25 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_25.setGeometry(QtCore.QRect(370, 330, 91, 81))
        self.pushButton_25.setText("")
        self.pushButton_25.setObjectName("pushButton_25")
        self.label_23 = QtWidgets.QLabel(self.centralwidget)
        self.label_23.setGeometry(QtCore.QRect(190, 280, 91, 51))
        self.label_23.setText("")
        self.label_23.setObjectName("label_23")
        self.label_24 = QtWidgets.QLabel(self.centralwidget)
        self.label_24.setGeometry(QtCore.QRect(280, 280, 91, 51))
        self.label_24.setText("")
        self.label_24.setObjectName("label_24")
        self.pushButton_26 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_26.setGeometry(QtCore.QRect(460, 330, 91, 81))
        self.pushButton_26.setText("")
        self.pushButton_26.setObjectName("pushButton_26")
        self.pushButton_27 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_27.setGeometry(QtCore.QRect(550, 330, 91, 81))
        self.pushButton_27.setText("")
        self.pushButton_27.setObjectName("pushButton_27")
        self.label_25 = QtWidgets.QLabel(self.centralwidget)
        self.label_25.setGeometry(QtCore.QRect(370, 280, 91, 51))
        self.label_25.setText("")
        self.label_25.setObjectName("label_25")
        self.label_26 = QtWidgets.QLabel(self.centralwidget)
        self.label_26.setGeometry(QtCore.QRect(460, 280, 91, 51))
        self.label_26.setText("")
        self.label_26.setObjectName("label_26")
        self.pushButton_28 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_28.setGeometry(QtCore.QRect(640, 330, 91, 81))
        self.pushButton_28.setText("")
        self.pushButton_28.setObjectName("pushButton_28")
        self.pushButton_29 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_29.setGeometry(QtCore.QRect(730, 330, 91, 81))
        self.pushButton_29.setText("")
        self.pushButton_29.setObjectName("pushButton_29")
        self.label_27 = QtWidgets.QLabel(self.centralwidget)
        self.label_27.setGeometry(QtCore.QRect(550, 280, 91, 51))
        self.label_27.setText("")
        self.label_27.setObjectName("label_27")
        self.label_28 = QtWidgets.QLabel(self.centralwidget)
        self.label_28.setGeometry(QtCore.QRect(640, 280, 91, 51))
        self.label_28.setText("")
        self.label_28.setObjectName("label_28")
        self.label_29 = QtWidgets.QLabel(self.centralwidget)
        self.label_29.setGeometry(QtCore.QRect(730, 280, 91, 51))
        self.label_29.setText("")
        self.label_29.setObjectName("label_29")
        self.pushButton_30 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_30.setGeometry(QtCore.QRect(820, 330, 91, 81))
        self.pushButton_30.setText("")
        self.pushButton_30.setObjectName("pushButton_30")
        self.label_30 = QtWidgets.QLabel(self.centralwidget)
        self.label_30.setGeometry(QtCore.QRect(820, 280, 91, 51))
        self.label_30.setText("")
        self.label_30.setObjectName("label_30")
        self.pushButton_31 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_31.setGeometry(QtCore.QRect(10, 470, 91, 81))
        self.pushButton_31.setText("")
        self.pushButton_31.setObjectName("pushButton_31")
        self.label_31 = QtWidgets.QLabel(self.centralwidget)
        self.label_31.setGeometry(QtCore.QRect(10, 420, 91, 51))
        self.label_31.setText("")
        self.label_31.setObjectName("label_31")
        self.pushButton_32 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_32.setGeometry(QtCore.QRect(100, 470, 91, 81))
        self.pushButton_32.setText("")
        self.pushButton_32.setObjectName("pushButton_32")
        self.label_32 = QtWidgets.QLabel(self.centralwidget)
        self.label_32.setGeometry(QtCore.QRect(100, 420, 91, 51))
        self.label_32.setText("")
        self.label_32.setObjectName("label_32")
        self.pushButton_33 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_33.setGeometry(QtCore.QRect(190, 470, 91, 81))
        self.pushButton_33.setText("")
        self.pushButton_33.setObjectName("pushButton_33")
        self.pushButton_34 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_34.setGeometry(QtCore.QRect(280, 470, 91, 81))
        self.pushButton_34.setText("")
        self.pushButton_34.setObjectName("pushButton_34")
        self.pushButton_35 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_35.setGeometry(QtCore.QRect(370, 470, 91, 81))
        self.pushButton_35.setText("")
        self.pushButton_35.setObjectName("pushButton_35")
        self.label_33 = QtWidgets.QLabel(self.centralwidget)
        self.label_33.setGeometry(QtCore.QRect(190, 420, 91, 51))
        self.label_33.setText("")
        self.label_33.setObjectName("label_33")
        self.label_34 = QtWidgets.QLabel(self.centralwidget)
        self.label_34.setGeometry(QtCore.QRect(280, 420, 91, 51))
        self.label_34.setText("")
        self.label_34.setObjectName("label_34")
        self.pushButton_36 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_36.setGeometry(QtCore.QRect(460, 470, 91, 81))
        self.pushButton_36.setText("")
        self.pushButton_36.setObjectName("pushButton_36")
        self.pushButton_37 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_37.setGeometry(QtCore.QRect(550, 470, 91, 81))
        self.pushButton_37.setText("")
        self.pushButton_37.setObjectName("pushButton_37")
        self.label_35 = QtWidgets.QLabel(self.centralwidget)
        self.label_35.setGeometry(QtCore.QRect(370, 420, 91, 51))
        self.label_35.setText("")
        self.label_35.setObjectName("label_35")
        self.label_36 = QtWidgets.QLabel(self.centralwidget)
        self.label_36.setGeometry(QtCore.QRect(460, 420, 91, 51))
        self.label_36.setText("")
        self.label_36.setObjectName("label_36")
        self.pushButton_38 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_38.setGeometry(QtCore.QRect(640, 470, 91, 81))
        self.pushButton_38.setText("")
        self.pushButton_38.setObjectName("pushButton_38")
        self.pushButton_39 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_39.setGeometry(QtCore.QRect(730, 470, 91, 81))
        self.pushButton_39.setText("")
        self.pushButton_39.setObjectName("pushButton_39")
        self.label_37 = QtWidgets.QLabel(self.centralwidget)
        self.label_37.setGeometry(QtCore.QRect(550, 420, 91, 51))
        self.label_37.setText("")
        self.label_37.setObjectName("label_37")
        self.label_38 = QtWidgets.QLabel(self.centralwidget)
        self.label_38.setGeometry(QtCore.QRect(640, 420, 91, 51))
        self.label_38.setText("")
        self.label_38.setObjectName("label_38")
        self.label_39 = QtWidgets.QLabel(self.centralwidget)
        self.label_39.setGeometry(QtCore.QRect(730, 420, 91, 51))
        self.label_39.setText("")
        self.label_39.setObjectName("label_39")
        self.pushButton_40 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_40.setGeometry(QtCore.QRect(820, 470, 91, 81))
        self.pushButton_40.setText("")
        self.pushButton_40.setObjectName("pushButton_40")
        self.label_40 = QtWidgets.QLabel(self.centralwidget)
        self.label_40.setGeometry(QtCore.QRect(820, 420, 91, 51))
        self.label_40.setText("")
        self.label_40.setObjectName("label_40")
        self.pushButton_41 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_41.setGeometry(QtCore.QRect(10, 610, 91, 81))
        self.pushButton_41.setText("")
        self.pushButton_41.setObjectName("pushButton_41")
        self.label_41 = QtWidgets.QLabel(self.centralwidget)
        self.label_41.setGeometry(QtCore.QRect(10, 560, 91, 51))
        self.label_41.setText("")
        self.label_41.setObjectName("label_41")
        self.pushButton_42 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_42.setGeometry(QtCore.QRect(100, 610, 91, 81))
        self.pushButton_42.setText("")
        self.pushButton_42.setObjectName("pushButton_42")
        self.label_42 = QtWidgets.QLabel(self.centralwidget)
        self.label_42.setGeometry(QtCore.QRect(100, 560, 91, 51))
        self.label_42.setText("")
        self.label_42.setObjectName("label_42")
        self.pushButton_43 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_43.setGeometry(QtCore.QRect(190, 610, 91, 81))
        self.pushButton_43.setText("")
        self.pushButton_43.setObjectName("pushButton_43")
        self.pushButton_44 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_44.setGeometry(QtCore.QRect(280, 610, 91, 81))
        self.pushButton_44.setText("")
        self.pushButton_44.setObjectName("pushButton_44")
        self.pushButton_45 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_45.setGeometry(QtCore.QRect(370, 610, 91, 81))
        self.pushButton_45.setText("")
        self.pushButton_45.setObjectName("pushButton_45")
        self.label_43 = QtWidgets.QLabel(self.centralwidget)
        self.label_43.setGeometry(QtCore.QRect(190, 560, 91, 51))
        self.label_43.setText("")
        self.label_43.setObjectName("label_43")
        self.label_44 = QtWidgets.QLabel(self.centralwidget)
        self.label_44.setGeometry(QtCore.QRect(280, 560, 91, 51))
        self.label_44.setText("")
        self.label_44.setObjectName("label_44")
        self.pushButton_46 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_46.setGeometry(QtCore.QRect(460, 610, 91, 81))
        self.pushButton_46.setText("")
        self.pushButton_46.setObjectName("pushButton_46")
        self.pushButton_47 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_47.setGeometry(QtCore.QRect(550, 610, 91, 81))
        self.pushButton_47.setText("")
        self.pushButton_47.setObjectName("pushButton_47")
        self.label_45 = QtWidgets.QLabel(self.centralwidget)
        self.label_45.setGeometry(QtCore.QRect(370, 560, 91, 51))
        self.label_45.setText("")
        self.label_45.setObjectName("label_45")
        self.label_46 = QtWidgets.QLabel(self.centralwidget)
        self.label_46.setGeometry(QtCore.QRect(460, 560, 91, 51))
        self.label_46.setText("")
        self.label_46.setObjectName("label_46")
        self.pushButton_48 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_48.setGeometry(QtCore.QRect(640, 610, 91, 81))
        self.pushButton_48.setText("")
        self.pushButton_48.setObjectName("pushButton_48")
        self.pushButton_49 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_49.setGeometry(QtCore.QRect(730, 610, 91, 81))
        self.pushButton_49.setText("")
        self.pushButton_49.setObjectName("pushButton_49")
        self.label_47 = QtWidgets.QLabel(self.centralwidget)
        self.label_47.setGeometry(QtCore.QRect(550, 560, 91, 51))
        self.label_47.setText("")
        self.label_47.setObjectName("label_47")
        self.label_48 = QtWidgets.QLabel(self.centralwidget)
        self.label_48.setGeometry(QtCore.QRect(640, 560, 91, 51))
        self.label_48.setText("")
        self.label_48.setObjectName("label_48")
        self.label_49 = QtWidgets.QLabel(self.centralwidget)
        self.label_49.setGeometry(QtCore.QRect(730, 560, 91, 51))
        self.label_49.setText("")
        self.label_49.setObjectName("label_49")
        self.pushButton_50 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_50.setGeometry(QtCore.QRect(820, 610, 91, 81))
        self.pushButton_50.setText("")
        self.pushButton_50.setObjectName("pushButton_50")
        self.label_50 = QtWidgets.QLabel(self.centralwidget)
        self.label_50.setGeometry(QtCore.QRect(820, 560, 91, 51))
        self.label_50.setText("")
        self.label_50.setObjectName("label_50")
        self.pushButton_51 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_51.setGeometry(QtCore.QRect(10, 750, 91, 81))
        self.pushButton_51.setText("")
        self.pushButton_51.setObjectName("pushButton_51")
        self.pushButton_52 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_52.setGeometry(QtCore.QRect(100, 750, 91, 81))
        self.pushButton_52.setText("")
        self.pushButton_52.setObjectName("pushButton_52")
        self.pushButton_53 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_53.setGeometry(QtCore.QRect(190, 750, 91, 81))
        self.pushButton_53.setText("")
        self.pushButton_53.setObjectName("pushButton_53")
        self.label_51 = QtWidgets.QLabel(self.centralwidget)
        self.label_51.setGeometry(QtCore.QRect(10, 700, 91, 51))
        self.label_51.setText("")
        self.label_51.setObjectName("label_51")
        self.label_52 = QtWidgets.QLabel(self.centralwidget)
        self.label_52.setGeometry(QtCore.QRect(100, 700, 91, 51))
        self.label_52.setText("")
        self.label_52.setObjectName("label_52")
        self.label_53 = QtWidgets.QLabel(self.centralwidget)
        self.label_53.setGeometry(QtCore.QRect(190, 700, 91, 51))
        self.label_53.setText("")
        self.label_53.setObjectName("label_53")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 919, 25))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


另外,我額外寫了一個界面,用於顯示魔物資訊的圖片。

Code 如下:

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

# Form implementation generated from reading ui file 'subMonsterWeakness.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow_sub(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(0, 0, 801, 611))
        self.label.setText("")
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        # self.statusbar = QtWidgets.QStatusBar(MainWindow)
        # self.statusbar.setObjectName("statusbar")
        # MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


基本上這個子界面我只擺了一個 QLabel,其大小幾乎與視窗等大,另外可以看到這幾行。

# self.statusbar = QtWidgets.QStatusBar(MainWindow)
# self.statusbar.setObjectName("statusbar")
# MainWindow.setStatusBar(self.statusbar)

這三行基本上把底下的狀態欄去掉了,否則我顯示的圖片底下會有白色的狀態欄,看起來很突兀。

然後,各位若有看過我的心得筆記,應該還記得怎麼定義 QPushButton, QLabel。

我們將界面與程式功能的部份分離,另外寫一份檔案繼承界面檔。當然,兩份界面的 Python 檔都要繼承。

這份就是我們最終產生界面的 Python 檔了,同樣很長,底下我再說明我怎麼產生這份檔案(是的,我大部分的時候不是自己寫的,而是透過程式產生。)


QLabel

# -*- coding: utf-8 -*-
import PyQt5
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from MonsterWeakness import Ui_MainWindow
from subMonsterWeakness import Ui_MainWindow_sub
import sys


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # LOGO
        self.setWindowIcon(QIcon('MHWI_LOGO.png'))

        # Label
        self.ui.label.setText("冥燈龍")
        self.ui.label.setAlignment(Qt.AlignCenter)
        self.ui.label_2.setText("冰咒龍")
        self.ui.label_2.setAlignment(Qt.AlignCenter)
        self.ui.label_3.setText("冰牙龍")
        self.ui.label_3.setAlignment(Qt.AlignCenter)
        self.ui.label_4.setText("冰魚龍")
        self.ui.label_4.setAlignment(Qt.AlignCenter)
        self.ui.label_5.setText("凶爪龍")
        self.ui.label_5.setAlignment(Qt.AlignCenter)
        self.ui.label_6.setText("古代鹿首精")
        self.ui.label_6.setAlignment(Qt.AlignCenter)
        self.ui.label_7.setText("土砂龍")
        self.ui.label_7.setAlignment(Qt.AlignCenter)
        self.ui.label_8.setText("大凶豺龍")
        self.ui.label_8.setAlignment(Qt.AlignCenter)
        self.ui.label_9.setText("大凶顎龍")
        self.ui.label_9.setAlignment(Qt.AlignCenter)
        self.ui.label_10.setText("天地煌啼龍")
        self.ui.label_10.setAlignment(Qt.AlignCenter)
        self.ui.label_11.setText("屍套龍")
        self.ui.label_11.setAlignment(Qt.AlignCenter)
        self.ui.label_12.setText("岩賊龍")
        self.ui.label_12.setAlignment(Qt.AlignCenter)
        self.ui.label_13.setText("恐暴龍")
        self.ui.label_13.setAlignment(Qt.AlignCenter)
        self.ui.label_14.setText("慘爪龍")
        self.ui.label_14.setAlignment(Qt.AlignCenter)
        self.ui.label_15.setText("斬龍")
        self.ui.label_15.setAlignment(Qt.AlignCenter)
        self.ui.label_16.setText("櫻火龍")
        self.ui.label_16.setAlignment(Qt.AlignCenter)
        self.ui.label_17.setText("殲世滅盡龍")
        self.ui.label_17.setAlignment(Qt.AlignCenter)
        self.ui.label_18.setText("毒妖鳥")
        self.ui.label_18.setAlignment(Qt.AlignCenter)
        self.ui.label_19.setText("水妖鳥")
        self.ui.label_19.setAlignment(Qt.AlignCenter)
        self.ui.label_20.setText("泥魚龍")
        self.ui.label_20.setAlignment(Qt.AlignCenter)
        self.ui.label_21.setText("浮眠龍")
        self.ui.label_21.setAlignment(Qt.AlignCenter)
        self.ui.label_22.setText("浮空龍")
        self.ui.label_22.setAlignment(Qt.AlignCenter)
        self.ui.label_23.setText("滅盡龍")
        self.ui.label_23.setAlignment(Qt.AlignCenter)
        self.ui.label_24.setText("炎妃龍")
        self.ui.label_24.setAlignment(Qt.AlignCenter)
        self.ui.label_25.setText("炎王龍")
        self.ui.label_25.setAlignment(Qt.AlignCenter)
        self.ui.label_26.setText("熔山龍")
        self.ui.label_26.setAlignment(Qt.AlignCenter)
        self.ui.label_27.setText("熔岩龍")
        self.ui.label_27.setAlignment(Qt.AlignCenter)
        self.ui.label_28.setText("爆錘龍")
        self.ui.label_28.setAlignment(Qt.AlignCenter)
        self.ui.label_29.setText("爆鱗龍")
        self.ui.label_29.setAlignment(Qt.AlignCenter)
        self.ui.label_30.setText("猛牛龍")
        self.ui.label_30.setAlignment(Qt.AlignCenter)
        self.ui.label_31.setText("痹毒龍")
        self.ui.label_31.setAlignment(Qt.AlignCenter)
        self.ui.label_32.setText("眩鳥")
        self.ui.label_32.setAlignment(Qt.AlignCenter)
        self.ui.label_33.setText("硫斬龍")
        self.ui.label_33.setAlignment(Qt.AlignCenter)
        self.ui.label_34.setText("碎龍")
        self.ui.label_34.setAlignment(Qt.AlignCenter)
        self.ui.label_35.setText("絢輝龍")
        self.ui.label_35.setAlignment(Qt.AlignCenter)
        self.ui.label_36.setText("蒼火龍")
        self.ui.label_36.setAlignment(Qt.AlignCenter)
        self.ui.label_37.setText("蠻顎龍")
        self.ui.label_37.setAlignment(Qt.AlignCenter)
        self.ui.label_38.setText("角龍")
        self.ui.label_38.setAlignment(Qt.AlignCenter)
        self.ui.label_39.setText("貝希摩斯")
        self.ui.label_39.setAlignment(Qt.AlignCenter)
        self.ui.label_40.setText("轟龍")
        self.ui.label_40.setAlignment(Qt.AlignCenter)
        self.ui.label_41.setText("迅龍")
        self.ui.label_41.setAlignment(Qt.AlignCenter)
        self.ui.label_42.setText("鋼龍")
        self.ui.label_42.setAlignment(Qt.AlignCenter)
        self.ui.label_43.setText("雄火龍")
        self.ui.label_43.setAlignment(Qt.AlignCenter)
        self.ui.label_44.setText("雌火龍")
        self.ui.label_44.setAlignment(Qt.AlignCenter)
        self.ui.label_45.setText("雷顎龍")
        self.ui.label_45.setAlignment(Qt.AlignCenter)
        self.ui.label_46.setText("霜翼風漂龍")
        self.ui.label_46.setAlignment(Qt.AlignCenter)
        self.ui.label_47.setText("風漂龍")
        self.ui.label_47.setAlignment(Qt.AlignCenter)
        self.ui.label_48.setText("飛雷龍")
        self.ui.label_48.setAlignment(Qt.AlignCenter)
        self.ui.label_49.setText("骨錘龍")
        self.ui.label_49.setAlignment(Qt.AlignCenter)
        self.ui.label_50.setText("鹿首精")
        self.ui.label_50.setAlignment(Qt.AlignCenter)
        self.ui.label_51.setText("麒麟")
        self.ui.label_51.setAlignment(Qt.AlignCenter)
        self.ui.label_52.setText("黑狼鳥")
        self.ui.label_52.setAlignment(Qt.AlignCenter)
        self.ui.label_53.setText("黑角龍")
        self.ui.label_53.setAlignment(Qt.AlignCenter)


我先定義了所有 QLabel 該顯示的文字。


QPushButton

        # Button
        self.ui.pushButton.setIcon(QIcon('Icon/冥燈龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_2.setIcon(QIcon('Icon/冰咒龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_3.setIcon(QIcon('Icon/冰牙龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_4.setIcon(QIcon('Icon/冰魚龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_5.setIcon(QIcon('Icon/凶爪龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_6.setIcon(QIcon('Icon/古代鹿首精.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_7.setIcon(QIcon('Icon/土砂龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_8.setIcon(QIcon('Icon/大凶豺龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_9.setIcon(QIcon('Icon/大凶顎龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_10.setIcon(QIcon('Icon/天地煌啼龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_11.setIcon(QIcon('Icon/屍套龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_12.setIcon(QIcon('Icon/岩賊龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_13.setIcon(QIcon('Icon/恐暴龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_14.setIcon(QIcon('Icon/慘爪龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_15.setIcon(QIcon('Icon/斬龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_16.setIcon(QIcon('Icon/櫻火龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_17.setIcon(QIcon('Icon/殲世滅盡龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_18.setIcon(QIcon('Icon/毒妖鳥.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_19.setIcon(QIcon('Icon/水妖鳥.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_20.setIcon(QIcon('Icon/泥魚龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_21.setIcon(QIcon('Icon/浮眠龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_22.setIcon(QIcon('Icon/浮空龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_23.setIcon(QIcon('Icon/滅盡龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_24.setIcon(QIcon('Icon/炎妃龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_25.setIcon(QIcon('Icon/炎王龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_26.setIcon(QIcon('Icon/熔山龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_27.setIcon(QIcon('Icon/熔岩龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_28.setIcon(QIcon('Icon/爆錘龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_29.setIcon(QIcon('Icon/爆鱗龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_30.setIcon(QIcon('Icon/猛牛龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_31.setIcon(QIcon('Icon/痹毒龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_32.setIcon(QIcon('Icon/眩鳥.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_33.setIcon(QIcon('Icon/硫斬龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_34.setIcon(QIcon('Icon/碎龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_35.setIcon(QIcon('Icon/絢輝龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_36.setIcon(QIcon('Icon/蒼火龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_37.setIcon(QIcon('Icon/蠻顎龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_38.setIcon(QIcon('Icon/角龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_39.setIcon(QIcon('Icon/貝希摩斯.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_40.setIcon(QIcon('Icon/轟龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_41.setIcon(QIcon('Icon/迅龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_42.setIcon(QIcon('Icon/鋼龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_43.setIcon(QIcon('Icon/雄火龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_44.setIcon(QIcon('Icon/雌火龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_45.setIcon(QIcon('Icon/雷顎龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_46.setIcon(QIcon('Icon/霜翼風漂龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_47.setIcon(QIcon('Icon/風漂龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_48.setIcon(QIcon('Icon/飛雷龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_49.setIcon(QIcon('Icon/骨錘龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_50.setIcon(QIcon('Icon/鹿首精.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_51.setIcon(QIcon('Icon/麒麟.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_52.setIcon(QIcon('Icon/黑狼鳥.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))
        self.ui.pushButton_53.setIcon(QIcon('Icon/黑角龍.jpg'))
        self.ui.pushButton.setIconSize(QSize(100, 100))


第二部份則是定義所有 QPushButton 應該要有的圖片,這裡我是放入我在 Icon 這個資料夾底下的所有圖片。

看起來就是這個樣子了!


Button Events

緊接著,我們來定義每個按鈕觸發的事件。在這裡我是全部寫在一個 function 裡面,由傳給這個 function 的數值來決定到底顯示哪種魔物的弱點屬性。

        # Button Events
        self.ui.pushButton.clicked.connect(lambda: self.setIcon(1))
        self.ui.pushButton_2.clicked.connect(lambda: self.setIcon(2))
        self.ui.pushButton_3.clicked.connect(lambda: self.setIcon(3))
        self.ui.pushButton_4.clicked.connect(lambda: self.setIcon(4))
        self.ui.pushButton_5.clicked.connect(lambda: self.setIcon(5))
        self.ui.pushButton_6.clicked.connect(lambda: self.setIcon(6))
        self.ui.pushButton_7.clicked.connect(lambda: self.setIcon(7))
        self.ui.pushButton_8.clicked.connect(lambda: self.setIcon(8))
        self.ui.pushButton_9.clicked.connect(lambda: self.setIcon(9))
        self.ui.pushButton_10.clicked.connect(lambda: self.setIcon(10))
        self.ui.pushButton_11.clicked.connect(lambda: self.setIcon(11))
        self.ui.pushButton_12.clicked.connect(lambda: self.setIcon(12))
        self.ui.pushButton_13.clicked.connect(lambda: self.setIcon(13))
        self.ui.pushButton_14.clicked.connect(lambda: self.setIcon(14))
        self.ui.pushButton_15.clicked.connect(lambda: self.setIcon(15))
        self.ui.pushButton_16.clicked.connect(lambda: self.setIcon(16))
        self.ui.pushButton_17.clicked.connect(lambda: self.setIcon(17))
        self.ui.pushButton_18.clicked.connect(lambda: self.setIcon(18))
        self.ui.pushButton_19.clicked.connect(lambda: self.setIcon(19))
        self.ui.pushButton_20.clicked.connect(lambda: self.setIcon(20))
        self.ui.pushButton_21.clicked.connect(lambda: self.setIcon(21))
        self.ui.pushButton_22.clicked.connect(lambda: self.setIcon(22))
        self.ui.pushButton_23.clicked.connect(lambda: self.setIcon(23))
        self.ui.pushButton_24.clicked.connect(lambda: self.setIcon(24))
        self.ui.pushButton_25.clicked.connect(lambda: self.setIcon(25))
        self.ui.pushButton_26.clicked.connect(lambda: self.setIcon(26))
        self.ui.pushButton_27.clicked.connect(lambda: self.setIcon(27))
        self.ui.pushButton_28.clicked.connect(lambda: self.setIcon(28))
        self.ui.pushButton_29.clicked.connect(lambda: self.setIcon(29))
        self.ui.pushButton_30.clicked.connect(lambda: self.setIcon(30))
        self.ui.pushButton_31.clicked.connect(lambda: self.setIcon(31))
        self.ui.pushButton_32.clicked.connect(lambda: self.setIcon(32))
        self.ui.pushButton_33.clicked.connect(lambda: self.setIcon(33))
        self.ui.pushButton_34.clicked.connect(lambda: self.setIcon(34))
        self.ui.pushButton_35.clicked.connect(lambda: self.setIcon(35))
        self.ui.pushButton_36.clicked.connect(lambda: self.setIcon(36))
        self.ui.pushButton_37.clicked.connect(lambda: self.setIcon(37))
        self.ui.pushButton_38.clicked.connect(lambda: self.setIcon(38))
        self.ui.pushButton_39.clicked.connect(lambda: self.setIcon(39))
        self.ui.pushButton_40.clicked.connect(lambda: self.setIcon(40))
        self.ui.pushButton_41.clicked.connect(lambda: self.setIcon(41))
        self.ui.pushButton_42.clicked.connect(lambda: self.setIcon(42))
        self.ui.pushButton_43.clicked.connect(lambda: self.setIcon(43))
        self.ui.pushButton_44.clicked.connect(lambda: self.setIcon(44))
        self.ui.pushButton_45.clicked.connect(lambda: self.setIcon(45))
        self.ui.pushButton_46.clicked.connect(lambda: self.setIcon(46))
        self.ui.pushButton_47.clicked.connect(lambda: self.setIcon(47))
        self.ui.pushButton_48.clicked.connect(lambda: self.setIcon(48))
        self.ui.pushButton_49.clicked.connect(lambda: self.setIcon(49))
        self.ui.pushButton_50.clicked.connect(lambda: self.setIcon(50))
        self.ui.pushButton_51.clicked.connect(lambda: self.setIcon(51))
        self.ui.pushButton_52.clicked.connect(lambda: self.setIcon(52))
        self.ui.pushButton_53.clicked.connect(lambda: self.setIcon(53))


使用 lambda,好讓我們可以傳參數進入我們的 function。至於我們設定的 self.setIcon() 其實我當初命名有點不清楚,這個我要反省,當初寫的時候其實想得是要顯示該魔物的弱點圖片。

self.setIcon() 就是我們所有按鈕按下去後會觸發的事件。


跳出子視窗、顯示圖片

接著來到了最後的關頭,我們來定義我們觸發的 function 裡頭的功能。

    def setIcon(self, index):
        if index == 1:
            self.newWindow = sub_MainWindow("冥燈龍.jpg")
        elif index == 2:
            self.newWindow = sub_MainWindow("冰咒龍.jpg")
        elif index == 3:
            self.newWindow = sub_MainWindow("冰牙龍.jpg")
        elif index == 4:
            self.newWindow = sub_MainWindow("冰魚龍.jpg")
        elif index == 5:
            self.newWindow = sub_MainWindow("凶爪龍.jpg")
        elif index == 6:
            self.newWindow = sub_MainWindow("古代鹿首精.jpg")
        elif index == 7:
            self.newWindow = sub_MainWindow("土砂龍.jpg")
        elif index == 8:
            self.newWindow = sub_MainWindow("大凶豺龍.jpg")
        elif index == 9:
            self.newWindow = sub_MainWindow("大凶顎龍.jpg")
        elif index == 10:
            self.newWindow = sub_MainWindow("天地煌啼龍.jpg")
        elif index == 11:
            self.newWindow = sub_MainWindow("屍套龍.jpg")
        elif index == 12:
            self.newWindow = sub_MainWindow("岩賊龍.jpg")
        elif index == 13:
            self.newWindow = sub_MainWindow("恐暴龍.jpg")
        elif index == 14:
            self.newWindow = sub_MainWindow("慘爪龍.jpg")
        elif index == 15:
            self.newWindow = sub_MainWindow("斬龍.jpg")
        elif index == 16:
            self.newWindow = sub_MainWindow("櫻火龍.jpg")
        elif index == 17:
            self.newWindow = sub_MainWindow("殲世滅盡龍.jpg")
        elif index == 18:
            self.newWindow = sub_MainWindow("毒妖鳥.jpg")
        elif index == 19:
            self.newWindow = sub_MainWindow("水妖鳥.jpg")
        elif index == 20:
            self.newWindow = sub_MainWindow("泥魚龍.jpg")
        elif index == 21:
            self.newWindow = sub_MainWindow("浮眠龍.jpg")
        elif index == 22:
            self.newWindow = sub_MainWindow("浮空龍.jpg")
        elif index == 23:
            self.newWindow = sub_MainWindow("滅盡龍.jpg")
        elif index == 24:
            self.newWindow = sub_MainWindow("炎妃龍.jpg")
        elif index == 25:
            self.newWindow = sub_MainWindow("炎王龍.jpg")
        elif index == 26:
            self.newWindow = sub_MainWindow("熔山龍.jpg")
        elif index == 27:
            self.newWindow = sub_MainWindow("熔岩龍.jpg")
        elif index == 28:
            self.newWindow = sub_MainWindow("爆錘龍.jpg")
        elif index == 29:
            self.newWindow = sub_MainWindow("爆鱗龍.jpg")
        elif index == 30:
            self.newWindow = sub_MainWindow("猛牛龍.jpg")
        elif index == 31:
            self.newWindow = sub_MainWindow("痹毒龍.jpg")
        elif index == 32:
            self.newWindow = sub_MainWindow("眩鳥.jpg")
        elif index == 33:
            self.newWindow = sub_MainWindow("硫斬龍.jpg")
        elif index == 34:
            self.newWindow = sub_MainWindow("碎龍.jpg")
        elif index == 35:
            self.newWindow = sub_MainWindow("絢輝龍.jpg")
        elif index == 36:
            self.newWindow = sub_MainWindow("蒼火龍.jpg")
        elif index == 37:
            self.newWindow = sub_MainWindow("蠻顎龍.jpg")
        elif index == 38:
            self.newWindow = sub_MainWindow("角龍.jpg")
        elif index == 39:
            self.newWindow = sub_MainWindow("貝希摩斯.jpg")
        elif index == 40:
            self.newWindow = sub_MainWindow("轟龍.jpg")
        elif index == 41:
            self.newWindow = sub_MainWindow("迅龍.jpg")
        elif index == 42:
            self.newWindow = sub_MainWindow("鋼龍.jpg")
        elif index == 43:
            self.newWindow = sub_MainWindow("雄火龍.jpg")
        elif index == 44:
            self.newWindow = sub_MainWindow("雌火龍.jpg")
        elif index == 45:
            self.newWindow = sub_MainWindow("雷顎龍.jpg")
        elif index == 46:
            self.newWindow = sub_MainWindow("霜翼風漂龍.jpg")
        elif index == 47:
            self.newWindow = sub_MainWindow("風漂龍.jpg")
        elif index == 48:
            self.newWindow = sub_MainWindow("飛雷龍.jpg")
        elif index == 49:
            self.newWindow = sub_MainWindow("骨錘龍.jpg")
        elif index == 50:
            self.newWindow = sub_MainWindow("鹿首精.jpg")
        elif index == 51:
            self.newWindow = sub_MainWindow("麒麟.jpg")
        elif index == 52:
            self.newWindow = sub_MainWindow("黑狼鳥.jpg")
        elif index == 53:
            self.newWindow = sub_MainWindow("黑角龍.jpg")

        self.newWindow.show()


基本上就是設定不同的數值來傳入不同的圖片,然後顯示子視窗。
子視窗的 class 繼承如下:(繼承我們當初定義的第二個 GUI)

class sub_MainWindow(QMainWindow):
    def __init__(self, pic):
        super(sub_MainWindow, self).__init__()
        self.ui = Ui_MainWindow_sub()
        self.ui.setupUi(self)
        self.ui.label.setPixmap(QPixmap('pic/%s' % pic))
        self.ui.label.setScaledContents(True)


基本上只是依照我所傳入的 pic 變數,來決定我該顯示在 pic 這個資料夾(不是剛才的變數)底下我所選擇的照片。

if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())


最後,一如既往地讓我們的 GUI 能正常開始運作,一切都大功告成了。


程式碼自動生成的部份

看完了剛剛以上的程式碼,想必各位一定很疑惑:這樣一個個照片、按鈕、標籤照著輸入,不是花掉很多時間嗎?

其實有時候我會讓程式自動生成程式碼,尤其是這種需要大量重複設定的程式碼。

我先說明一下,剛剛那些很大量的程式碼其實分成四大部份:

  • QLabel 的定義
  • QPushButton 的縮圖設定
  • QPushButton 的事件觸發 (需給予不同的 id)
  • function 所需要的不同圖片 (依照不同 id 給予不同圖片)

這部份其實我都用程式完成了,而且程式短得讓人驚訝。
事先聲明,我程式當中一共有 pic 以及 icon 兩個資料夾,這裡頭會放著依照魔物命名的 JPG 檔,最後我會放上我的 Github 連結,大家可以在裡面找到。(接下來我預計會不斷更新的)

# -*- coding: utf-8 -*-
import os
import re

picList = os.listdir('pic/')
IconList = os.listdir('Icon/')


首先我 import 了兩個 Python 自帶的套件,使用 os 來顯示我 pic 資料夾、Icon 資料夾底下的所有檔案,並將它們分別存入變數 picList 以及 IconList。

def auto_label():
    n = 0
    for pic in picList:
        for icon in IconList:
            if pic == icon:
                n += 1
                if n == 1:
                    print('self.ui.label.setText("%s")\nself.ui.label.setAlignment(Qt.AlignCenter)' % re.sub('.jpg', '', pic))
                else:
                    print('self.ui.label_%s.setText("%s")\nself.ui.label_%s.setAlignment(Qt.AlignCenter)' % (n, re.sub('.jpg', '', pic), n))


使用 for 迴圈依序存取 picList 以及 IconList 底下的檔案,如有相同的便開始執行程式碼(因為裡頭有多出來的檔案,並不是每隻魔物都一定會有相應的圖片),然後自動產生 QLabel。

def auto_pushbutton():
    n = 0
    for pic in picList:
        for icon in IconList:
            if pic == icon:
                n += 1
                if n == 1:
                    print("self.ui.pushButton.setIcon(QIcon('Icon/%s'))\nself.ui.pushButton.setIconSize(QSize(100, 100))" % pic)
                else:
                    print("self.ui.pushButton_%s.setIcon(QIcon('Icon/%s'))\nself.ui.pushButton.setIconSize(QSize(100, 100))" % (n, pic))


依此類推產生 QPushButton。

def auto_ButtonClicked():
    n = 0
    for pic in picList:
        for icon in IconList:
            if pic == icon:
                n += 1
                if n == 1:
                    print('self.ui.pushButton.clicked.connect(lambda: self.setIcon(%s))' % n)
                else:
                    print('self.ui.pushButton_%s.clicked.connect(lambda: self.setIcon(%s))' % (n, n))


依此類推寫下每個按鈕給予事件的不同 id。

def auto_Event():
    n = 0
    for pic in picList:
        for icon in IconList:
            if pic == icon:
                n += 1
                if n == 1:
                    print('if index == %s:\n self.newWindow = sub_MainWindow("%s")' % (n, pic))
                else:
                    print('elif index == %s:\n self.newWindow = sub_MainWindow("%s")' % (n, pic))


依此類推寫下不同 id 時顯示的不同圖片設定。

以上,我用程式循環生產這四個部份的程式碼,並且將其複製到當初設定繼承圖形界面的地方。

這次執行程式,我們應該可以看到我們的魔物弱點屬性已經完成了。

有著像這個界面。

點擊之後會產生相應魔物的弱點圖片(這裡是官方所給的弱點圖片)。

而且找到魔物、點擊的速度非常快,目前有用過的朋友(其實包括我大概才 4 人左右哈哈哈) 都表示這比找網頁快,當然我個人私心也這麼覺得。


後記

那麼,這此這個簡單的教學已經告一段落了。老實說我很猶豫這要不要併入 PyQt5 的下一次教學,並詳細解說如何在程式碼中呼叫另一個界面。但想想這件事情其實滿簡單的,大家應該摸熟 PyQt5 之後都做得到,我有點像是在廢話 XD

另外,就是我魔物獵人玩太勤了,很想趕快寫一套充實、堪用的配裝器 XDDD 只好先拿這個練練手。

那麼,若想查看完整的程式碼,歡迎來我的 Github 看看: https://github.com/ccs96307/MonsterHunterRaiders

以後若是真的有寫出配裝器,我也會分享在這裡的,希望哪個大神能夠整理好 Monster Hunter World: Iceborne 的各種裝備資料!

Leave a Reply取消回覆

Exit mobile version