Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use pyqt4 to create GUI that runs python script

Tags:

python

qt

pyqt4

I have written a python script that manipulates data and then saves it into an excel file with the following function:

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

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

I also have the following super basic/minimalistic gui window:

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_())

What I really would like to know is this:

1) How can I let the user of my gui select the location + name for the manipulatedData file?

2) How can I add a "run" button, that starts my python script and manipulates the data, before it saves the manipulated data to the desired location.

like image 831
titusAdam Avatar asked May 27 '26 06:05

titusAdam


1 Answers

PyQt4 has QLineEdit() and 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_())
like image 194
furas Avatar answered May 30 '26 11:05

furas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!