2015-10-29 45 views
0

我是一個初學者,當談到python宇宙中的GUI時,我試圖爲sin(x),cos(x),tan(x)編寫一個簡單的函數計算器。這是我的代碼。簡單的PyQt函數計算器

import matplotlib.pyplot as plt 
import numpy as np 
import sys 
from PyQt4 import QtGui, QtCore 


class Form(QtGui.QWidget) : 
    def __init__(self): 
     super(Form, self).__init__() 

     layout = QtGui.QVBoxLayout(self) 
     combo = QtGui.QComboBox() 
     combo.addItem("Sin") 
     combo.addItem("Cos") 
     combo.addItem("Tan") 

     parameter = QtGui.QLineEdit("np.linspace(lower,upper,dx)") 
     parameter.selectAll() 
     output = QtGui.QLineEdit("Output (Press Enter)") 
     output.selectAll() 

     layout.addWidget(combo) 
     layout.addWidget(parameter) 
     layout.addWidget(output) 

     self.setLayout(layout) 
     combo.setFocus() 
     self.connect(output, QtCore.SIGNAL("returnPressed()"), self.updateUI) 
     self.setWindowTitle("Function Evaluator") 

    def updateUI(self) : 
     x = float(self.parameter_edit.text()) 
     f = str(eval(str(self.function_edit.text()))) 
     self.output_edit.setText(f) 


app = QtGui.QApplication(sys.argv) 
form = Form() 
form.show() 
app.exec_() 

我該如何去做這件事?我有一個特定功能的下拉菜單,但並不真正知道如何對特定的下拉菜單功能進行評估。或者我怎麼去實際評估函數本身的x輸入,並讓它在我的updateUI方法中輸出。

回答

1

QComboBox's項目可以有一個名字和內容:

qCombo.addItem('Sin', 'np.sin') 

這裏的一種方式來獲得所選擇項的內容:

content = qCombo.itemData(qCombo.currentIndex()) 

記住,在python2返回的內容被包裹在QVariant;我們必須手動解開它:

content = content.toString() 

所以,你的代碼可能如下所示:

import matplotlib.pyplot as plt 
import numpy as np 
import sys 
from PyQt4 import QtGui, QtCore 


class Form(QtGui.QWidget): 
    def __init__(self): 
     super(Form, self).__init__() 

     self.func_selector = QtGui.QComboBox() 
     self.func_selector.setFocus() 
     self.func_selector.addItem("Sin", 'np.sin') 
     self.func_selector.addItem("Cos", 'np.cos') 
     self.func_selector.addItem("Tan", 'np.tan') 

     self.parameter_edit = QtGui.QLineEdit("np.linspace(lower, upper, n)") 

     self.output_edit = QtGui.QLineEdit("Output (Press Enter)") 
     self.output_edit.returnPressed.connect(self.updateUI) 

     layout = QtGui.QVBoxLayout(self) 
     layout.addWidget(self.func_selector) 
     layout.addWidget(self.parameter_edit) 
     layout.addWidget(self.output_edit) 

     self.setLayout(layout) 
     self.setWindowTitle("Function Evaluator") 

    def updateUI(self): 
     # A dictionary of local variables that can be used by eval() 
     locs = {'lower': 0, 'upper': 2*np.pi, 'n': 10} 

     x = self.parameter_edit.text() 

     # Get a content of the selected QComboBox's item 
     f = self.func_selector.itemData(self.func_selector.currentIndex()) 
     # In python2 a QComboBox item's content is wrapped in QVariant, so we must unwrap it: 
     if sys.version_info.major == 2: 
      f = f.toString() 

     y = eval('{}({})'.format(f, x), None, locs) 
     self.output_edit.setText(str(y)) 

     # if the function returns multiple values, show a plot 
     if isinstance(y, np.ndarray): 
      x_eval = eval(str(x), None, locs) 
      plt.plot(x_eval, y) 
      plt.show() 


if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    form = Form() 
    form.show() 
    app.exec_()