Python PyQt6 GUI 计算器 zip压缩包文件
立即下载
资源介绍:
Python PyQt6 GUI 计算器 zip压缩包文件
import sys
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6 import QtCore
from PyQt6.QtWidgets import QWidget
import math
Window_Icon = "D:/PythonProjects/计算器/Window_Icon.png"
Button_Font = QFont("Arial",20)
Help_text = '''
平方(x²)的格式是“pow(x,n)”,意为x的n次方,x通常为2。
根号/开平方(²√x)的格式是“sqrt(x)”,意为根号x。
'''
WIDTH = 300
HEIGHT = 400
class Main_Window(QWidget):
def __init__(self):
super(Main_Window,self).__init__()
self.resize(WIDTH,HEIGHT)
self.setWindowTitle("计算器")
self.setWindowIcon(QIcon(Window_Icon))
self.Menu = QLabel(self)
self.Menu.resize(WIDTH,HEIGHT)
self.button = QToolButton(self.Menu)
self.button.setGeometry(0,0,40,40)
self.button.setFont(QFont("Arial",15))
self.button.setText("☰")
text = "标准"
self.text = QLabel(self.Menu)
self.text.setGeometry(40,0,80,40)
self.text.setFont(QFont("SimHei",15))
self.text.setText(" "+text)
self.Numlabel = QLineEdit(self.Menu)
self.Numlabel.setGeometry(0,40,WIDTH,60)
self.Numlabel.setAlignment(Qt.AlignmentFlag.AlignRight|Qt.AlignmentFlag.AlignBottom)
self.Numlabel.setFont(QFont("Arial",30))
self.Numlabel.setReadOnly(True)
self.button_left_braket = QToolButton(self.Menu)
self.button_left_braket.setGeometry(0,100,75,50)
self.button_left_braket.setFont(Button_Font)
self.button_left_braket.setText("(")
self.button_right_braket = QToolButton(self.Menu)
self.button_right_braket.setGeometry(75,100,75,50)
self.button_right_braket.setFont(Button_Font)
self.button_right_braket.setText(")")
self.button_comma = QToolButton(self.Menu)
self.button_comma.setGeometry(150,100,75,50)
self.button_comma.setFont(Button_Font)
self.button_comma.setText(",")
self.button_sq = QToolButton(self.Menu)
self.button_sq.setGeometry(0,150,75,50)
self.button_sq.setFont(Button_Font)
self.button_sq.setText("x²")
self.button_sqrt = QToolButton(self.Menu)
self.button_sqrt.setGeometry(75,150,75,50)
self.button_sqrt.setFont(Button_Font)
self.button_sqrt.setText("²√x")
self.button_clear = QToolButton(self.Menu)
self.button_clear.setGeometry(150,150,75,50)
self.button_clear.setFont(Button_Font)
self.button_clear.setText("C")
self.button_backspace = QToolButton(self.Menu)
self.button_backspace.setGeometry(225,150,75,50)
self.button_backspace.setFont(Button_Font)
self.button_backspace.setText("◁×")
self.button_7 = QToolButton(self.Menu)
self.button_7.setGeometry(0,200,75,50)
self.button_7.setFont(Button_Font)
self.button_7.setText("7")
self.button_8 = QToolButton(self.Menu)
self.button_8.setGeometry(75,200,75,50)
self.button_8.setFont(Button_Font)
self.button_8.setText("8")
self.button_9 = QToolButton(self.Menu)
self.button_9.setGeometry(150,200,75,50)
self.button_9.setFont(Button_Font)
self.button_9.setText("9")
self.button_div = QToolButton(self.Menu)
self.button_div.setGeometry(225,200,75,50)
self.button_div.setFont(Button_Font)
self.button_div.setText("÷")
self.button_4 = QToolButton(self.Menu)
self.button_4.setGeometry(0,250,75,50)
self.button_4.setFont(Button_Font)
self.button_4.setText("4")
self.button_5 = QToolButton(self.Menu)
self.button_5.setGeometry(75,250,75,50)
self.button_5.setFont(Button_Font)
self.button_5.setText("5")
self.button_6 = QToolButton(self.Menu)
self.button_6.setGeometry(150,250,75,50)
self.button_6.setFont(Button_Font)
self.button_6.setText("6")
self.button_mul = QToolButton(self.Menu)
self.button_mul.setGeometry(225,250,75,50)
self.button_mul.setFont(Button_Font)
self.button_mul.setText("×")
self.button_1 = QToolButton(self.Menu)
self.button_1.setGeometry(0,300,75,50)
self.button_1.setFont(Button_Font)
self.button_1.setText("1")
self.button_2 = QToolButton(self.Menu)
self.button_2.setGeometry(75,300,75,50)
self.button_2.setFont(Button_Font)
self.button_2.setText("2")
self.button_3 = QToolButton(self.Menu)
self.button_3.setGeometry(150,300,75,50)
self.button_3.setFont(Button_Font)
self.button_3.setText("3")
self.button_sub = QToolButton(self.Menu)
self.button_sub.setGeometry(225,300,75,50)
self.button_sub.setFont(Button_Font)
self.button_sub.setText("-")
self.button_0 = QToolButton(self.Menu)
self.button_0.setGeometry(0,350,75,50)
self.button_0.setFont(Button_Font)
self.button_0.setText("0")
self.button_point = QToolButton(self.Menu)
self.button_point.setGeometry(75,350,75,50)
self.button_point.setFont(Button_Font)
self.button_point.setText(".")
self.button_calculate = QToolButton(self.Menu)
self.button_calculate.setGeometry(150,350,75,50)
self.button_calculate.setFont(Button_Font)
self.button_calculate.setText("=")
self.button_add = QToolButton(self.Menu)
self.button_add.setGeometry(225,350,75,50)
self.button_add.setFont(Button_Font)
self.button_add.setText("+")
self.button.clicked.connect(lambda:QMessageBox.information(self,"帮助",Help_text))
self.button_left_braket.clicked.connect(lambda:self.Input("("))
self.button_right_braket.clicked.connect(lambda:self.Input(")"))
self.button_comma.clicked.connect(lambda:self.Input(","))
self.button_sq.clicked.connect(lambda:self.Input("pow("))
self.button_sqrt.clicked.connect(lambda:self.Input("sqrt("))
self.button_clear.clicked.connect(lambda:self.Clear())
self.button_backspace.clicked.connect(lambda:self.Backspace())
self.button_7.clicked.connect(lambda:self.Input("7"))
self.button_8.clicked.connect(lambda:self.Input("8"))
self.button_9.clicked.connect(lambda:self.Input("9"))
self.button_div.clicked.connect(lambda:self.Input("/"))
self.button_4.clicked.connect(lambda:self.Input("4"))
self.button_5.clicked.connect(lambda:self.Input("5"))
self.button_6.clicked.connect(lambda:self.Input("6"))
self.button_mul.clicked.connect(lambda:self.Input("*"))
self.button_1.clicked.connect(lambda:self.Input("1"))
self.button_2.clicked.connect(lambda:self.Input("2"))
self.button_3.clicked.connect(lambda:self.Input("3"))
self.button_sub.clicked.connect(lambda:self.Input("-"))
self.button_0.clicked.connect(lambda:self.Input("0"))
self.button_point.clicked.connect(lambda:self.Input("."))
self.button_calculate.clicked.connect(lambda:self.Calculate())
self.button_add.clicked.connect(lambda:self.Input("+"))
self.Clear()
def Calculate(self):
try:
answer = eval(self.Numlabel.text())
self.Numlabel.setText(str(answer))
except:
self.Numlabel.setText("ERROR")
def Backspace(self):
if self.Numlabel.text() != "0" and self.Numlabel.text() != "ERROR":
Str = self.Numlabel.text()
List = list(Str)
List.pop(len(List)-1)
Str = ''.join(List)
if Str == "":
self.Numlabel.setText("0")
else:
self.Numlabel.setText(Str)
def Input(self,text):
资源文件列表:
计算器.zip 大约有192个文件