How can I add items in listWidget? - python

I have trouble adding item in listWidget
I made UI with QT creator and want to import it in python.
When I run this code the items doesn't shows in listWidget, only white blanks.
what is the problem??
please help me
my code is
import sys
from PyQt4 import QtCore, QtGui
from secdialog import Ui_SecDialog
class SecDialog(QtGui.QDialog, Ui_SecDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.setupUi(self)
self.listWidget.addItem("QlistItem_1");
self.listWidget.addItem("QlistItem_2");
self.listWidget.addItem("QlistItem_3");
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Ui_SecDialog()
ex.show()
sys.exit(app.exec_())

You create an instance of Ui_SecDialog in ex but you want to create a SecDialog instance:
ex = SecDialog()

Related

QToolBar does not show text next to icon

I have the following code:
from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon
class ConfigureBar(QtWidgets.QToolBar):
def __init__(self, parent=None):
super().__init__(parent)
self.addAction(QtWidgets.QIcon("some_icon.png"), "Hi")
self.addSeparator()
self.addAction(QIcon("some_icon.png"), "Hello")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = ConfigureBar()
window.show()
app.exec()
But for some reason, the displayed widget does not have a text (Hi, Hello), only the icon. I tried to find some answer but I can't seem to get the keyword right. Help?
The toolButtonStyle property indicates the style of how the QToolButtons are shown, and by default it is Qt::ToolButtonIconOnly, so only show the icon, if you want to show the text you have to use Qt::ToolButtonTextBesideIcon or Qt::ToolButtonTextUnderIcon:
from PyQt5 import QtCore, QtGui, QtWidgets
class ConfigureBar(QtWidgets.QToolBar):
def __init__(self, parent=None):
super().__init__(parent)
self.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
# or
# self.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
self.addAction(QtGui.QIcon("some_icon.png"), "Hi")
self.addSeparator()
self.addAction(QtGui.QIcon("some_icon.png"), "Hello")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
toolbar = ConfigureBar()
w = QtWidgets.QMainWindow()
w.addToolBar(toolbar)
w.show()
sys.exit(app.exec_())

Python PySide with Qt Designer

I'm trying to make a PySide application. I've watched some tutorials to try to solve the problem but none worked and i do not have any errors in my code.
Here's the file where i'd do the scripting
main.py
import sys
from PySide import QtGui
from ui import Ui_Form
class Main(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow)
self.ui = Ui_Form()
self.ui.setupUi(self)
if __name__ == '__init__':
app = QtGui.QApplication(sys.argv)
wid = QtGui.QWidget()
wid.resize(250, 150)
wid.setWindowTitle('Simple')
wid.show()
sys.exit(app.exec_())
It has to be '__main__'
if __name__ == '__main__':
You have class Main() but you don't use it
wid = Main()
You have to execute super() in correct way
super(Main, self).__init__()
BTW: and you have wrong indentions inside class
Working example - without ui because I don't have it - but it shows window.
from PySide import QtGui
import sys
#from ui import Ui_Form
class Main(QtGui.QMainWindow):
def __init__(self):
super(Main, self).__init__()
#self.ui = Ui_Form()
#self.ui.setupUi(self)
self.resize(250, 150)
self.setWindowTitle('Simple')
self.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
wid = Main()
sys.exit(app.exec_())
Well i just realized that i never actually ran self.show().
Problem solved

Ui_MainWindow has no attribute 'show'

I'm writing a program which will have multiple windows. I have a main program (attached) that calls the Ui files (that have been converted into .py). The main window and customise window open correctly (the first two listed) but neither the third or fourth windows open correctly, giving me the error
'Ui_MainWindow' object has no attribute 'show'
The main program;
from PyQt4 import QtCore, QtGui
import sys
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainwin = main_menu_ui.Ui_MainWindow()
mainwin.show()
sys.exit(app.exec_())
def openCustomise(self):
customiseOpen = question_set_menu_ui.Ui_MainWindow()
customiseOpen.show()
sys.exit(app.exec_())
def openQuiz(self):
quizOpen = quiz_window_ui.Ui_MainWindow()
quizOpen.show()
sys.exit(app.exec_())
def addNewSet(self):
addNewOpen = question_set_edit_ui.Ui_MainWindow()
addNewOpen.show()
sys.exit(app.exec_())
Sorry if I'm missing something obvious, I'm learning Qt/Python for college.
The auto-generated UI class that you are importing extends object and doesn't have a show method (open up the .py file for yourself and verify this).
In general, you should structure your GUIs like this:
from PyQt4 import QtCore, QtGui
import sys
from layout_file import main_menu_ui
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = main_menu_ui()
self.ui.setupUi(self)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainwin = MyForm()
mainwin.show()
sys.exit(app.exec_())
You import your UI from your autogenerated UI file. You have a class that contains your GUI logic. It then sets up your UI layout from your imported UI in its __init__() method.

Open a GUI file from another file PyQT

I've created many GUI interfaces in PyQT using QT Designer, but now I'm trying to open an interface from another one, and I don't know how to do it..
Start.py is the file which run the GUI Interface Authentification_1 and Acceuil_start.py is the file which run the GUI interface Acceuil_2.py, now I want from Start.py to lunch Acceuil_start.py.
Do you have any idea about that ? Thank you.
Here's my code :
Start.py :
import sys
from PyQt4 import QtCore, QtGui
from Authentification_1 import Ui_Fenetre_auth
from Acceuil_2 import Ui_MainWindow #??? Acceuil_2.py is the file which I want to open
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Fenetre_auth()
self.ui.setupUi(self)
def authentifier(val): #Slot method
self.Acceuil = Acceuil() #???
self.Acceuil.show() #???
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
Acceuil_start.py
import sys
from PyQt4 import QtCore, QtGui
from Authentification_1 import Ui_Fenetre_auth
from Acceuil_2 import Ui_MainWindow
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
First, you should to name your GUI classes so they have a different name, and not the generic one, so you could distinct them.
Why you would need to do that? Well - simply, because it makes sense - if every class is representing different type of dialog, so it is the different type - and it should be named differently. Some of the names are/may be: QMessageBox, AboutBox, AddUserDialog, and so on.
In Acceuil_start.py (you should rename class in other module, too).
import sys
from PyQt4 import QtCore, QtGui
from Authentification_1 import Ui_Fenetre_auth
from Acceuil_2 import Ui_MainWindow
class Acceuil(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = Acceuil()
myapp.show()
sys.exit(app.exec_())
in the parent class, when you want to create the window, you are close (but it should work in any case):
def authentifier(val): #Slot method
self.Acceuil = Acceuil(self) # You should always pass the parent to the child control
self.Acceuil.show() #???
About parent issue: If your widget/window is creating another widget, setting creator object to be parent is always a good idea (apart from some singular cases), and you should read this to see why is that so:
QObjects organize themselves in object trees. When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is. It turns out that this approach fits the needs of GUI objects very well. For example, a QShortcut (keyboard shortcut) is a child of the relevant window, so when the user closes that window, the shorcut is deleted too.
Edit - Minimal Working Sample
To see what I am trying to tell you, I've built a simple example. You have two classes - MainWindow and
ChildWindow. Every class can work without other class by creating separate QApplication objects. But, if you import ChildWindow in MainWindow, you will create ChildWindow in slot connected to singleshot timer which will trigger in 5 seconds.
MainWindow.py:
import sys
from PyQt4 import QtCore, QtGui
from ChildWindow import ChildWindow
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
QtCore.QTimer.singleShot(5000, self.showChildWindow)
def showChildWindow(self):
self.child_win = ChildWindow(self)
self.child_win.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
ChildWindow.py:
import sys
from PyQt4 import QtCore, QtGui
class ChildWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setWindowTitle("Child Window!")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = ChildWindow()
myapp.show()
sys.exit(app.exec_())
To reference the other dialog from Start.py, you must prefix it by the module name, which in this case is Acceuil_start. As such, it is OK if there are duplicated function names in each module. So, you would have:
def authentifier(val): #Slot method
dlg = Acceuil_start.StartQT4()
dlg.exec_()
However, if you want these to run from the same process, keep in mind that you can't have two app objects. You would probably want to structure Acceuil_start.py to act like a dialog, rather than a main window. If these are two distinct main windows, you might find it easier to just invoke another Python interpreter with the Acceuil_start.py as a parameter.

Basic Widget Interaction with PyQt

Please can someone tell me what im doing wrong here with respect to calling pwTxt.text.
#!/usr/bin/python
import sys
from PyQt4 import QtCore, QtGui
from mainwindow import Ui_MainWindow
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def on_pwExtract_pressed(self):
print self.pwTxt.text
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
The line print self.pwTxt.text fails because it can't find the widget, pwTxt is a QLineEdit defined on the main window. I just made it in QTDesigner and generated python code with pyuic4.
How do I correctly reference other widgets on the same window, in this case I just want to get the text from a QLineEdit named pwTxt when the QPushButton pwExtract is pressed.
Thanks a lot.
Try:
print self.ui.pwTxt.text()

Categories