如何将Qt Designer完成的设计与PyCharm中Python3.6.4及PyQt5关联
Hey Tariq, let's get your Qt Designer setup linked with PyCharm and Python 3.6.4 smoothly. Since you've already got PyQt5 installed, here's a step-by-step guide to tie everything together:
Step 1: Configure Python Interpreter in PyCharm
- Open PyCharm and head to File > Settings (or PyCharm > Preferences if you're on macOS).
- Navigate to Project: [Your Project Name] > Python Interpreter.
- Click the gear icon ⚙️ and select Add.
- Choose System Interpreter, then locate your Python 3.6.4 installation path — based on your pip output, it should be
C:\Users\Tariq\AppData\Local\Programs\Python\Python36-32\python.exe. - Select this path and click OK to set it as your project's interpreter. This ensures PyCharm uses the exact Python version where you installed PyQt5.
Step 2: Link Qt Designer to PyCharm
- Stay in Settings and go to Tools > External Tools.
- Click the + button to add a new external tool.
- Fill in these details:
- Name: Qt Designer
- Program: Find the
designer.exefile in your PyQt5 folder. It’s typically atC:\Users\Tariq\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\PyQt5\Qt\bin\designer.exe - Arguments: Leave this blank
- Working directory:
$ProjectFileDir$(this makes Designer open files relative to your project folder)
- Click Apply then OK to save. Now you can launch Qt Designer directly from PyCharm via Tools > External Tools > Qt Designer.
Step 3: Convert .ui Files to Python Code
After designing your UI in Qt Designer and saving it as a .ui file, you need to convert it to Python code to use it in your project. Here’s how to set up the converter tool:
- Go back to Tools > External Tools and click + again.
- Fill in:
- Name: PyUIC
- Program: Your Python 3.6.4
python.exepath (same as before:C:\Users\Tariq\AppData\Local\Programs\Python\Python36-32\python.exe) - Arguments:
-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py - Working directory:
$FileDir$
- Save this tool. Now, right-click your
.uifile in PyCharm, go to External Tools > PyUIC, and it’ll generate a corresponding.pyfile with your UI code.
Step 4: Import and Use the Generated UI Code
In your main Python script, you can import the generated UI class and use it like this:
import sys from PyQt5.QtWidgets import QApplication, QMainWindow from your_ui_file_name import Ui_MainWindow # Replace with your actual UI class name class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) # Add your custom functionality here if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
Just make sure the generated .py file is in the same directory as your main script, or adjust the import path if needed.
内容的提问来源于stack exchange,提问作者Tariq
相关产品推荐
相关产品推荐

