Related
When I try to get the text from the qtextedit created with PyQt5 Designer I get an error or "Python stop working" and the script stop automatically. I've tried multiple solutions but nothing works. I have to assign the text from the qtextedit to a variable, for check if the process run or no. That's the code generated by PyQt5:
from PyQt5 import QtCore, QtGui, QtWidgets
import psutil
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(221, 157)
MainWindow.setMinimumSize(QtCore.QSize(221, 157))
MainWindow.setMaximumSize(QtCore.QSize(221, 157))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.cercaprocesso = QtWidgets.QPushButton(self.centralwidget)
self.cercaprocesso.setGeometry(QtCore.QRect(0, 80, 221, 31))
font = QtGui.QFont()
font.setFamily("MS Shell Dlg 2")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.cercaprocesso.setFont(font)
self.cercaprocesso.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 221, 40))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.textbox = QtWidgets.QTextEdit(self.centralwidget)
self.textbox.setGeometry(QtCore.QRect(30, 40, 161, 31))
self.textbox.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.textbox.setObjectName("textEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 221, 21))
self.menubar.setObjectName("menubar")
self.menucredit = QtWidgets.QMenu(self.menubar)
self.menucredit.setObjectName("menucredit")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionbot_created_by_andrea1980345_inforge_user = QtWidgets.QAction(MainWindow)
self.actionbot_created_by_andrea1980345_inforge_user.setObjectName ("actionbot")
self.menucredit.addAction(self.actionbot_created_by_andrea1980345_inforge_user)
self.menubar.addAction(self.menucredit.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
Button Code here:
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "andrea1980345 Bot"))
self.cercaprocesso.setText(_translate("MainWindow", "Cerca Processo"))
self.label.setText(_translate("MainWindow", "Inserire il nome del processo"))
self.menucredit.setTitle(_translate("MainWindow", "Credit"))
self.actionbot_created_by_andrea1980345_inforge_user.setText(_translate("MainWindow", "bot"))
self.cercaprocesso.clicked.connect(self.search)
Here the code for get the text from the textbox and assign to a variable:
def search(self):
textboxValue = self.textbox.text()
for pid in psutil.pids(): # Controlla se il processo è attivo
listapid = psutil.Process(pid)
if listapid.name() == textboxValue:
print('Processo trovato')
self.textbox.setText("")
End code:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
QTextEdit does not have any text() method, if you want to get the text you must use toPlainText(), if you want to clean the text it is better to use clear() since it makes it more readable.
def search(self):
textboxValue = self.textbox.toPlainText()
for pid in psutil.pids(): # Controlla se il processo è attivo
listapid = psutil.Process(pid)
if listapid.name() == textboxValue:
print('Processo trovato')
self.textbox.clear()
I am writing GUI app with the help of Qt Designer. The app consists of multiple windows, so when the user clicks the push button on one window, the next window should open and the current window should hide. I achieved this feature for going from window 1 to window 2. But I am not able to do this to move from window 2 to window 3. I think this might be because the window 2 gets user arguments.
Is there any way to hide the window which gets user arguments?
The code is as follows:
Class for main window:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import user_data
class MainWindow(object):
def user_data(self):
self.user_data_window = QtWidgets.QMainWindow()
self.next_window = user_data.Ui_UserData()
self.next_window.setupUi(self.user_data_window)
main_window.hide()
self.user_data_window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(140, 100, 361, 81))
font = QtGui.QFont()
font.setPointSize(24)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 260, 75, 51))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.user_data)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Main Window"))
self.label.setText(_translate("MainWindow", " Manager"))
self.pushButton.setText(_translate("MainWindow", "Lets Start!"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = QtWidgets.QMainWindow()
ui = MainWindow()
ui.setupUi(main_window)
main_window.show()
sys.exit(app.exec_())
user_data.py ---> window 2
from PyQt5 import QtCore, QtGui, QtWidgets
import start_recording
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This is the part not working
"""
MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.groupBox_speaker_data = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_speaker_data.setGeometry(QtCore.QRect(80, 30, 481, 381))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(84)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
self.groupBox_speaker_data.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.groupBox_speaker_data.setFont(font)
self.groupBox_speaker_data.setObjectName("groupBox_speaker_data")
self.label_name = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_name.setGeometry(QtCore.QRect(30, 60, 47, 13))
self.label_name.setObjectName("label_name")
self.label_age = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_age.setGeometry(QtCore.QRect(30, 120, 47, 13))
self.label_age.setObjectName("label_age")
self.label_id = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_id.setGeometry(QtCore.QRect(30, 90, 47, 13))
self.label_id.setObjectName("label_id")
self.label_gender = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_gender.setGeometry(QtCore.QRect(30, 150, 47, 13))
self.label_gender.setObjectName("label_gender")
self.lineEdit_name = QtWidgets.QLineEdit(self.groupBox_speaker_data)
self.lineEdit_name.setGeometry(QtCore.QRect(110, 60, 141, 20))
self.lineEdit_name.setObjectName("lineEdit_name")
self.lineEdit_id = QtWidgets.QLineEdit(self.groupBox_speaker_data)
self.lineEdit_id.setGeometry(QtCore.QRect(110, 90, 141, 20))
self.lineEdit_id.setObjectName("lineEdit_id")
self.spinBox_age = QtWidgets.QSpinBox(self.groupBox_speaker_data)
self.spinBox_age.setGeometry(QtCore.QRect(110, 120, 42, 22))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(18)
sizePolicy.setVerticalStretch(78)
self.spinBox_age.setSizePolicy(sizePolicy)
self.spinBox_age.setMinimum(18)
self.spinBox_age.setObjectName("spinBox_age")
self.comboBox_gender = QtWidgets.QComboBox(self.groupBox_speaker_data)
self.comboBox_gender.setGeometry(QtCore.QRect(110, 150, 69, 22))
self.comboBox_gender.setObjectName("comboBox_gender")
self.comboBox_gender.addItem("")
self.comboBox_gender.addItem("")
self.comboBox_gender.addItem("")
self.pushButton = QtWidgets.QPushButton(self.groupBox_speaker_data)
self.pushButton.setGeometry(QtCore.QRect(370, 330, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.start_record)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "User Data"))
self.groupBox_speaker_data.setTitle(_translate("MainWindow", "Speaker Data"))
self.label_name.setText(_translate("MainWindow", "Name:"))
self.label_age.setText(_translate("MainWindow", "Age:"))
self.label_id.setText(_translate("MainWindow", "ID:"))
self.label_gender.setText(_translate("MainWindow", "Gender:"))
self.comboBox_gender.setItemText(0, _translate("MainWindow", "Male"))
self.comboBox_gender.setItemText(1, _translate("MainWindow", "Female"))
self.comboBox_gender.setItemText(2, _translate("MainWindow", "Unknown"))
self.pushButton.setText(_translate("MainWindow", "NEXT"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_UserData()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
start_recording.py ----> window 3
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_StartRecording(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(160, 120, 331, 91))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(260, 200, 101, 51))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 260, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Lets begin"))
self.label_2.setText(_translate("MainWindow", "xxx!"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_StartRecording()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This is the part not working
"""
MainWindow.hide()
^^^^^^^^ ---- there is no 'MainWindow' variable available to class, hence it doesn't know what to hide here.
Solution is:
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This part is now working!
"""
self.mw.hide()
self.window.show()
def setupUi(self, MainWindow):
self.mw = MainWindow
# ^^^^^^^^ - You create class variable for mainwindow object
I am trying to build simple FTP client in Python and for GUI I use PyQt5. Problem is when I run program QTreeView do not showing directories, it is just blank surface. How can I code this to show folders and how to link with this listView below? I was looked on Google but I can not resolve this.
This window I get
And I want something like this
Code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(852, 637)
MainWindow.setAnimated(True)
MainWindow.setDocumentMode(False)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.connectButton = QtWidgets.QPushButton(self.centralwidget)
self.connectButton.setGeometry(QtCore.QRect(710, 20, 111, 21))
self.connectButton.setObjectName("connectButton")
self.usernameEdit = QtWidgets.QLineEdit(self.centralwidget)
self.usernameEdit.setGeometry(QtCore.QRect(180, 20, 161, 20))
self.usernameEdit.setObjectName("usernameEdit")
self.passEdit = QtWidgets.QLineEdit(self.centralwidget)
self.passEdit.setGeometry(QtCore.QRect(350, 20, 161, 20))
self.passEdit.setObjectName("passEdit")
self.portEdit = QtWidgets.QLineEdit(self.centralwidget)
self.portEdit.setGeometry(QtCore.QRect(520, 20, 161, 20))
self.portEdit.setReadOnly(False)
self.portEdit.setObjectName("portEdit")
self.hostEdit = QtWidgets.QLineEdit(self.centralwidget)
self.hostEdit.setGeometry(QtCore.QRect(10, 20, 161, 20))
self.hostEdit.setText("")
self.hostEdit.setObjectName("hostEdit")
self.treeView = QtWidgets.QTreeView(self.centralwidget)
self.treeView.setGeometry(QtCore.QRect(10, 60, 401, 281))
self.treeView.setObjectName("treeView")
self.treeView_2 = QtWidgets.QTreeView(self.centralwidget)
self.treeView_2.setGeometry(QtCore.QRect(420, 60, 401, 281))
self.treeView_2.setObjectName("treeView_2")
self.listView = QtWidgets.QListView(self.centralwidget)
self.listView.setGeometry(QtCore.QRect(10, 350, 401, 192))
self.listView.setObjectName("listView")
self.listView_2 = QtWidgets.QListView(self.centralwidget)
self.listView_2.setEnabled(True)
self.listView_2.setGeometry(QtCore.QRect(420, 350, 401, 192))
self.listView_2.setObjectName("listView_2")
self.uploadButton = QtWidgets.QPushButton(self.centralwidget)
self.uploadButton.setGeometry(QtCore.QRect(10, 550, 401, 41))
self.uploadButton.setObjectName("uploadButton")
self.downloadButton = QtWidgets.QPushButton(self.centralwidget)
self.downloadButton.setGeometry(QtCore.QRect(420, 550, 401, 41))
self.downloadButton.setObjectName("downloadButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 852, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "My FTP Client"))
self.connectButton.setText(_translate("MainWindow", "Quickconnect"))
self.usernameEdit.setPlaceholderText(_translate("MainWindow", "Username"))
self.passEdit.setPlaceholderText(_translate("MainWindow", "Password"))
self.portEdit.setPlaceholderText(_translate("MainWindow", "Port"))
self.hostEdit.setPlaceholderText(_translate("MainWindow", "Host"))
self.uploadButton.setText(_translate("MainWindow", "Upload"))
self.downloadButton.setText(_translate("MainWindow", "Download"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
How do you want the QTreeView to show something if it does not tell you what to show?, a QTreeView what it does is show the content of some model, in your case you should use a QFileSystemModel.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
...
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
model = QtWidgets.QFileSystemModel(self)
model.setRootPath(QtCore.QDir.rootPath())
self.treeView.setModel(model)
self.treeView.expandAll()
self.treeView.setRootIndex(model.index(QtCore.QDir.rootPath()))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I'm new on stackoverflow.
I have a MainWindow on PyQt, I want click a button and open a QFileDialog for choose a file. The problem is: If I use a MainWindow, QFileDialog don't run. If I use a Dialog, QFileDialog run.
This is my code for the MainWindow.
import sys
from Import_fsa import import_fsa
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
from Vannucci_Gemignani import Ui_MainWindow
class GUI_fsa(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.Button_Browse, QtCore.SIGNAL('clicked()'), self.Browse)
def Browse(self):
fname=QFileDialog.getOpenFileName()
self.lineEdit.setText(fname)
data_set=import_fsa(fname)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This is the code for Dialog. Here I write the code in the .py generate using pyuic4 (QTDesigner)
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
from ab1 import ABIFReader
import pylab
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(508, 363)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(10, 40, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(110, 40, 361, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL('clicked()'), self.selectFile)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
def selectFile(self):
fname=QFileDialog.getOpenFileName()
self.lineEdit.setText(fname)
reader=ABIFReader(fname)
dati=reader.getData('DATA',1)
pylab.plot(dati)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
This is Vannucci_Gemignani.py:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(1445, 744)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 771, 83))
self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setMargin(0)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.Button_Browse = QtGui.QPushButton(self.gridLayoutWidget)
self.Button_Browse.setObjectName(_fromUtf8("Button_Browse"))
self.gridLayout.addWidget(self.Button_Browse, 0, 0, 1, 1)
self.Button_Plot = QtGui.QPushButton(self.gridLayoutWidget)
self.Button_Plot.setObjectName(_fromUtf8("Button_Plot"))
self.gridLayout.addWidget(self.Button_Plot, 1, 0, 1, 1)
self.lineEdit = QtGui.QLineEdit(self.gridLayoutWidget)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
self.Button_Clear = QtGui.QPushButton(self.gridLayoutWidget)
self.Button_Clear.setObjectName(_fromUtf8("Button_Clear"))
self.gridLayout.addWidget(self.Button_Clear, 2, 0, 1, 1)
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 130, 61, 31))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(700, 130, 61, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(20, 430, 61, 31))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(700, 430, 61, 31))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.widget = matplotlibWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(90, 130, 571, 251))
self.widget.setObjectName(_fromUtf8("widget"))
self.widget_2 = matplotlibWidget2(self.centralwidget)
self.widget_2.setGeometry(QtCore.QRect(770, 130, 571, 251))
self.widget_2.setObjectName(_fromUtf8("widget_2"))
self.widget_3 = matplotlibWidget3(self.centralwidget)
self.widget_3.setGeometry(QtCore.QRect(90, 430, 571, 251))
self.widget_3.setObjectName(_fromUtf8("widget_3"))
self.widget_4 = matplotlibWidget4(self.centralwidget)
self.widget_4.setGeometry(QtCore.QRect(770, 430, 571, 251))
self.widget_4.setObjectName(_fromUtf8("widget_4"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1445, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuFile = QtGui.QMenu(self.menubar)
self.menuFile.setObjectName(_fromUtf8("menuFile"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "fsa Plotter", None, QtGui.QApplication.UnicodeUTF8))
self.Button_Browse.setText(QtGui.QApplication.translate("MainWindow", "Browse", None, QtGui.QApplication.UnicodeUTF8))
self.Button_Plot.setText(QtGui.QApplication.translate("MainWindow", "Plot", None, QtGui.QApplication.UnicodeUTF8))
self.Button_Clear.setText(QtGui.QApplication.translate("MainWindow", "Clear", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "Channel 1", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("MainWindow", "Channel 2", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("MainWindow", "Channel 3", None, QtGui.QApplication.UnicodeUTF8))
self.label_4.setText(QtGui.QApplication.translate("MainWindow", "Channel 4", None, QtGui.QApplication.UnicodeUTF8))
self.menuFile.setTitle(QtGui.QApplication.translate("MainWindow", "File", None, QtGui.QApplication.UnicodeUTF8))
from matplotlibwidget import matplotlibWidget
from matplotlibwidget3 import matplotlibWidget3
from matplotlibwidget2 import matplotlibWidget2
from matplotlibwidget4 import matplotlibWidget4
The file-dialog doesn't show, because you did not create an instance of your GUI_fsa class.
To fix that, make the following changes:
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = GUI_fsa()
# the next two lines aren't needed
# ui = Ui_MainWindow()
# ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
There is another problem you will also need to fix, in your Browse method. The widgets from Ui_MainWindow need to accessed via self.ui. So also make the following changes:
def Browse(self):
fname = QFileDialog.getOpenFileName()
self.ui.lineEdit.setText(fname)
...
One final suggestion: avoid using the old-style syntax for connecting signals, and use the new-style syntax instead:
# don't do this:
# QtCore.QObject.connect(self.ui.Button_Browse, QtCore.SIGNAL('clicked()'), self.Browse)
# do this!
self.ui.Button_Browse.clicked.connect(self.Browse)
try changing the lines:
def Browse(self):
fname=QFileDialog.getOpenFileName()
by
from PyQt4.QtCore import QObject, pyqtSlot
#pyqtSlot()
def on_Button_Browse_clicked(self):
fname=QFileDialog.getOpenFileName()
and remove the line
QtCore.QObject.connect(self.ui.Button_Browse, QtCore.SIGNAL('clicked()'), self.Browse)
Explanation:
When you're using QtDesigner and pyuic4 you don't need to connect the events by your self. The pyuic4 generated class take care of that for you. The only you have to do is write your class methods correctly. For example: IF you have a button called "button_1" and you want perform some action when clicked, you just create a method like this:
class Example(QMainWindow):
...
def on_button_1_clicked(self):
pass
the key here is on_button_1_clicked which stands for: on_<widget-name>_<signal> with button_1 as widget name and clicked as signal
I tried to follow just the first part of this guide:
http://talk.maemo.org/archive/index.php/t-39879.html
to make only the hello world part work. It doesn't
here is the error
Traceback (most recent call last):
File "helloworld2.py", line 69, in <module>
mainWindow = MainWin(None, "main window")
File "helloworld2.py", line 49, in __init__
self.setupUi(rootWidget)
File "helloworld2.py", line 25, in setupUi
MainWindow.setCentralWidget(self.centralwidget)
AttributeError: 'QWidget' object has no attribute 'setCentralWidget'
Should this be working(after tweaking), or should I go a whole different route?
If it should be working, what are the things to do to troubleshoot?
Here are the ingredients of the .py file. I'm not sure I have it entered into here correctly, I'm not sure I understand the correct way to enter it, so it will appear the same as in the file. If this doesn't look right I'll try again.
from PyKDE4 import kdecore
from PyKDE4 import kdeui
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 400)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(80, 110, 271, 51))
font = QtGui.QFont()
font.setPointSize(24)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 20))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(kdecore.i18n(_fromUtf8("MainWindow")))
self.label.setText(kdecore.i18n(_fromUtf8("Hello World!")))
if __name__ == '__main__':
import sys
global app
class MainWin(kdeui.KMainWindow, Ui_MainWindow):
def __init__ (self, *args):
kdeui.KMainWindow.__init__ (self)
rootWidget = QtGui.QWidget(self)
self.setupUi(rootWidget)
self.resize(640, 480)
self.setCentralWidget(rootWidget)
appName = "default"
catalog = ""
programName = kdecore.ki18n("default")
version = "1.0"
description = kdecore.ki18n("Default Example")
license = kdecore.KAboutData.License_GPL
copyright = kdecore.ki18n("unknown")
text = kdecore.ki18n("none")
homePage = ""
bugEmail = "email"
aboutData = kdecore.KAboutData(appName, catalog, programName, version, description,
license, copyright, text, homePage, bugEmail)
kdecore.KCmdLineArgs.init(sys.argv, aboutData)
app = kdeui.KApplication()
mainWindow = MainWin(None, "main window")
mainWindow.show()
app.lastWindowClosed.connect(app.quit)
app.exec_ ()
Looks like your code is a little messed up, see if this is working for you and take it on from there:
#!/usr/bin/env python
from PyKDE4 import kdecore, kdeui
from PyQt4 import QtCore, QtGui
appName = "default"
catalog = ""
programName = kdecore.ki18n("default")
version = "1.0"
description = kdecore.ki18n("Default Example")
license = kdecore.KAboutData.License_GPL
copyright = kdecore.ki18n("unknown")
text = kdecore.ki18n("none")
homePage = ""
bugEmail = "email"
aboutData = kdecore.KAboutData(appName, catalog, programName, version, description,
license, copyright, text, homePage, bugEmail)
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 400)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(80, 110, 271, 51))
font = QtGui.QFont()
font.setPointSize(24)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 20))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(kdecore.i18n(_fromUtf8("MainWindow")))
self.label.setText(kdecore.i18n(_fromUtf8("Hello World!")))
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
import sys
kdecore.KCmdLineArgs.init(sys.argv, aboutData)
app = kdeui.KApplication()
main = MainWindow()
main.resize(640, 480)
main.show()
app.lastWindowClosed.connect(app.quit)
app.exec_()