基于PySide6的简易单位转换器

2023-12-20

制作一个简易的长度和重量单位转换器,在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文件即可得到如下的数值转换器了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于PySide6的简易单位转换器 的相关文章

  • 使用 jquery 通配符检查 cookie 名称

    我有一个生成动态 cookie 的表单 例如 webform 62 1234356 62 1234356 可以是任意数字 我需要使用一些通配符检查来检查名称以 webform 开头的 cookie 是否存在 下面不起作用 if cookie
  • 覆盖函数(例如“警报”)并调用原始函数?

    我想用调用原始版本的新版本覆盖 Javascript 内置函数 类似于用调用的版本覆盖类上的方法 super有多种语言版本 我怎样才能做到这一点 例如 window alert function str do something addit
  • 如何创建自定义元素扩展类的新实例

    我正在尝试以下示例谷歌开发者网站 https developers google com web fundamentals getting started primers customelements extendhtml我收到错误 Typ
  • 将多个 isinstance 检查转换为结构模式匹配

    我想转换此现有代码以使用模式匹配 if isinstance x int pass elif isinstance x str x int x elif isinstance x float Decimal x round x else r
  • 为 Meteor 数据创建编号列表

    有没有办法获取 Meteor 集合中项目的编号列表的 编号 我知道我可以在 html 中做到这一点 但我觉得如果我可以在 spacebars 中放置一些东西 那么样式会更容易 如果我可以使用更好的术语 请告诉我 像这样的东西 前 20 部电
  • RuntimeError:模型类 django_messages.models.Message 未声明显式 app_label 并且不在 INSTALLED_APPS 中的应用程序中

    我正在尝试使用https github com arneb django messages https github com arneb django messages打包我的消息传递内容并尝试了以下操作 pip install git h
  • 通过多个回调优雅地传递“点击事件”

    当未登录的用户单击给定的按钮时 我想停止该事件 收集他的 oauth 收集他的电子邮件 如果我没有 然后执行该事件 我想用 javascript 来做所有事情 因为这会让事情变得更加简单 这就是我执行它的方式 我有两个问题 有没有更优雅的方
  • python 中的异步编程

    python 中有异步编程的通用概念吗 我可以为一个函数分配一个回调 执行它并立即返回主程序流 无论该函数的执行需要多长时间吗 您所描述的 主程序流程在另一个函数执行时立即恢复 不是通常所说的 异步 又名 事件驱动 编程 而是 多任务 又名
  • Python:如何使用生成器来避免 sql 内存问题

    我有以下方法来访问 mysql 数据库 并且查询在服务器中执行 我无权更改有关增加内存的任何内容 我对生成器很陌生 并开始阅读更多有关它的内容 并认为我可以将其转换为使用生成器 def getUNames self globalUserQu
  • 如何查看网站浏览者的操作系统?

    我运行的是 Ubuntu 8 04 最近在访问网站时收到以下错误 请使用运行 Windows 98 2000 Me NT 或 XP 的计算机返回 www site com 网站如何知道我正在运行哪个操作系统 是仅通过 javascript
  • Python写入dbf数据时出错

    我得到这个错误 DbfError unable to modify fields individually except in with or Process 如何修复它 这是我的code with dbf Table aa dbf as
  • Numba jitclass 不适用于 python 列表

    我在用python 3 6 and numba 0 36 这个问题有一个sister https stackoverflow com questions 48159360 numba custom stack class and pop f
  • 如何在 Flask 中获取 POSTed JSON?

    我正在尝试使用 Flask 构建一个简单的 API 现在我想在其中读取一些 POSTed JSON 我使用 Postman Chrome 扩展进行 POST 我 POST 的 JSON 很简单 text lalala 我尝试使用以下方法读取
  • 使用多行选项和编码选项读取 CSV

    在 azure Databricks 中 当我使用以下命令读取 CSV 文件时multiline true and encoding SJIS 似乎编码选项被忽略了 如果我使用multiline选项 Spark 使用默认值encoding那
  • 如何按字母顺序排序并先小写排序

    如何获得以下排序的结果Food to Eat然后是 食物123 显然 第二个较低的 o 应该将 要吃的食物 带到排序后的第一个项目中 我很惊讶这个问题不容易通过谷歌找到答案 这个壮举没有包含在 javascript 标准中也让我感到惊讶 F
  • Jquery 两个字段的时间差(以小时为单位)

    我的表单中有两个字段 用户可以在其中选择输入时间 start time end time 我想在更改这些字段时重新计算另一个字段的值 我想做的是获取两次之间的小时数 例如 如果我的开始时间为 5 30 结束时间为 7 50 我想将结果 2
  • Django:在单独的线程中使用相同的测试数据库

    我正在使用具有以下数据库设置的测试数据库运行 pytests DATABASES default ENGINE django db backends postgresql psycopg2 NAME postgres USER someth
  • 使用 javascript Array reduce() 方法有什么真正的好处吗?

    reduce 方法的大多数用例都可以使用 for 循环轻松重写 对 JSPerf 的测试表明 reduce 通常会慢 60 75 具体取决于每次迭代内执行的操作 除了能够以 函数式风格 编写代码之外 还有什么真正的理由使用reduce 吗
  • 透视包含字符串的 Pandas Dataframe - “没有要聚合的数字类型”错误

    关于此错误有很多问题 但环顾四周后 我仍然无法找到 解决解决方案 我正在尝试用字符串旋转数据框 以使一些行数据变成列 但到目前为止还没有成功 我的 df 的形状
  • WooCommerce 使用 AJAX 设置购物车数量?

    我已经为此绞尽脑汁好几天了 需要一些指导 我正在为 WooCommerce 网站完全从头开始制作自定义主题 现在我正在尝试让购物车功能正常工作 我一直试图使用按钮 来更新购物车中产品的数量 对我来说问题似乎是WC 我在functions p

随机推荐