使用pyqt4创建运行python脚本的GUI

时间:2016-12-13 23:34:13

标签: python qt pyqt4

我编写了一个操作数据的python脚本,然后使用以下函数将其保存到excel文件中:

def saveData():
    table = pd.DataFrame(data)

    writer = pd.ExcelWriter('manipulatedData.xlsx')
    table.to_excel(writer, 'sheet 1')
    writer.save()

我还有以下超级基本/简约gui窗口:

from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)

window = QtGui.QWidget()
window.setGeometry(500,500,500,500)
window.setWindowTitle("PyQt Test!")
window.setWindowIcon(QtGui.QIcon('pythonLogoSmall.png'))
window.show()

sys.exit(app.exec_())

我真正想知道的是:

1)如何让我的gui用户选择manipulatedData文件的位置+名称?

2)如何添加"运行"按钮,它启动我的python脚本并操纵数据,然后将操纵的数据保存到所需的位置。

1 个答案:

答案 0 :(得分:1)

PyQt4QLineEdit()QPushButton()

from PyQt4 import QtGui
import sys

# --- functions ---

def my_function(event=None):
    print 'Button clicked: event:', event
    print linetext.text()

    # run your code

# --- main ---

app = QtGui.QApplication(sys.argv)

window = QtGui.QWidget()

# add "layout manager"
vbox = QtGui.QVBoxLayout()
window.setLayout(vbox)

# add place for text
linetext = QtGui.QLineEdit(window)
vbox.addWidget(linetext)

# add button 
button = QtGui.QPushButton("Run", window)
vbox.addWidget(button)

# add function to button 
button.clicked.connect(my_function)

window.show()

sys.exit(app.exec_())