gui学习
This commit is contained in:
parent
c2333af67c
commit
9e03066b38
|
|
@ -0,0 +1,188 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
## Form generated from reading UI file '计算器.ui'
|
||||||
|
##
|
||||||
|
## Created by: Qt User Interface Compiler version 6.6.2
|
||||||
|
##
|
||||||
|
## WARNING! All changes made in this file will be lost when recompiling UI file!
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
|
||||||
|
QMetaObject, QObject, QPoint, QRect,
|
||||||
|
QSize, QTime, QUrl, Qt)
|
||||||
|
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
|
||||||
|
QFont, QFontDatabase, QGradient, QIcon,
|
||||||
|
QImage, QKeySequence, QLinearGradient, QPainter,
|
||||||
|
QPalette, QPixmap, QRadialGradient, QTransform)
|
||||||
|
from PySide6.QtWidgets import (QApplication, QHBoxLayout, QLineEdit, QPushButton,
|
||||||
|
QSizePolicy, QVBoxLayout, QWidget)
|
||||||
|
|
||||||
|
class Ui_Form(object):
|
||||||
|
def setupUi(self, Form):
|
||||||
|
if not Form.objectName():
|
||||||
|
Form.setObjectName(u"Form")
|
||||||
|
Form.resize(334, 235)
|
||||||
|
self.widget = QWidget(Form)
|
||||||
|
self.widget.setObjectName(u"widget")
|
||||||
|
self.widget.setGeometry(QRect(10, 0, 322, 230))
|
||||||
|
self.verticalLayout = QVBoxLayout(self.widget)
|
||||||
|
self.verticalLayout.setObjectName(u"verticalLayout")
|
||||||
|
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
self.lineEdit = QLineEdit(self.widget)
|
||||||
|
self.lineEdit.setObjectName(u"lineEdit")
|
||||||
|
self.lineEdit.setMinimumSize(QSize(0, 100))
|
||||||
|
font = QFont()
|
||||||
|
font.setPointSize(48)
|
||||||
|
self.lineEdit.setFont(font)
|
||||||
|
self.lineEdit.setAlignment(Qt.AlignRight|Qt.AlignTrailing|Qt.AlignVCenter)
|
||||||
|
self.lineEdit.setClearButtonEnabled(False)
|
||||||
|
|
||||||
|
self.verticalLayout.addWidget(self.lineEdit, 0, Qt.AlignVCenter)
|
||||||
|
|
||||||
|
self.horizontalLayout = QHBoxLayout()
|
||||||
|
self.horizontalLayout.setObjectName(u"horizontalLayout")
|
||||||
|
self.pushButton_7 = QPushButton(self.widget)
|
||||||
|
self.pushButton_7.setObjectName(u"pushButton_7")
|
||||||
|
self.pushButton_7.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout.addWidget(self.pushButton_7)
|
||||||
|
|
||||||
|
self.pushButton_8 = QPushButton(self.widget)
|
||||||
|
self.pushButton_8.setObjectName(u"pushButton_8")
|
||||||
|
self.pushButton_8.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout.addWidget(self.pushButton_8)
|
||||||
|
|
||||||
|
self.pushButton_9 = QPushButton(self.widget)
|
||||||
|
self.pushButton_9.setObjectName(u"pushButton_9")
|
||||||
|
self.pushButton_9.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout.addWidget(self.pushButton_9)
|
||||||
|
|
||||||
|
self.pushButton_div = QPushButton(self.widget)
|
||||||
|
self.pushButton_div.setObjectName(u"pushButton_div")
|
||||||
|
self.pushButton_div.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout.addWidget(self.pushButton_div)
|
||||||
|
|
||||||
|
|
||||||
|
self.verticalLayout.addLayout(self.horizontalLayout)
|
||||||
|
|
||||||
|
self.horizontalLayout_2 = QHBoxLayout()
|
||||||
|
self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
|
||||||
|
self.pushButton_4 = QPushButton(self.widget)
|
||||||
|
self.pushButton_4.setObjectName(u"pushButton_4")
|
||||||
|
self.pushButton_4.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_2.addWidget(self.pushButton_4)
|
||||||
|
|
||||||
|
self.pushButton_5 = QPushButton(self.widget)
|
||||||
|
self.pushButton_5.setObjectName(u"pushButton_5")
|
||||||
|
self.pushButton_5.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_2.addWidget(self.pushButton_5)
|
||||||
|
|
||||||
|
self.pushButton_6 = QPushButton(self.widget)
|
||||||
|
self.pushButton_6.setObjectName(u"pushButton_6")
|
||||||
|
self.pushButton_6.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_2.addWidget(self.pushButton_6)
|
||||||
|
|
||||||
|
self.pushButton_times = QPushButton(self.widget)
|
||||||
|
self.pushButton_times.setObjectName(u"pushButton_times")
|
||||||
|
self.pushButton_times.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_2.addWidget(self.pushButton_times)
|
||||||
|
|
||||||
|
|
||||||
|
self.verticalLayout.addLayout(self.horizontalLayout_2)
|
||||||
|
|
||||||
|
self.horizontalLayout_3 = QHBoxLayout()
|
||||||
|
self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
|
||||||
|
self.pushButton_1 = QPushButton(self.widget)
|
||||||
|
self.pushButton_1.setObjectName(u"pushButton_1")
|
||||||
|
self.pushButton_1.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_3.addWidget(self.pushButton_1)
|
||||||
|
|
||||||
|
self.pushButton_2 = QPushButton(self.widget)
|
||||||
|
self.pushButton_2.setObjectName(u"pushButton_2")
|
||||||
|
self.pushButton_2.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_3.addWidget(self.pushButton_2)
|
||||||
|
|
||||||
|
self.pushButton_3 = QPushButton(self.widget)
|
||||||
|
self.pushButton_3.setObjectName(u"pushButton_3")
|
||||||
|
self.pushButton_3.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_3.addWidget(self.pushButton_3)
|
||||||
|
|
||||||
|
self.pushButton_sub = QPushButton(self.widget)
|
||||||
|
self.pushButton_sub.setObjectName(u"pushButton_sub")
|
||||||
|
self.pushButton_sub.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_3.addWidget(self.pushButton_sub)
|
||||||
|
|
||||||
|
|
||||||
|
self.verticalLayout.addLayout(self.horizontalLayout_3)
|
||||||
|
|
||||||
|
self.horizontalLayout_4 = QHBoxLayout()
|
||||||
|
self.horizontalLayout_4.setObjectName(u"horizontalLayout_4")
|
||||||
|
self.pushButton_0 = QPushButton(self.widget)
|
||||||
|
self.pushButton_0.setObjectName(u"pushButton_0")
|
||||||
|
self.pushButton_0.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_4.addWidget(self.pushButton_0)
|
||||||
|
|
||||||
|
self.pushButton_dot = QPushButton(self.widget)
|
||||||
|
self.pushButton_dot.setObjectName(u"pushButton_dot")
|
||||||
|
self.pushButton_dot.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_4.addWidget(self.pushButton_dot)
|
||||||
|
|
||||||
|
self.pushButton_eq = QPushButton(self.widget)
|
||||||
|
self.pushButton_eq.setObjectName(u"pushButton_eq")
|
||||||
|
self.pushButton_eq.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_4.addWidget(self.pushButton_eq)
|
||||||
|
|
||||||
|
self.pushButton_add = QPushButton(self.widget)
|
||||||
|
self.pushButton_add.setObjectName(u"pushButton_add")
|
||||||
|
self.pushButton_add.setEnabled(True)
|
||||||
|
|
||||||
|
self.horizontalLayout_4.addWidget(self.pushButton_add)
|
||||||
|
|
||||||
|
|
||||||
|
self.verticalLayout.addLayout(self.horizontalLayout_4)
|
||||||
|
|
||||||
|
|
||||||
|
self.retranslateUi(Form)
|
||||||
|
|
||||||
|
QMetaObject.connectSlotsByName(Form)
|
||||||
|
# setupUi
|
||||||
|
|
||||||
|
def retranslateUi(self, Form):
|
||||||
|
Form.setWindowTitle(QCoreApplication.translate("Form", u"\u4e00\u4e2a\u7b80\u964b\u7684\u8ba1\u7b97\u5668", None))
|
||||||
|
#if QT_CONFIG(statustip)
|
||||||
|
self.lineEdit.setStatusTip("")
|
||||||
|
#endif // QT_CONFIG(statustip)
|
||||||
|
self.lineEdit.setPlaceholderText(QCoreApplication.translate("Form", u"0", None))
|
||||||
|
self.pushButton_7.setText(QCoreApplication.translate("Form", u"7", None))
|
||||||
|
self.pushButton_8.setText(QCoreApplication.translate("Form", u"8", None))
|
||||||
|
self.pushButton_9.setText(QCoreApplication.translate("Form", u"9", None))
|
||||||
|
self.pushButton_div.setText(QCoreApplication.translate("Form", u"/", None))
|
||||||
|
self.pushButton_4.setText(QCoreApplication.translate("Form", u"4", None))
|
||||||
|
self.pushButton_5.setText(QCoreApplication.translate("Form", u"5", None))
|
||||||
|
self.pushButton_6.setText(QCoreApplication.translate("Form", u"6", None))
|
||||||
|
self.pushButton_times.setText(QCoreApplication.translate("Form", u"*", None))
|
||||||
|
self.pushButton_1.setText(QCoreApplication.translate("Form", u"1", None))
|
||||||
|
self.pushButton_2.setText(QCoreApplication.translate("Form", u"2", None))
|
||||||
|
self.pushButton_3.setText(QCoreApplication.translate("Form", u"3", None))
|
||||||
|
self.pushButton_sub.setText(QCoreApplication.translate("Form", u"-", None))
|
||||||
|
self.pushButton_0.setText(QCoreApplication.translate("Form", u"0", None))
|
||||||
|
self.pushButton_dot.setText(QCoreApplication.translate("Form", u".", None))
|
||||||
|
self.pushButton_eq.setText(QCoreApplication.translate("Form", u"=", None))
|
||||||
|
self.pushButton_add.setText(QCoreApplication.translate("Form", u"+", None))
|
||||||
|
# retranslateUi
|
||||||
|
|
||||||
|
|
@ -1,11 +1,42 @@
|
||||||
from PySide6.QtWidgets import QMainWindow, QApplication, QLabel
|
from PySide6.QtWidgets import QWidget, QApplication, QLineEdit, QVBoxLayout
|
||||||
|
from Ui_计算器 import Ui_Form
|
||||||
|
|
||||||
class MyWindow(QMainWindow):
|
class MyWindow(QWidget,Ui_Form):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
lbl = QLabel("按钮", self)
|
self.setupUi(self)
|
||||||
lbl.setGeometry(100,100,200,100)
|
|
||||||
lbl.setText("重新设置按钮文字")
|
self.result = ""
|
||||||
|
|
||||||
|
self.bind()
|
||||||
|
|
||||||
|
def bind(self):
|
||||||
|
self.pushButton_0.clicked.connect(lambda: self.addNumber('0'))
|
||||||
|
self.pushButton_1.clicked.connect(lambda: self.addNumber('1'))
|
||||||
|
self.pushButton_2.clicked.connect(lambda: self.addNumber('2'))
|
||||||
|
self.pushButton_3.clicked.connect(lambda: self.addNumber('3'))
|
||||||
|
self.pushButton_4.clicked.connect(lambda: self.addNumber('4'))
|
||||||
|
self.pushButton_5.clicked.connect(lambda: self.addNumber('5'))
|
||||||
|
self.pushButton_6.clicked.connect(lambda: self.addNumber('6'))
|
||||||
|
self.pushButton_7.clicked.connect(lambda: self.addNumber('7'))
|
||||||
|
self.pushButton_8.clicked.connect(lambda: self.addNumber('8'))
|
||||||
|
self.pushButton_9.clicked.connect(lambda: self.addNumber('9'))
|
||||||
|
self.pushButton_add.clicked.connect(lambda: self.addNumber('+'))
|
||||||
|
self.pushButton_sub.clicked.connect(lambda: self.addNumber('-'))
|
||||||
|
self.pushButton_times.clicked.connect(lambda: self.addNumber('*'))
|
||||||
|
self.pushButton_div.clicked.connect(lambda: self.addNumber('/'))
|
||||||
|
self.pushButton_dot.clicked.connect(lambda: self.addNumber('.'))
|
||||||
|
self.pushButton_eq.clicked.connect(self.equal)
|
||||||
|
|
||||||
|
|
||||||
|
def addNumber(self, number):
|
||||||
|
self.lineEdit.clear()
|
||||||
|
self.result += number
|
||||||
|
self.lineEdit.setText(self.result)
|
||||||
|
|
||||||
|
def equal(self):
|
||||||
|
self.numberResult = eval(self.result)
|
||||||
|
self.lineEdit.setText(str(self.numberResult))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = QApplication([])
|
app = QApplication([])
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,234 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>Form</class>
|
||||||
|
<widget class="QWidget" name="Form">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>334</width>
|
||||||
|
<height>235</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>一个简陋的计算器</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>10</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>322</width>
|
||||||
|
<height>230</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout">
|
||||||
|
<item alignment="Qt::AlignVCenter">
|
||||||
|
<widget class="QLineEdit" name="lineEdit">
|
||||||
|
<property name="minimumSize">
|
||||||
|
<size>
|
||||||
|
<width>0</width>
|
||||||
|
<height>100</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
<property name="font">
|
||||||
|
<font>
|
||||||
|
<pointsize>48</pointsize>
|
||||||
|
</font>
|
||||||
|
</property>
|
||||||
|
<property name="statusTip">
|
||||||
|
<string/>
|
||||||
|
</property>
|
||||||
|
<property name="alignment">
|
||||||
|
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||||
|
</property>
|
||||||
|
<property name="placeholderText">
|
||||||
|
<string>0</string>
|
||||||
|
</property>
|
||||||
|
<property name="clearButtonEnabled">
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_7">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>7</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_8">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>8</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_9">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>9</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_div">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>/</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_4">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>4</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_5">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>5</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_6">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>6</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_times">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>*</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_1">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>1</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_2">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>2</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_3">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>3</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_sub">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>-</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_0">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>0</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_dot">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>.</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_eq">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>=</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_add">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>+</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
||||||
Reference in New Issue