PyQt5可视化窗口【计算器】

import sys

from PyQt5.QtWidgets import QApplication, QTextEdit, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QGroupBox, QRadioButton, QLineEdit,QGridLayout

from PyQt5.QtCore import QPoint

class MyWindow(QWidget):

方法init_text窗口函数(addWidge:添加一个小部件窗口)
创建一个文本框,setFixedSize设置文本框大小

    def init_text(self):
        text = QLineEdit()
        text.setFixedSize(800,30)
        self.text = text
        return text    

init_grid(addLayout:在布局控制器里添加一个小的布局控制器)
data 字典 : 按照行数计算器按键的显示
循环字典里按键一整行的字符,然后循环列表
btn, 按钮 line_number 行数 , col_number 列数
btn.clicked.connect(self.buttonClicked) 链接可点击

    def init_grid(self):
        data ={
            0: ["7", "8", "9", "+", "-"],
            1: ["4", "5", "6", "*", "/"],
            2: ["1", "2", "3", "(", ")"],
            3: ["0", ".", "=", "del", "C"]
        }
        grid = QGridLayout()
        for line_number, line_data in data.items():
            for col_number, number in enumerate(line_data):
                btn = QPushButton(number)
                grid.addWidget(btn, line_number, col_number)
                btn.clicked.connect(self.buttonClicked)
        return grid

buttonClicked 运算函数 ,目前支持单次运算结果加减乘除**(结果出来后无法继续再加减乘除运行)**

    def buttonClicked(self):
         #sender = self.sender();  
         # 确定信号发送者
         # self.display.setText(sender.text())
        sender = self.sender().text()
        if sender not in "Cdel":
            self.input_list += sender
        if sender == "C":
            self.input_list = ""
            # self.text.setText(self.input_list) 
            #设置文本为空
            self.text.clear()  
            #清空文本
        if sender == "del":
            self.input_list = self.input_list[:-1]
        if sender == "=":
            idx = 0  
            #初始索引位置
            result = 0   
            #初始结果数字
            number = None  
            #上一个运算符
           # self.input_list = [99+8-7=]
            for i, info in enumerate(self.input_list):
                if info in "+-*/":
                    if number == None:
                        result = int(self.input_list[idx:i])
                        idx = i+1 
                        #运算符后一位的索引位置
                        number = info 
                        #保存运算符下次判断
                    else:
                        if number == "+":
                            result += int(self.input_list[idx:i])
                            idx = i+1
                            number = info
                        elif number == "-":
                            result -= int(self.input_list[idx:i])
                            idx = i+1
                            number = info
                        elif number == "*":
                            result *= int(self.input_list[idx:i])
                            idx = i+1
                            number = info
                        elif number == "/":
                            result -= int(self.input_list[idx:i])
                            idx = i+1
                            number = info
                if info == "=":
                    if number == "+":
                        result += int(self.input_list[idx:i])
                    elif number == "-":
                        result -= int(self.input_list[idx:i])
                    elif number == "*":
                        result *= int(self.input_list[idx:i])
                    elif number == "/":
                        result /= int(self.input_list[idx:i])
                #[8*(9+6-9)]
            str_result = str(result)
            self.text.setText(str_result)
            self.input_list = ""
        else:
            self.text.setText(self.input_list)  
            #文本显示内容
app = QApplication(sys.argv)
w = MyWindow()
w.show()
app.exec()

初始化定义文本,QVBoxLayout 垂直布局控制器函数,调用方法init_text窗口函数(addWidge:添加一个小部件窗口)、init_grid(addLayout:在布局控制器里添加一个小的布局控制器)

    def __init__(self):
        super().__init__()    
        self.text = None
        self.input_list = ""
        layout = QVBoxLayout()
        layout.addWidget(self.init_text())
        layout.addLayout(self.init_grid())  
        self.setLayout(layout)

self.setLayout(layout) 设置布局方式(主窗口)在这里插入代码片

import sys
from PyQt5.QtWidgets import QApplication, QTextEdit, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QGroupBox, QRadioButton, QLineEdit,QGridLayout
from PyQt5.QtCore import QPoint

class MyWindow(QWidget):
def init(self):
super().init()

    self.text = None
    self.input_list = ""
    self.str_result = None

    
    layout = QVBoxLayout()
    layout.addWidget(self.init_text())
    layout.addLayout(self.init_grid())  
 
    self.setLayout(layout)

def init_text(self):
    text = QLineEdit()
    text.setFixedSize(800,30)
    self.text = text

    return text    
    

def init_grid(self):
    data ={
        0: ["7", "8", "9", "+", "-"],
        1: ["4", "5", "6", "*", "/"],
        2: ["1", "2", "3", "(", ")"],
        3: ["0", ".", "=", "del", "C"]
    }
    grid = QGridLayout()
    for line_number, line_data in data.items():
        for col_number, number in enumerate(line_data):
            btn = QPushButton(number)
            
            grid.addWidget(btn, line_number, col_number)
            btn.clicked.connect(self.buttonClicked)
    
    return grid


def buttonClicked(self):
     #sender = self.sender();  # 确定信号发送者
     #self.display.setText(sender.text())
    
    sender = self.sender().text()
    if sender in "+-*/" and len(self.input_list)== 0:
        self.input_list = self.str_result 
        print("error")

    if sender not in "Cdel":
        self.input_list += sender
    if sender == "C":
        self.input_list = ""
        # self.text.setText(self.input_list) #设置文本为空
        self.text.clear()  #清空文本
    if sender == "del":
        self.input_list = self.input_list[:-1]

    if sender == "=":
        
        idx = 0  #初始索引位置
        result = 0   #初始结果数字
        number = None  #上一个运算符
       # self.input_list = [99+8-7=]
        for i, info in enumerate(self.input_list):
            if info in "+-*/":
                if number == None:
                    result = int(self.input_list[idx:i])
                    idx = i+1 #运算符后一位的索引位置
                    number = info #保存运算符下次判断
                else:
                    if number == "+":
                        result += int(self.input_list[idx:i])
                        idx = i+1
                        number = info
                    elif number == "-":
                        result -= int(self.input_list[idx:i])
                        idx = i+1
                        number = info
                    elif number == "*":
                        result *= int(self.input_list[idx:i])
                        idx = i+1
                        number = info
                    elif number == "/":
                        result -= int(self.input_list[idx:i])
                        idx = i+1
                        number = info
                    
            if info == "=":
                if number == "+":
                    result += int(self.input_list[idx:i])
                elif number == "-":
                    result -= int(self.input_list[idx:i])
                elif number == "*":
                    result *= int(self.input_list[idx:i])
                elif number == "/":
                    result /= int(self.input_list[idx:i])
            #[8*(9+6-9)]
        
                                              
        self.str_result = str(result)
        self.text.setText(self.str_result)
        self.input_list = "" 

   
    else:
        self.text.setText(self.input_list)  #文本显示内容

app = QApplication(sys.argv)
w = MyWindow()
w.show()

app.exec()

你可能感兴趣的:(qt)