基于PySide6的简易单位转换器

制作一个简易的长度和重量单位转换器,在qtdesigner中设计如下的界面

下图为全部控件和整体布局。

也可以直接复制下面代码,下面是整个ui界面的.ui文件,将其在vscode中新建后使用工具进行编译生成py文件即可,由于上面控件中计算按钮处使用了中文所以存在一小处乱码,手动调整即可。

<?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>342</width>
    <height>268</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <layout class="QGridLayout" name="gridLayout">
     <item row="3" column="0">
      <widget class="QLineEdit" name="oneinput">
       <property name="minimumSize">
        <size>
         <width>40</width>
         <height>40</height>
        </size>
       </property>
       <property name="styleSheet">
        <string notr="true">.QCombox{
	border-radius:10%;
	border:1px solid black;
}
.QLineEdit{
	border-radius:10%;
	border:1px solid black;
}
.QCombox:hover{
	background-color:rgb(170,255,255);
}
.QLineEdit:hover{
	background-color:rgb(170,255,255);
}
</string>
       </property>
      </widget>
     </item>
     <item row="4" column="1">
      <widget class="QComboBox" name="twoinputchoose">
       <property name="minimumSize">
        <size>
         <width>150</width>
         <height>30</height>
        </size>
       </property>
      </widget>
     </item>
     <item row="2" column="0">
      <widget class="QComboBox" name="datatype">
       <property name="minimumSize">
        <size>
         <width>40</width>
         <height>25</height>
        </size>
       </property>
      </widget>
     </item>
     <item row="0" column="0" colspan="2">
      <widget class="QLabel" name="origin_data">
       <property name="minimumSize">
        <size>
         <width>100</width>
         <height>30</height>
        </size>
       </property>
       <property name="font">
        <font>
         <pointsize>16</pointsize>
        </font>
       </property>
       <property name="styleSheet">
        <string notr="true">color:rgb(132, 132, 132)</string>
       </property>
       <property name="text">
        <string>0=</string>
       </property>
      </widget>
     </item>
     <item row="3" column="1">
      <widget class="QComboBox" name="oneinputchoose">
       <property name="minimumSize">
        <size>
         <width>150</width>
         <height>30</height>
        </size>
       </property>
      </widget>
     </item>
     <item row="1" column="0" colspan="2">
      <widget class="QLabel" name="trans_data">
       <property name="minimumSize">
        <size>
         <width>140</width>
         <height>40</height>
        </size>
       </property>
       <property name="font">
        <font>
         <pointsize>30</pointsize>
        </font>
       </property>
       <property name="text">
        <string>0=</string>
       </property>
      </widget>
     </item>
     <item row="4" column="0">
      <widget class="QLineEdit" name="twoinput">
       <property name="minimumSize">
        <size>
         <width>40</width>
         <height>40</height>
        </size>
       </property>
       <property name="styleSheet">
        <string notr="true">.QCombox{
	border-radius:10%;
	border:1px solid black;
}
.QLineEdit{
	border-radius:10%;
	border:1px solid black;
}
.QCombox:hover{
	background-color:rgb(170,255,255);
}
.QLineEdit:hover{
	background-color:rgb(170,255,255);
}
</string>
       </property>
      </widget>
     </item>
    </layout>
   </item>
   <item>
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>璁$畻</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

下面是整个ui的实现逻辑代码,完成了米,千米,厘米,分米。常用长度单位以及克,千克,斤的数值转化。

#coding=gbk
from PySide6.QtWidgets import QWidget,QMainWindow,QApplication,QVBoxLayout,QComboBox,QCheckBox
from Ui_transdata import Ui_Form

class Mywindow(QWidget,Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.lengthVar = {'米':100,'千米':100000,'厘米':1,'分米':10}
        self.weightVar = {'克':1,'千克':1000,'斤':500}   
        self.TypeDict={'长度':self.lengthVar,'质量':self.weightVar}
        self.datatype.addItems(self.TypeDict.keys())
        self.oneinputchoose.addItems(self.lengthVar.keys())
        self.twoinputchoose.addItems(self.lengthVar.keys())
        self.bind()
    def bind(self):
        self.datatype.currentTextChanged.connect(self.typeChanged)
        self.pushButton.clicked.connect(self.cal)
    def cal(self):
        bigtype = self.datatype.currentText()
        value = self.oneinput.text()
        if value == '':
            return
        currentType = self.oneinputchoose.currentText()
        transType = self.twoinputchoose.currentText()
        
        standardization = float(value)*self.TypeDict[bigtype][currentType]
        result = standardization/self.TypeDict[bigtype][transType]
        self.origin_data.setText(f'{value}{currentType} =')
        self.trans_data.setText(f'{result}{transType}')
        self.twoinput.setText(str(result))

    def typeChanged(self,text):
        self.oneinputchoose.clear()
        self.twoinputchoose.clear()
        self.oneinputchoose.addItems(self.TypeDict[text].keys())
        self.twoinputchoose.addItems(self.TypeDict[text].keys())


if __name__=='__main__':
    app = QApplication()
    window = Mywindow()
    window.show()
    app.exec()

点击运行py文件即可得到如下的数值转换器了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白研究僧学习嵌入式

谢谢您的鼓励,会持续努力滴!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值