加算、減算、乗算、除算を例として、pycharm を使用します。
1. デザインインターフェイス
- デザインホームページ
[ツール] -> [外部ツール] -> [Qt Designer] をクリックし、4 つのボタン (ページ 1、ページ 2、ページ 3、ページ 4) を追加し、shouye.ui として保存します。
ターミナルで pyuic5 -o shouye.py shouye.ui -x を使用して、ui ファイルを .py ファイルに変換します。
-
ページ1
2 つの数値の加算を実現します。数値 1 と数値 2 はラベル、数値入力ボックスは LineEdit、結果は PushButton、結果出力ボックスは TextEdit です。
リンク関係を追加します。
setupUi() にコードを追加します。add は関数名です。
self.pushButton.clicked.connect(self.add)
LineEditのコンテンツを取得する
self.lineEdit.text()
TextEditの内容の取得と入力内容
self.textEdit.toPlainText()
self.textEdit.setText('要输入的内容')
- 2ページ、3ページ、4ページ 同上
2. 異なるページ間をジャンプする
メインインターフェイスの各ページのpyファイルをインポートします
from add import * # py文件名
from sub import *
from mul import *
from dev import *
メインインターフェースのオブジェクトとQtWidgets.QMainWindowを変更し、各サブインターフェースのクラス名をメインインターフェースのクラス名と重複しないように修正し、以下のように定義します。
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindow,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
class Add(QtWidgets.QMainWindow):
def __init__(self):
super(Add,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
class Sub(QtWidgets.QMainWindow):
def __init__(self):
super(Sub,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
class Mul(QtWidgets.QMainWindow):
def __init__(self):
super(Mul,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
class Dev(QtWidgets.QMainWindow):
def __init__(self):
super(Dev,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
ホームページボタンをジャンプに設定する
self.pushButton.clicked.connect(self.yemian1)
self.pushButton_2.clicked.connect(self.yemian2)
self.pushButton_3.clicked.connect(self.yemian3)
self.pushButton_4.clicked.connect(self.yemian4)
各ページジャンプ機能を設計
def yemian1(self):
ui_add.show()
MainWindow.close()
def yemian2(self):
ui_sub.show()
MainWindow.close()
def yemian3(self):
ui_mul.show()
MainWindow.close()
def yemian4(self):
ui_dev.show()
MainWindow.close()
各ページジャンプのオブジェクトをページ内に設定します
ui_add=Add() # 类名
ui_sub=Sub()
ui_mul=Mul()
ui_dev=Dev()