pyqt5实例讲解
PyQt5实现简易计算器本文实例为大家分享了pyqt5实现简易计算器的具体代码,供大家参考,具体内容如下
效果图:
界面代码
calc_interface.py
|
# -*- coding: utf-8 -*- # form implementation generated from reading ui file 'calc_interface.ui' # # created by: pyqt5 ui code generator 5.8.2 # # warning! all changes made in this file will be lost! from pyqt5 import qtcore, qtgui, qtwidgets class ui_mainwindow( object ): def setupui( self , mainwindow): mainwindow.setobjectname( "mainwindow" ) mainwindow.resize( 647 , 675 ) self .centralwidget = qtwidgets.qwidget(mainwindow) self .centralwidget.setobjectname( "centralwidget" ) self .b_1 = qtwidgets.qpushbutton( self .centralwidget) self .b_1.setgeometry(qtcore.qrect( 10 , 330 , 71 , 71 )) self .b_1.setobjectname( "b_1" ) self .b_2 = qtwidgets.qpushbutton( self .centralwidget) self .b_2.setgeometry(qtcore.qrect( 80 , 330 , 71 , 71 )) self .b_2.setobjectname( "b_2" ) self .b_3 = qtwidgets.qpushbutton( self .centralwidget) self .b_3.setgeometry(qtcore.qrect( 150 , 330 , 71 , 71 )) self .b_3.setobjectname( "b_3" ) self .b_6 = qtwidgets.qpushbutton( self .centralwidget) self .b_6.setgeometry(qtcore.qrect( 150 , 400 , 71 , 71 )) self .b_6.setobjectname( "b_6" ) self .b_4 = qtwidgets.qpushbutton( self .centralwidget) self .b_4.setgeometry(qtcore.qrect( 10 , 400 , 71 , 71 )) self .b_4.setobjectname( "b_4" ) self .b_5 = qtwidgets.qpushbutton( self .centralwidget) self .b_5.setgeometry(qtcore.qrect( 80 , 400 , 71 , 71 )) self .b_5.setobjectname( "b_5" ) self .b_8 = qtwidgets.qpushbutton( self .centralwidget) self .b_8.setgeometry(qtcore.qrect( 80 , 470 , 71 , 71 )) self .b_8.setobjectname( "b_8" ) self .b_9 = qtwidgets.qpushbutton( self .centralwidget) self .b_9.setgeometry(qtcore.qrect( 150 , 470 , 71 , 71 )) self .b_9.setobjectname( "b_9" ) self .b_7 = qtwidgets.qpushbutton( self .centralwidget) self .b_7.setgeometry(qtcore.qrect( 10 , 470 , 71 , 71 )) self .b_7.setobjectname( "b_7" ) self .b_eq = qtwidgets.qpushbutton( self .centralwidget) self .b_eq.setgeometry(qtcore.qrect( 150 , 540 , 211 , 71 )) self .b_eq.setobjectname( "b_eq" ) self .b_mod = qtwidgets.qpushbutton( self .centralwidget) self .b_mod.setgeometry(qtcore.qrect( 220 , 400 , 71 , 71 )) self .b_mod.setobjectname( "b_mod" ) self .b_bra_l = qtwidgets.qpushbutton( self .centralwidget) self .b_bra_l.setgeometry(qtcore.qrect( 220 , 330 , 71 , 71 )) self .b_bra_l.setobjectname( "b_bra_l" ) self .b_li = qtwidgets.qpushbutton( self .centralwidget) self .b_li.setgeometry(qtcore.qrect( 220 , 260 , 71 , 71 )) self .b_li.setobjectname( "b_li" ) self .b_add = qtwidgets.qpushbutton( self .centralwidget) self .b_add.setgeometry(qtcore.qrect( 10 , 260 , 71 , 71 )) self .b_add.setobjectname( "b_add" ) self .b_sub = qtwidgets.qpushbutton( self .centralwidget) self .b_sub.setgeometry(qtcore.qrect( 80 , 260 , 71 , 71 )) self .b_sub.setobjectname( "b_sub" ) self .b_mul = qtwidgets.qpushbutton( self .centralwidget) self .b_mul.setgeometry(qtcore.qrect( 150 , 260 , 71 , 71 )) self .b_mul.setobjectname( "b_mul" ) self .b_pow = qtwidgets.qpushbutton( self .centralwidget) self .b_pow.setgeometry(qtcore.qrect( 290 , 260 , 71 , 71 )) self .b_pow.setobjectname( "b_pow" ) self .b_pai = qtwidgets.qpushbutton( self .centralwidget) self .b_pai.setgeometry(qtcore.qrect( 290 , 400 , 71 , 71 )) self .b_pai.setobjectname( "b_pai" ) self .b_bra_r = qtwidgets.qpushbutton( self .centralwidget) self .b_bra_r.setgeometry(qtcore.qrect( 290 , 330 , 71 , 71 )) self .b_bra_r.setobjectname( "b_bra_r" ) self .l_hist = qtwidgets.qlistwidget( self .centralwidget) self .l_hist.setgeometry(qtcore.qrect( 380 , 10 , 256 , 601 )) self .l_hist.setobjectname( "l_hist" ) self .e_view = qtwidgets.qtextedit( self .centralwidget) self .e_view.setgeometry(qtcore.qrect( 10 , 10 , 351 , 231 )) font = qtgui.qfont() font.setfamily( "adobe caslon pro bold" ) font.setpointsize( 22 ) font.setbold(true) font.setweight( 75 ) self .e_view.setfont(font) self .e_view.setobjectname( "e_view" ) self .b_0 = qtwidgets.qpushbutton( self .centralwidget) self .b_0.setgeometry(qtcore.qrect( 80 , 540 , 71 , 71 )) self .b_0.setobjectname( "b_0" ) self .b_pt = qtwidgets.qpushbutton( self .centralwidget) self .b_pt.setgeometry(qtcore.qrect( 10 , 540 , 71 , 71 )) self .b_pt.setobjectname( "b_pt" ) self .b_del = qtwidgets.qpushbutton( self .centralwidget) self .b_del.setgeometry(qtcore.qrect( 220 , 470 , 71 , 71 )) self .b_del.setobjectname( "b_del" ) self .b_clc = qtwidgets.qpushbutton( self .centralwidget) self .b_clc.setgeometry(qtcore.qrect( 290 , 470 , 71 , 71 )) self .b_clc.setobjectname( "b_clc" ) mainwindow.setcentralwidget( self .centralwidget) self .menubar = qtwidgets.qmenubar(mainwindow) self .menubar.setgeometry(qtcore.qrect( 0 , 0 , 647 , 30 )) self .menubar.setobjectname( "menubar" ) mainwindow.setmenubar( self .menubar) self .statusbar = qtwidgets.qstatusbar(mainwindow) self .statusbar.setobjectname( "statusbar" ) mainwindow.setstatusbar( self .statusbar) self .retranslateui(mainwindow) qtcore.qmetaobject.connectslotsbyname(mainwindow) def retranslateui( self , mainwindow): _translate = qtcore.qcoreapplication.translate mainwindow.setwindowtitle(_translate( "mainwindow" , "mainwindow" )) self .b_1.settext(_translate( "mainwindow" , "1" )) self .b_2.settext(_translate( "mainwindow" , "2" )) self .b_3.settext(_translate( "mainwindow" , "3" )) self .b_6.settext(_translate( "mainwindow" , "6" )) self .b_4.settext(_translate( "mainwindow" , "4" )) self .b_5.settext(_translate( "mainwindow" , "5" )) self .b_8.settext(_translate( "mainwindow" , "8" )) self .b_9.settext(_translate( "mainwindow" , "9" )) self .b_7.settext(_translate( "mainwindow" , "7" )) self .b_eq.settext(_translate( "mainwindow" , "=" )) self .b_mod.settext(_translate( "mainwindow" , "%" )) self .b_bra_l.settext(_translate( "mainwindow" , "(" )) self .b_li.settext(_translate( "mainwindow" , "/" )) self .b_add.settext(_translate( "mainwindow" , "+" )) self .b_sub.settext(_translate( "mainwindow" , "-" )) self .b_mul.settext(_translate( "mainwindow" , "*" )) self .b_pow.settext(_translate( "mainwindow" , "^" )) self .b_pai.settext(_translate( "mainwindow" , "π" )) self .b_bra_r.settext(_translate( "mainwindow" , ")" )) self .b_0.settext(_translate( "mainwindow" , "0" )) self .b_pt.settext(_translate( "mainwindow" , "." )) self .b_del.settext(_translate( "mainwindow" , "删除" )) self .b_clc.settext(_translate( "mainwindow" , "清空" )) |
主逻辑代码
calc_main.py
|
from pyqt5.qtcore import * from pyqt5.qtgui import * from pyqt5.qtwidgets import * from calc_interface import ui_mainwindow import os,sys global e_view pluginspath = 'pyqt5/qt/plugins' if os.path.exists(pluginspath): #指定插件路径。源码运行时不会生效,打包后运行检测到路径,加载插件 qapplication.addlibrarypath(pluginspath) class mymainwindow(qmainwindow, ui_mainwindow): def forge_link( self ): self .b_0.clicked.connect( self .button_event( 0 )) self .b_1.clicked.connect( self .button_event( 1 )) self .b_2.clicked.connect( self .button_event( 2 )) 猜您喜欢
|