基于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的简易单位转换器 的相关文章

  • 关于具有自定义损失的 3 输出 ANN 的加权

    我正在尝试定义一个自定义损失函数 它在回归模型中接收 3 个输出变量 def custom loss y true y pred y true c K cast y true float32 Shape batch size 3 y pre
  • 按升序对数字字符串列表进行排序

    我创建了一个SQLite https en wikipedia org wiki SQLite数据库有一个存储温度值的表 第一次将温度值按升序写入数据库 然后 我将数据库中的温度值读入列表中 然后将该列表添加到组合框中以选择温度 效果很好
  • Firefox 上的 jquery 焦点未设置

    我想将焦点设置到我的文本区域 以下是我的代码 this textInput val show focus 但它不起作用 实际上 当我按下鼠标按钮时 它会出现 但是当我松开鼠标时 它会从文本区域中删除 因此 经过大量搜索后 我发现 setTi
  • 如何在 tkinter 后台运行函数[重复]

    这个问题在这里已经有答案了 我是 GUI 编程新手 我想用 tkinter 编写一个 Python 程序 我想要它做的就是在后台运行一个可以通过 GUI 影响的简单函数 该函数从 0 计数到无穷大 直到按下按钮为止 至少这是我想要它做的 但
  • Ajax 函数在重定向后不保存滚动位置

    正如标题所述 我编写了一个 ajax 函数 该函数应该滚动到用户在重定向之前所在的位置 我写了一个alert对于测试场景 它确实触发了 但滚动不断回到顶部 我在这里做错了什么 JavaScript ajax type GET url Adm
  • 如果突出显示一个单词并且用户单击连接单词,则同时突出显示两个单词

    我最近发布了一个question https stackoverflow com questions 34963610 how can i highlight a word term quicker and smarter寻求一种更智能地突
  • Jquery Ajax 调用返回 403 状态

    我有一个 jquery Ajax 调用来实现会话的 keepalive 这个 keepAlive 方法将每 20 分钟调用一次 function keepAlive ajax type POST url KeepAliveDummy asp
  • Kendo 刷新 (DropDownList.refresh()) 不起作用错误未定义

    我试图在另一个 DropDownList 更改后刷新下拉列表 但 Refresh 方法未定义错误正在升级 我尝试再次读取数据源 它显示它正在加载 但数据仍然相同 帮助解决这个问题请 Code DropDownList1 change fun
  • numpy 向量化而不是 for 循环

    我用 Python 写了一些代码 运行良好 但速度很慢 我认为是由于 for 循环 我希望可以使用 numpy 命令加速以下操作 让我定义目标 假设我有一个 2D numpy 数组all CMs尺寸row x col 例如考虑一个6x11数
  • 在基本 Tensorflow 2.0 中运行简单回归

    我正在学习 Tensorflow 2 0 我认为在 Tensorflow 中实现最基本的简单线性回归是一个好主意 不幸的是 我遇到了几个问题 我想知道这里是否有人可以提供帮助 考虑以下设置 import tensorflow as tf 2
  • IE9 中的无效字符 DOM 异常

    以下这段 JS 曾经在 IE8 中工作 现在在 IE9 中失败 document createElement 我收到以下异常 SCRIPT5022 DOM 异常 INVALID CHARACTER ERR 5 上面这段代码是不是不符合标准呢
  • Python:如何“杀死”类实例/对象?

    我希望 Roach 类在达到一定量的 饥饿 时 死亡 但我不知道如何删除该实例 我的术语可能有误 但我的意思是 窗户上有大量 蟑螂 我希望特定的蟑螂完全消失 我会向您展示代码 但它很长 我将蟑螂类添加到策划者类蟑螂种群列表中 一般来说 每个
  • 根据标签位置计算 Pandas DataFrame 的索引

    我正在尝试计算标签的索引Pandas https pandas pydata org DataFrame在每一列中 基本上我有以下内容DataFrame d col1 label1 label2 label3 col2 label2 lab
  • 如何通过 API Gateway 使用事件调用类型调用 Lambda 函数?

    文件说 默认情况下 Invoke API 采用 RequestResponse 调用类型 您可以选择通过将 Event 指定为 InitationType 来请求异步执行 因此 我可以发送到我的函数 python 的就是到处都是 Inspi
  • 如何在react.js中将/n替换为换行符?

    我正在尝试更换每一个 n to a br tag in ReactJS In my note note对象有一个包含多个的字符串 n in it 示例注释 注释 test ntest ntest 我尝试过的ReactJS note note
  • 如何使用 Matplotlib 可视化标量二维数据?

    所以我有一个网格网格 矩阵 X 和 Y 以及标量数据 矩阵 Z 我需要将其可视化 最好是一些 2D 图像 在各点处带有颜色 显示 Z 值 我做了一些研究 但没有找到任何能完全满足我想要的效果的东西 pyplot imshow Z 看起来不错
  • 无法将 librosa 与 python 3 一起使用

    我已经在 Windows 上的 ubuntu 子系统上使用 pip3 正确安装了 librosa 但是当我尝试执行像这样的简单程序时 import librosa data sr librosa load sound mp3 print d
  • 使用 javascript 从亚马逊 URL 中抓取 ASIN

    假设我有一个像这样的亚马逊产品 URL http www amazon com Kindle Wireless Reading Display Generation dp B0015T963C ref amb link 86123711 2
  • Docker Python 脚本找不到文件

    我已经成功构建了一个 Docker 容器 并将应用程序的文件复制到 Dockerfile 中的容器中 但是 我正在尝试执行引用输入文件 在 Docker 构建期间复制到容器中 的 Python 脚本 我似乎无法弄清楚为什么我的脚本告诉我它无
  • 尽管 getBoundingClientRect() 是假的,但如何将事件坐标转换为 SVG 坐标?

    我正在尝试根据鼠标的位置在 SVG 元素上动态绘制内容 不幸的是 我很难将 mousemove 事件中的鼠标坐标转换为 SVG 元素的坐标空间 这是我一直在测试的一个有缺陷的函数 CylinderDemo prototype handleM

随机推荐