PyQt5 + QtDesigner 复刻 Windows 计算器 (二)

目标

画一个 <standard> 计算器草稿,并实现相关的基本功能

QtDesigner 画 <standard> 草稿 UI

用 QtDesigner 打开 calculator.ui,先在主窗口中插入 24 个 Pushbutton

 将 Pushbutton 上的 text 分别改名,同时按照功能修改每个 button 对应的对象名字。

percentButtonceButtoncButtondelButton
invButtonsquareButtonsqrtButtondevButton
sevenButtoneightButtonnineButtonmulButton
fourButtonfiveButtonsixButtonminusButton
oneButtontwoButtonthreeButtonaddButton
revButtonzeroButtondotButtonequalButton

选中所有按钮,点击工具栏中的栅格布局。选中所有按钮,将 sizePolicy 中的 “垂直策略” 设置为 preferred。调整栅格到合适大小。

 再次拖拽 6 个 pushButton 到 mainWindow,分别命名为 mcButton, mrButton, maddButton, mminusButton, msButton, mlistButton。然后选中这 6 个按钮,点击工具栏中的水平布局,同时调整布局到合适大小。再次选中这 6 个按钮,将 QPushbutton 属性设置为 Flat。

将 mainWindow 的 menubar 和 statusbar 删除

 在 mainWindow 中插入三个 pushButton 和一个 label。并用同样的方法设置水平布局。

 控件名称分别命名为 menuButton, modeLabel, topButton, historyButton。同时在 top 和 history 按钮中间插入一个 Horizontal Spacer。将以上 5 个控件的水平策略设置为 Fixed。同时调整 Horizontal Spacer 的长度到合适的数值。

在 mainWindow 中插入一个 lineEdit,命名为 digitDisplay。再插入第二个 lineEdit,命名为 digitUpDisplay:

在 mainWindow 中插入一个 QStackedWidget,并将 第一个 page 命名为 pageStd,第二个 page 命名为 pageProg。这一步是为了方便实现计算器切换 standard / programmer 模式。将刚刚画好的 standard 相关的视图拖拽到 pageStd 中 (即 除了 menu 栏之外的组件)。

 点击 centralWidget,将其设置为垂直布局:

 选择 pageStd,将 Layout 中的所有 Margin 设置为0,将 layoutStretch 设置为 0,2,1,9:

选择 centralWidget,将 layoutStretch 改为 1,12:

 最后,将 mainWindow 最小 size 改为 470*700:

 现在 standard 计算器 UI 的草稿就完成了。生成 UI 对应的 py 文件并运行主程序:

pyuic5.exe -o ui_calculator.py calculator.ui

python ./calculator.py

 运行结果:

 功能实现放到下一篇吧.. UI 源码贴在下面。复制之后保存为 .ui 文件就可以用了。

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>470</width>
    <height>700</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="minimumSize">
   <size>
    <width>470</width>
    <height>700</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <property name="sizePolicy">
    <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
     <horstretch>0</horstretch>
     <verstretch>0</verstretch>
    </sizepolicy>
   </property>
   <property name="maximumSize">
    <size>
     <width>16777215</width>
     <height>16777215</height>
    </size>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout_2" stretch="1,12">
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout_2">
      <item>
       <widget class="QPushButton" name="menuButton">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Minimum">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="maximumSize">
         <size>
          <width>16777215</width>
          <height>40</height>
         </size>
        </property>
        <property name="text">
         <string>menu</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QLabel" name="modeLabel">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Minimum">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="maximumSize">
         <size>
          <width>16777215</width>
          <height>40</height>
         </size>
        </property>
        <property name="text">
         <string>Standard</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="topButton">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Minimum">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="maximumSize">
         <size>
          <width>16777215</width>
          <height>40</height>
         </size>
        </property>
        <property name="text">
         <string>top</string>
        </property>
       </widget>
      </item>
      <item>
       <spacer name="horizontalSpacer">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Minimum">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="maximumSize">
         <size>
          <width>16777215</width>
          <height>40</height>
         </size>
        </property>
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeType">
         <enum>QSizePolicy::Preferred</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>400</width>
          <height>17</height>
         </size>
        </property>
       </spacer>
      </item>
      <item>
       <widget class="QPushButton" name="historyButton">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Minimum">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="maximumSize">
         <size>
          <width>16777215</width>
          <height>40</height>
         </size>
        </property>
        <property name="text">
         <string>history</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <widget class="QStackedWidget" name="calcModeView">
      <property name="currentIndex">
       <number>0</number>
      </property>
      <widget class="QWidget" name="pageStd">
       <layout class="QVBoxLayout" name="verticalLayout" stretch="0,2,1,9">
        <property name="spacing">
         <number>0</number>
        </property>
        <property name="leftMargin">
         <number>0</number>
        </property>
        <property name="topMargin">
         <number>0</number>
        </property>
        <property name="rightMargin">
         <number>0</number>
        </property>
        <property name="bottomMargin">
         <number>0</number>
        </property>
        <item>
         <widget class="QLineEdit" name="digitUpDisplay"/>
        </item>
        <item>
         <widget class="QLineEdit" name="digitDisplay">
          <property name="sizePolicy">
           <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
         </widget>
        </item>
        <item>
         <layout class="QHBoxLayout" name="horizontalLayout_3">
          <property name="spacing">
           <number>0</number>
          </property>
          <property name="sizeConstraint">
           <enum>QLayout::SetMinimumSize</enum>
          </property>
          <property name="leftMargin">
           <number>0</number>
          </property>
          <property name="topMargin">
           <number>9</number>
          </property>
          <item>
           <widget class="QPushButton" name="mcButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="maximumSize">
             <size>
              <width>16777215</width>
              <height>16777215</height>
             </size>
            </property>
            <property name="text">
             <string>MC</string>
            </property>
            <property name="flat">
             <bool>true</bool>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="mrButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="maximumSize">
             <size>
              <width>16777215</width>
              <height>16777215</height>
             </size>
            </property>
            <property name="text">
             <string>MR</string>
            </property>
            <property name="flat">
             <bool>true</bool>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="maddButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="maximumSize">
             <size>
              <width>16777215</width>
              <height>16777215</height>
             </size>
            </property>
            <property name="text">
             <string>M+</string>
            </property>
            <property name="flat">
             <bool>true</bool>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="mminusButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="maximumSize">
             <size>
              <width>16777215</width>
              <height>16777215</height>
             </size>
            </property>
            <property name="text">
             <string>M-</string>
            </property>
            <property name="flat">
             <bool>true</bool>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="mlistButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="maximumSize">
             <size>
              <width>16777215</width>
              <height>16777215</height>
             </size>
            </property>
            <property name="text">
             <string>Mlist</string>
            </property>
            <property name="flat">
             <bool>true</bool>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="msButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="maximumSize">
             <size>
              <width>16777215</width>
              <height>16777215</height>
             </size>
            </property>
            <property name="text">
             <string>MS</string>
            </property>
            <property name="flat">
             <bool>true</bool>
            </property>
           </widget>
          </item>
         </layout>
        </item>
        <item>
         <layout class="QGridLayout" name="gridLayout_2">
          <item row="0" column="1">
           <widget class="QPushButton" name="ceButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>CE</string>
            </property>
           </widget>
          </item>
          <item row="3" column="1">
           <widget class="QPushButton" name="fiveButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>5</string>
            </property>
           </widget>
          </item>
          <item row="2" column="1">
           <widget class="QPushButton" name="eightButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>8</string>
            </property>
           </widget>
          </item>
          <item row="0" column="0">
           <widget class="QPushButton" name="percentButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>%</string>
            </property>
           </widget>
          </item>
          <item row="2" column="0">
           <widget class="QPushButton" name="sevenButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>7</string>
            </property>
           </widget>
          </item>
          <item row="3" column="2">
           <widget class="QPushButton" name="sixButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>6</string>
            </property>
           </widget>
          </item>
          <item row="0" column="3">
           <widget class="QPushButton" name="delButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>del</string>
            </property>
           </widget>
          </item>
          <item row="1" column="3">
           <widget class="QPushButton" name="devButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>/</string>
            </property>
           </widget>
          </item>
          <item row="5" column="3">
           <widget class="QPushButton" name="equalButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>=</string>
            </property>
           </widget>
          </item>
          <item row="3" column="3">
           <widget class="QPushButton" name="minusButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>-</string>
            </property>
           </widget>
          </item>
          <item row="1" column="0">
           <widget class="QPushButton" name="invButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>1/x</string>
            </property>
           </widget>
          </item>
          <item row="5" column="2">
           <widget class="QPushButton" name="dotButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>.</string>
            </property>
           </widget>
          </item>
          <item row="4" column="1">
           <widget class="QPushButton" name="twoButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>2</string>
            </property>
           </widget>
          </item>
          <item row="4" column="2">
           <widget class="QPushButton" name="threeButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>3</string>
            </property>
           </widget>
          </item>
          <item row="2" column="3">
           <widget class="QPushButton" name="mulButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>*</string>
            </property>
           </widget>
          </item>
          <item row="1" column="1">
           <widget class="QPushButton" name="squareButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>x^2</string>
            </property>
           </widget>
          </item>
          <item row="2" column="2">
           <widget class="QPushButton" name="nineButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>9</string>
            </property>
           </widget>
          </item>
          <item row="1" column="2">
           <widget class="QPushButton" name="sqrtButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>sqrt(x)</string>
            </property>
           </widget>
          </item>
          <item row="5" column="0">
           <widget class="QPushButton" name="revButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>+/-</string>
            </property>
           </widget>
          </item>
          <item row="0" column="2">
           <widget class="QPushButton" name="cButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>C</string>
            </property>
           </widget>
          </item>
          <item row="5" column="1">
           <widget class="QPushButton" name="zeroButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>0</string>
            </property>
           </widget>
          </item>
          <item row="4" column="0">
           <widget class="QPushButton" name="oneButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>1</string>
            </property>
           </widget>
          </item>
          <item row="3" column="0">
           <widget class="QPushButton" name="fourButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>4</string>
            </property>
           </widget>
          </item>
          <item row="4" column="3">
           <widget class="QPushButton" name="addButton_2">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>+</string>
            </property>
           </widget>
          </item>
         </layout>
        </item>
       </layout>
      </widget>
      <widget class="QWidget" name="pageProg"/>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

 

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyCharm 2020.1是集成开发环境(IDE),主要用于编写和调试Python代码。它具有许多强大的功能,例如代码自动补全、调试器、版本控制集成等,使开发人员更加高效地编写代码。 PyQt一个用于创建图形用户界面(GUI)应用程序的Python库。它是对Qt应用程序框架的Python绑定,提供了丰富的界面组件和功能。通过PyQt开发人员可以轻松地创建跨平台的GUI应用程序。 Qt DesignerQt开发工具包的一部分,它是一个可视化设计工具,用于创建Qt应用程序的用户界面。它提供了丰富的界面组件和布局选项,开发人员可以通过拖放和设置属性来设计界面Qt Designer还可以将设计界面转换为Python代码,以便在PyQt中使用。 在PyCharm 2020.1中使用PyQtQt Designer的案例可以是创建一个简单的GUI应用程序。首先,我们可以在PyCharm中创建一个新的PyQt项目,然后使用Qt Designer设计应用程序的界面。在Qt Designer中,我们可以添加按钮、标签、文本框等界面组件,并设置它们的属性和布局。 然后,我们可以将设计好的界面保存为.ui文件,并将其转换为Python代码。在PyCharm中,我们可以使用PyQt的工具来将.ui文件转换为.py文件,并在代码中导入生成的Python模块。 接下来,我们可以在PyCharm中编写代码来处理界面组件的事件,例如按钮的点击事件。通过PyQt提供的信号与槽机制,我们可以连接界面组件的信号和事件处理函数,以实现交互逻辑。 最后,我们可以在PyCharm中运行项目,测试和调试应用程序的功能。PyCharm提供了调试器和代码分析工具,帮助我们找出潜在的错误并进行修复。 总之,使用PyCharm 2020.1、PyQtQt Designer,我们可以方便地开发跨平台的GUI应用程序,通过可视化设计界面和编写Python代码,使应用程序更加易于维护和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值