Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 推荐处理单位转换的方法_Python_Pyqt4_Data Conversion - Fatal编程技术网

Python 推荐处理单位转换的方法

Python 推荐处理单位转换的方法,python,pyqt4,data-conversion,Python,Pyqt4,Data Conversion,我正在用pyqt4和python制作GUI。现在我有一个QLineEdit和QComboBox,其中QLineEdit显示值,QComboBox可用于更改单位。我使用信号和插槽为用户处理实时单位/值反馈,但我在理解如何以编程方式处理这些值时遇到了问题,因为我需要它们都是标准单位。这是我到目前为止得到的,组合框、行、编辑列表是一个列表列表,我将组合框和行列表包装在一起 class UnitConverterSignaler(QtCore.QObject): def __init__(se

我正在用pyqt4和python制作GUI。现在我有一个QLineEdit和QComboBox,其中QLineEdit显示值,QComboBox可用于更改单位。我使用信号和插槽为用户处理实时单位/值反馈,但我在理解如何以编程方式处理这些值时遇到了问题,因为我需要它们都是标准单位。这是我到目前为止得到的,组合框、行、编辑列表是一个列表列表,我将组合框和行列表包装在一起

class UnitConverterSignaler(QtCore.QObject):

    def __init__(self, combo_box_line_edit_list):
        super(QtCore.QObject, self).__init__()
        self.combo_box_line_edit_list = combo_box_line_edit_list

        self.combo_box_list = [line_edit_combo_box[0] for line_edit_combo_box in combo_box_line_edit_list]

        for combo_box, line_edit in self.combo_box_line_edit_list:
            combo_box.currentIndexChanged['QString'].connect(line_edit.convert_units)
            line_edit.store_unit_state(combo_box.currentText())
            line_edit.standard_unit = combo_box.itemText(1)


    def convert_to_standard(self):
        for combo_box in self.combo_box_list:
            combo_box.setCurrentIndex(0)

    def convert_to_international(self):
        for combo_box in self.combo_box_list:
            combo_box.setCurrentIndex(1)

    def toggle_unit_conversion(self, hold_line_values_steady):
        for combo_box in self.combo_box_list:
            if hold_line_values_steady:
                combo_box.do_not_convert_units_on_change()
            else:
                combo_box.convert_units_on_change()

    def convert_units_on_change(self):
        """
        Changes the value of the line edit each time the combo box is changed
        """
        for combo_box, line_edit in self.combo_box_line_edit_list:
            combo_box.currentIndexChanged['QString'].connect(line_edit.convert_units)
            combo_box.currentIndexChanged['QString'].disconnect(line_edit.store_unit_state)

    def do_not_convert_units_on_change(self):
        """
        Holds the line edit value constant in spite of combo box changes
        """
        for combo_box, line_edit in self.combo_box_line_edit_list:
            combo_box.currentIndexChanged['QString'].disconnect(line_edit.convert_units)
            combo_box.currentIndexChanged['QString'].connect(line_edit.store_unit_state)
实例化并在另一个类中使用

self.lockCellCheckBox.toggled.connect(self.unit_converter_signaler.toggle_unit_conversion)
self.internationalPushButton.clicked.connect(self.unit_converter_signaler.convert_to_international)
self.standardPushButton.clicked.connect(self.unit_converter_signaler.convert_to_standard)
我还对QLineEdit进行了猴子补丁,而不是子类化,因此我可以使用QtDesigner进行快速更改

# monkey patch slot onto line_edit
def convert_units(line_edit, end_unit):
    converted_unit_value = line_edit.unit_registry.convert(float(line_edit.text()), line_edit.stored_unit_state, str(end_unit))
    line_edit.setText(str(converted_unit_value))
    line_edit.stored_unit_state = str(end_unit)

# monkey patch slot onto line_edit
def store_unit_state(line_edit, unit):
    line_edit.stored_unit_state = str(unit)

在我的主程序中获取标准单位的最通用方法是为UnitConverter中的每个组合框/行编辑创建一个信号吗

根据我到目前为止的理解:您有许多组合框/行编辑对,输入的值应始终转换为标准单位(例如,显示在第三个QLabel或其他任何东西上)

在我的主程序中获取标准单位的最通用方法是为UnitConverter中的每个组合框/行编辑创建一个信号吗

不,你不必。python(尤其是pyqt)中的插槽可以是任何可调用对象。可调用对象是实现了方法
的对象。
因此,我建议您创建一个类,该类将相关对象作为构造函数中的参数,并在
\uuuu调用\uuuuu(self)
中对其进行更改。大概是这样的:

class ConverterSignal:
    def __init__(whatever_you_want_to_refer_to):
        self.whatever_you_want_to_refer_to = whatever_you_want_to_refer_to

    def __call(self)__:
        """ Here you can refer to whatever_you_want_to_refer_to and do whatever you want with it """
连接如下所示(以组合框为例):


这里创建了一个类
ConverterSignal
的实例,如果发出相应的信号,就会调用它。

这就是我目前理解的地方。我正在考虑将来使用委托或代理模型。很抱歉花了这么长时间才接受这个答案
self.connect(combo_box, QtCore.SIGNAL('activated(int)'), ConverterSignal(whatever_you_want_to_refer_to))