使用 QMainWindow 中的变量名调用 Python 函数

问题描述

我正在尝试找到一种有效且安全的方式来根据用户输入的交易名称调用不同的函数。有 100 多个不同的交易。 100“IF”可以完成这项工作,但是,我想找到一种更有效的方式来调用事务。 “eval”可以做到这一点,但我读到不应该使用它,因为用户可以输入任何交易名称

from operator import methodcaller
import  sys
from    PyQt5.QtWidgets     import (QMainWindow,qtoolbar,QLineEdit,QLabel,QApplication)
def one():
        print ("1")

def two():
        print ("2")

def three():
        print("3")

class main_menu(QMainWindow):
    def __init__(self,parent=None):
        super().__init__(parent)

        menuBar = self.menuBar()
        self.ToolBar = qtoolbar()
        self.ToolBar.setMovable(False)
        self.addToolBar(self.ToolBar)
        self.tcode = QLineEdit(maxLength=5)
        self.tcode.returnpressed.connect(self.tcode_action) 
        self.ToolBar.addWidget(QLabel("  Transaction : "))
        self.ToolBar.addWidget(self.tcode)

    def tcode_action(self):
##        if self.tcode.text() == "one":
##                one()
##        if self.tcode.text() == "two":
##                two()
##        if self.tcode.text() == "three":
##                three()

##        eval(self.tcode.text()+"()")        

def main(args):
    app                 = QApplication(sys.argv)
    mm = main_menu()
    mm.show()
    sys.exit(app.exec_())
if __name__=="__main__":
    main(sys.argv)


解决方法

在python中可以通过globals()访问全局变量。 您可以使用:

def tcode_action(self):
    fn = globals().get(self.tcode.text())
    if fn:
        fn()
    else:
        print("invalid input")
,

一种选择是使用 QComboBox 来限制函数集。您还可以使用 Enum 枚举有效函数。

from enum import Enum
from functools import partial


# function definitions
def fcn_1( x ):
  print( 'F1' )


def fcn_2( x ):
  print( 'F2' )


# valid functions Enum
# Must wrap functions in partial so they are not defined as methods.
# See below post for more details.
class ValidFunctions( Enum ):
  Echo = partial( fcn_1 )
  Increment = partial( fcn_2 )


# function selection ComboBox
cb = QComboBox()
cb.addItem( 'Echo' )
cb.AddItem( 'Increment' )


# connecting the signal
def call_function():
  fcn = ValidFunctions[ cb.currentText() ]
  fcn()


cb.currentIndexChanged.connect( call_function )

注意:我没有调试过这段代码。

How to define enum values that are functions

,

我现在将使用此代码执行此操作:

def tcode_action(self):
    try:
            func = getattr(self,self.tcode.text())
            func()
    except:
            pass

对此有何评论?