I am still working on the GUI for my Database and now i have a different error:
Traceback (most recent call last):
File "G:\Python\Database Kast Thuis\PyQt Test\MainMenu_ui.py", line 84, in <module>
ex = Ui_MainWindow()
File "G:\Python\Database Kast Thuis\PyQt Test\MainMenu_ui.py", line 16, in __init__
self.setupUi(self)
File "G:\Python\Database Kast Thuis\PyQt Test\MainMenu_ui.py", line 55, in setupUi
MainWindow.setCentralWidget(self.centralwidget)
AttributeError: 'Ui_MainWindow' object has no attribute 'setCentralWidget'
I dont understand a thing about PyQt so i dont understand what the problem is.
The code is:
(This is the edited version, Raw Version is Posted below)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Main_Menu.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
class Ui_MainWindow(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(387, 317)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/Natan Samuel Geldorp.Remytop-PC/Documents/music-note-sound-256.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.ButtonRemove = QtWidgets.QPushButton(self.centralwidget)
self.ButtonRemove.setObjectName("ButtonRemove")
self.gridLayout.addWidget(self.ButtonRemove, 10, 0, 1, 1)
self.ButtonShowAll = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowAll.setObjectName("ButtonShowAll")
self.gridLayout.addWidget(self.ButtonShowAll, 7, 0, 1, 1)
self.ButtonShowSpecific = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowSpecific.setObjectName("ButtonShowSpecific")
self.gridLayout.addWidget(self.ButtonShowSpecific, 8, 0, 1, 1)
self.Text = QtWidgets.QLabel(self.centralwidget)
self.Text.setObjectName("Text")
self.gridLayout.addWidget(self.Text, 1, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 3, 0, 1, 1)
self.ButtonAdd = QtWidgets.QPushButton(self.centralwidget)
self.ButtonAdd.setObjectName("ButtonAdd")
self.gridLayout.addWidget(self.ButtonAdd, 9, 0, 1, 1)
self.ButtonExit = QtWidgets.QPushButton(self.centralwidget)
self.ButtonExit.setObjectName("ButtonExit")
self.gridLayout.addWidget(self.ButtonExit, 4, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
self.verticalLayout.addLayout(self.gridLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 387, 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 Menu"))
self.ButtonRemove.setText(_translate("MainWindow", "Remove an album"))
self.ButtonShowAll.setText(_translate("MainWindow", "Show all registerd albums"))
self.ButtonShowSpecific.setText(_translate("MainWindow", "Show a specific album"))
self.Text.setText(_translate("MainWindow", "Welcome to the MusicDatabase Version 2.1-2017"))
self.label.setText(_translate("MainWindow", "Please select one of the options below."))
self.ButtonAdd.setText(_translate("MainWindow", "Add an album"))
self.ButtonExit.setText(_translate("MainWindow", "Exit"))
self.label_2.setText(_translate("MainWindow", "Authors: N.S. Geldorp & H. Nahumury"))
self.ButtonExit.clicked.connect(self.ButtonExit)
def ButtonExit(self):
exit()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Ui_MainWindow()
ex.show()
sys.exit(app.exec_())
Does anyone see the problem?
-Natan
Posting Raw Convert:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Main_Menu.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(387, 317)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/Natan Samuel Geldorp.Remytop-PC/Documents/music-note-sound-256.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.ButtonRemove = QtWidgets.QPushButton(self.centralwidget)
self.ButtonRemove.setObjectName("ButtonRemove")
self.gridLayout.addWidget(self.ButtonRemove, 10, 0, 1, 1)
self.ButtonShowAll = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowAll.setObjectName("ButtonShowAll")
self.gridLayout.addWidget(self.ButtonShowAll, 7, 0, 1, 1)
self.ButtonShowSpecific = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowSpecific.setObjectName("ButtonShowSpecific")
self.gridLayout.addWidget(self.ButtonShowSpecific, 8, 0, 1, 1)
self.Text = QtWidgets.QLabel(self.centralwidget)
self.Text.setObjectName("Text")
self.gridLayout.addWidget(self.Text, 1, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 3, 0, 1, 1)
self.ButtonAdd = QtWidgets.QPushButton(self.centralwidget)
self.ButtonAdd.setObjectName("ButtonAdd")
self.gridLayout.addWidget(self.ButtonAdd, 9, 0, 1, 1)
self.ButtonExit = QtWidgets.QPushButton(self.centralwidget)
self.ButtonExit.setObjectName("ButtonExit")
self.gridLayout.addWidget(self.ButtonExit, 4, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
self.verticalLayout.addLayout(self.gridLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 387, 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 Menu"))
self.ButtonRemove.setText(_translate("MainWindow", "Remove an album"))
self.ButtonShowAll.setText(_translate("MainWindow", "Show all registerd albums"))
self.ButtonShowSpecific.setText(_translate("MainWindow", "Show a specific album"))
self.Text.setText(_translate("MainWindow", "Welcome to the MusicDatabase Version 2.1-2017"))
self.label.setText(_translate("MainWindow", "Please select one of the options below."))
self.ButtonAdd.setText(_translate("MainWindow", "Add an album"))
self.ButtonExit.setText(_translate("MainWindow", "Exit"))
self.label_2.setText(_translate("MainWindow", "Authors: N.S. Geldorp & H. Nahumury"))
Qt Designer serves to generate the design of the view, pyuic5 converts that design into python code, this element must be used with logic, in addition it is recommended not to modify it.
first solution:
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Ui_MainWindow()
w = QtWidgets.QMainWindow()
ex.setupUi(w)
w.show()
sys.exit(app.exec_())
Second Solution:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
ui = Ui_MainWindow()
ui.setupUi(self)
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Third solution:
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Related
The function genPassword is going to generate a random password, all I want is getting the value returned from the function and putting it into the screen.
This is the full code.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(553, 600)
MainWindow.setMouseTracking(False)
MainWindow.setAutoFillBackground(False)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(110, 60, 371, 61))
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.generate = QtWidgets.QPushButton(self.centralwidget)
self.generate.setEnabled(True)
self.generate.setGeometry(QtCore.QRect(190, 220, 161, 41))
font = QtGui.QFont()
font.setPointSize(20)
self.generate.setFont(font)
self.generate.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.generate.setObjectName("generate")
Here, I wish to put my password,
Lambda didn't work
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(210, 140, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(160, 320, 231, 80))
self.widget.setObjectName("widget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 553, 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", "Generator"))
self.label.setText(_translate("MainWindow", "Enter the password length"))
self.generate.setText(_translate("MainWindow", "Generate"))
Here is the function,
"I want to take the length argument from the LineEdit widget"
def genPassword(self, length):
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
resources = list(ascii_lowercase) + list(ascii_uppercase) + nums
result = ""
for i in range(length):
result += str(choice(resources))
return result
if __name__ == "__main__":
import sys
from string import ascii_lowercase, ascii_uppercase
from random import choice
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I have used Qt designer 5 to create three different windows,
the principal MainWindows.py
the subwindows subVENTANA1.py and subVENTANA2.py
How can I call the sub-windows from MainWindows.py, with the use of mdiArea?
In MainWindows.py only have a menu bar, in this menu there are several subMenus, in this case 2 submenus.
I´ve tried did but I don´t understand how to call with example
like this:
from subVentana import Ui_subwindow
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent=parent)
subwindow = QMdiSubWindow()
subwindow.setWidget(Ui_subwindow())
self.mdiArea.addSubWindow(subwindow)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(670, 548)
mainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
self.mdiArea.setObjectName("mdiArea")
self.gridLayout.addWidget(self.mdiArea, 0, 0, 1, 1)
mainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(mainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 670, 21))
self.menubar.setObjectName("menubar")
self.menuAn_lisis = QtWidgets.QMenu(self.menubar)
self.menuAn_lisis.setObjectName("menuAn_lisis")
self.menuEliminar_ruido = QtWidgets.QMenu(self.menubar)
self.menuEliminar_ruido.setObjectName("menuEliminar_ruido")
self.menuMenu_C = QtWidgets.QMenu(self.menubar)
self.menuMenu_C.setObjectName("menuMenu_C")
mainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(mainWindow)
self.statusbar.setObjectName("statusbar")
mainWindow.setStatusBar(self.statusbar)
self.actionNDVI = QtWidgets.QAction(mainWindow)
self.actionNDVI.setObjectName("actionNDVI")
self.actionEliminar_Ruido = QtWidgets.QAction(mainWindow)
self.actionEliminar_Ruido.setObjectName("actionEliminar_Ruido")
self.actionBrillo_Contraste = QtWidgets.QAction(mainWindow)
self.actionBrillo_Contraste.setObjectName("actionBrillo_Contraste")
self.actionDetecci_n_de_Bordes = QtWidgets.QAction(mainWindow)
self.actionDetecci_n_de_Bordes.setObjectName("actionDetecci_n_de_Bordes")
self.actionEcualizaci_n = QtWidgets.QAction(mainWindow)
self.actionEcualizaci_n.setObjectName("actionEcualizaci_n")
self.actionMostrar_ventna_4 = QtWidgets.QAction(mainWindow)
self.actionMostrar_ventna_4.setObjectName("actionMostrar_ventna_4")
self.actionMostar_subventna2 = QtWidgets.QAction(mainWindow)
self.actionMostar_subventna2.setObjectName("actionMostar_subventna2")
self.menuAn_lisis.addAction(self.actionBrillo_Contraste)
self.menuAn_lisis.addAction(self.actionMostar_subventna2)
self.menuEliminar_ruido.addAction(self.actionEliminar_Ruido)
self.menuEliminar_ruido.addAction(self.actionMostrar_ventna_4)
self.menubar.addAction(self.menuAn_lisis.menuAction())
self.menubar.addAction(self.menuEliminar_ruido.menuAction())
self.menubar.addAction(self.menuMenu_C.menuAction())
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "Venta principal"))
self.menuAn_lisis.setTitle(_translate("mainWindow", "Memu A"))
self.menuEliminar_ruido.setTitle(_translate("mainWindow", "Menu B"))
self.menuMenu_C.setTitle(_translate("mainWindow", "Menu C"))
self.actionNDVI.setText(_translate("mainWindow", "NDVI"))
self.actionEliminar_Ruido.setText(_translate("mainWindow", "Mostar subventna 3"))
self.actionBrillo_Contraste.setText(_translate("mainWindow", "Mostar subventna 1"))
self.actionDetecci_n_de_Bordes.setText(_translate("mainWindow", "Detección de Bordes"))
self.actionEcualizaci_n.setText(_translate("mainWindow", "Ecualización"))
self.actionMostrar_ventna_4.setText(_translate("mainWindow", "Mostar subventna 4"))
self.actionMostar_subventna2.setText(_translate("mainWindow", "Mostar subventna 2"))
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_())
subVENTANA1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_subWindow1(object):
def setupUi(self, subWindow1):
subWindow1.setObjectName("subWindow1")
subWindow1.resize(388, 300)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
subWindow1.setFont(font)
subWindow1.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
subWindow1.setLayoutDirection(QtCore.Qt.RightToLeft)
self.centralwidget = QtWidgets.QWidget(subWindow1)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setObjectName("groupBox")
self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 3)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem, 0, 3, 1, 1)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem1, 1, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 1, 1, 1, 1)
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem2, 1, 2, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
subWindow1.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(subWindow1)
self.menubar.setGeometry(QtCore.QRect(0, 0, 388, 21))
self.menubar.setObjectName("menubar")
subWindow1.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(subWindow1)
self.statusbar.setObjectName("statusbar")
subWindow1.setStatusBar(self.statusbar)
self.retranslateUi(subWindow1)
QtCore.QMetaObject.connectSlotsByName(subWindow1)
def retranslateUi(self, subWindow1):
_translate = QtCore.QCoreApplication.translate
subWindow1.setWindowTitle(_translate("subWindow1", "Sub Ventna 1"))
self.groupBox.setTitle(_translate("subWindow1", "Imagen original"))
self.pushButton.setText(_translate("subWindow1", "Abrir imagen"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
subWindow1 = QtWidgets.QMainWindow()
ui = Ui_subWindow1()
ui.setupUi(subWindow1)
subWindow1.show()
sys.exit(app.exec_())
subVENTANA2.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_subWindow2(object):
def setupUi(self, subWindow2):
subWindow2.setObjectName("subWindow2")
subWindow2.resize(366, 299)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
subWindow2.setFont(font)
subWindow2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
subWindow2.setLayoutDirection(QtCore.Qt.RightToLeft)
self.centralwidget = QtWidgets.QWidget(subWindow2)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setObjectName("groupBox")
self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 3)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem, 0, 3, 1, 1)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem1, 1, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 1, 1, 1, 1)
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem2, 1, 2, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
subWindow2.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(subWindow2)
self.menubar.setGeometry(QtCore.QRect(0, 0, 366, 23))
self.menubar.setObjectName("menubar")
subWindow2.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(subWindow2)
self.statusbar.setObjectName("statusbar")
subWindow2.setStatusBar(self.statusbar)
self.retranslateUi(subWindow2)
QtCore.QMetaObject.connectSlotsByName(subWindow2)
def retranslateUi(self, subWindow2):
_translate = QtCore.QCoreApplication.translate
subWindow2.setWindowTitle(_translate("subWindow2", "Sub Ventana 2"))
self.groupBox.setTitle(_translate("subWindow2", "Imagen procesada"))
self.pushButton.setText(_translate("subWindow2", "Ver"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
subWindow2 = QtWidgets.QMainWindow()
ui = Ui_subWindow2()
ui.setupUi(subWindow2)
subWindow2.show()
sys.exit(app.exec_())
I try to do something like this:
Example
In my case like this: myExample
I hope you understand
setWidget() requires a QWidget argument, not the ui_* subclass used from the pyuic generated files.
from PyQt5 import QtCore, QtGui, QtWidgets
from ui_mainwindow import Ui_mainWindow
from subVENTANA1 import Ui_subWindow1
class MainWindow(QtWidgets.QMainWindow, Ui_mainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent=parent)
self.setupUi(self)
subwindow1 = QtWidgets.QMdiSubWindow()
# create the widget instance
window1 = QtWidgets.QMainWindow()
# create the ui class instance
ui = Ui_subWindow1()
# set the ui for the widget instance
ui.setupUi(window1)
# set the widget for the subwindow
subwindow1.setWidget(window1)
self.mdiArea.addSubWindow(subwindow1)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
i have two .ui files that converted to python using pyuic5.
first converted ui file is (LoginWindowUI.py) as below:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_LoginWindow(object):
def setupUi(self, LoginWindow):
LoginWindow.setObjectName("LoginWindow")
LoginWindow.setWindowModality(QtCore.Qt.NonModal)
LoginWindow.setEnabled(True)
LoginWindow.resize(446, 182)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(LoginWindow.sizePolicy().hasHeightForWidth())
LoginWindow.setSizePolicy(sizePolicy)
LoginWindow.setMouseTracking(False)
LoginWindow.setLayoutDirection(QtCore.Qt.RightToLeft)
LoginWindow.setLocale(QtCore.QLocale(QtCore.QLocale.Persian, QtCore.QLocale.Iran))
LoginWindow.setDocumentMode(False)
self.centralwidget = QtWidgets.QWidget(LoginWindow)
self.centralwidget.setObjectName("centralwidget")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(194, 30, 187, 77))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.password_ldt = QtWidgets.QLineEdit(self.layoutWidget)
self.password_ldt.setLocale(QtCore.QLocale(QtCore.QLocale.Persian, QtCore.QLocale.Iran))
self.password_ldt.setText("")
self.password_ldt.setEchoMode(QtWidgets.QLineEdit.Password)
self.password_ldt.setCursorPosition(0)
self.password_ldt.setAlignment(QtCore.Qt.AlignCenter)
self.password_ldt.setObjectName("password_ldt")
self.gridLayout.addWidget(self.password_ldt, 1, 1, 1, 1)
self.password_lbl = QtWidgets.QLabel(self.layoutWidget)
self.password_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.password_lbl.setObjectName("password_lbl")
self.gridLayout.addWidget(self.password_lbl, 1, 0, 1, 1)
self.username_lbl = QtWidgets.QLabel(self.layoutWidget)
self.username_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.username_lbl.setObjectName("username_lbl")
self.gridLayout.addWidget(self.username_lbl, 0, 0, 1, 1)
self.username_ldt = QtWidgets.QLineEdit(self.layoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.username_ldt.sizePolicy().hasHeightForWidth())
self.username_ldt.setSizePolicy(sizePolicy)
self.username_ldt.setLocale(QtCore.QLocale(QtCore.QLocale.Persian, QtCore.QLocale.Iran))
self.username_ldt.setText("")
self.username_ldt.setEchoMode(QtWidgets.QLineEdit.Normal)
self.username_ldt.setAlignment(QtCore.Qt.AlignCenter)
self.username_ldt.setObjectName("username_ldt")
self.gridLayout.addWidget(self.username_ldt, 0, 1, 1, 1)
self.login_btn = QtWidgets.QPushButton(self.layoutWidget)
self.login_btn.setCursor(QtGui.QCursor(QtCore.Qt.ClosedHandCursor))
self.login_btn.setAutoDefault(False)
self.login_btn.setDefault(False)
self.login_btn.setObjectName("login_btn")
self.gridLayout.addWidget(self.login_btn, 2, 1, 1, 1)
self.description_lbl = QtWidgets.QLabel(self.centralwidget)
self.description_lbl.setGeometry(QtCore.QRect(30, 40, 141, 41))
self.description_lbl.setText("")
self.description_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.description_lbl.setWordWrap(True)
self.description_lbl.setObjectName("description_lbl")
LoginWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(LoginWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 446, 21))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
LoginWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(LoginWindow)
self.statusbar.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.statusbar.setObjectName("statusbar")
LoginWindow.setStatusBar(self.statusbar)
self.action_cra = QtWidgets.QAction(LoginWindow)
self.action_cra.setObjectName("action_cra")
self.action_db = QtWidgets.QAction(LoginWindow)
self.action_db.setObjectName("action_db")
self.action_exit = QtWidgets.QAction(LoginWindow)
self.action_exit.setObjectName("action_exit")
self.menu.addAction(self.action_cra)
self.menu.addAction(self.action_db)
self.menu.addSeparator()
self.menu.addAction(self.action_exit)
self.menubar.addAction(self.menu.menuAction())
self.retranslateUi(LoginWindow)
self.action_exit.triggered.connect(LoginWindow.close)
QtCore.QMetaObject.connectSlotsByName(LoginWindow)
LoginWindow.setTabOrder(self.username_ldt, self.password_ldt)
LoginWindow.setTabOrder(self.password_ldt, self.login_btn)
def retranslateUi(self, LoginWindow):
_translate = QtCore.QCoreApplication.translate
self.password_ldt.setToolTip(_translate("LoginWindow", "رمز عبور خود را وارد کنید."))
self.password_lbl.setText(_translate("LoginWindow", " رمز عبور "))
self.username_lbl.setText(_translate("LoginWindow", "نام کاربری"))
self.username_ldt.setToolTip(_translate("LoginWindow", "لطفاً نام کاربری خود را وارد نمایید."))
self.login_btn.setText(_translate("LoginWindow", "ورود"))
self.menu.setTitle(_translate("LoginWindow", "درباره ما"))
self.action_cra.setText(_translate("LoginWindow", "A"))
self.action_db.setText(_translate("LoginWindow", "B"))
self.action_exit.setText(_translate("LoginWindow", "خروج"))
self.action_exit.setStatusTip(_translate("LoginWindow", "خروج"))
self.action_exit.setShortcut(_translate("LoginWindow", "Ctrl+Q"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
LoginWindow = QtWidgets.QMainWindow()
ui = Ui_LoginWindow()
ui.setupUi(LoginWindow)
LoginWindow.show()
sys.exit(app.exec_())
and the second converted .ui file is (MainWindowUI.py) as below:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(452, 432)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.cralogo_lbl = QtWidgets.QLabel(self.centralwidget)
self.cralogo_lbl.setGeometry(QtCore.QRect(90, 30, 171, 161))
self.cralogo_lbl.setText("")
self.cralogo_lbl.setPixmap(QtGui.QPixmap("../../.designer/backup/ICONS/CraLogo.png"))
self.cralogo_lbl.setScaledContents(True)
self.cralogo_lbl.setObjectName("cralogo_lbl")
self.exit_btn = QtWidgets.QPushButton(self.centralwidget)
self.exit_btn.setGeometry(QtCore.QRect(140, 340, 125, 40))
self.exit_btn.setMinimumSize(QtCore.QSize(125, 40))
self.exit_btn.setLayoutDirection(QtCore.Qt.LeftToRight)
self.exit_btn.setCheckable(False)
self.exit_btn.setChecked(False)
self.exit_btn.setObjectName("exit_btn")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(20, 10, 389, 152))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.mobile_btn = QtWidgets.QPushButton(self.layoutWidget)
self.mobile_btn.setEnabled(False)
self.mobile_btn.setMinimumSize(QtCore.QSize(125, 40))
self.mobile_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.mobile_btn.setCheckable(False)
self.mobile_btn.setChecked(False)
self.mobile_btn.setObjectName("mobile_btn")
self.gridLayout.addWidget(self.mobile_btn, 1, 0, 1, 1)
self.dataentry_btn = QtWidgets.QPushButton(self.layoutWidget)
self.dataentry_btn.setMinimumSize(QtCore.QSize(125, 40))
self.dataentry_btn.setLayoutDirection(QtCore.Qt.LeftToRight)
self.dataentry_btn.setCheckable(False)
self.dataentry_btn.setChecked(False)
self.dataentry_btn.setObjectName("dataentry_btn")
self.gridLayout.addWidget(self.dataentry_btn, 0, 2, 1, 1)
self.village_btn = QtWidgets.QPushButton(self.layoutWidget)
self.village_btn.setEnabled(False)
self.village_btn.setMinimumSize(QtCore.QSize(125, 40))
self.village_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.village_btn.setCheckable(False)
self.village_btn.setChecked(False)
self.village_btn.setObjectName("village_btn")
self.gridLayout.addWidget(self.village_btn, 1, 1, 1, 1)
self.fix_btn = QtWidgets.QPushButton(self.layoutWidget)
self.fix_btn.setEnabled(False)
self.fix_btn.setMinimumSize(QtCore.QSize(125, 40))
self.fix_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.fix_btn.setCheckable(False)
self.fix_btn.setChecked(False)
self.fix_btn.setObjectName("fix_btn")
self.gridLayout.addWidget(self.fix_btn, 0, 0, 1, 1)
self.post_btn = QtWidgets.QPushButton(self.layoutWidget)
self.post_btn.setEnabled(False)
self.post_btn.setMinimumSize(QtCore.QSize(125, 40))
self.post_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.post_btn.setCheckable(False)
self.post_btn.setChecked(False)
self.post_btn.setObjectName("post_btn")
self.gridLayout.addWidget(self.post_btn, 2, 0, 1, 1)
self.city_btn = QtWidgets.QPushButton(self.layoutWidget)
self.city_btn.setEnabled(False)
self.city_btn.setMinimumSize(QtCore.QSize(125, 40))
self.city_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.city_btn.setCheckable(False)
self.city_btn.setChecked(False)
self.city_btn.setObjectName("city_btn")
self.gridLayout.addWidget(self.city_btn, 0, 1, 1, 1)
self.layoutWidget1 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget1.setGeometry(QtCore.QRect(20, 190, 389, 134))
self.layoutWidget1.setObjectName("layoutWidget1")
self.gridLayout_2 = QtWidgets.QGridLayout(self.layoutWidget1)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setObjectName("gridLayout_2")
self.rep4_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep4_btn.setEnabled(False)
self.rep4_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep4_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep4_btn.setCheckable(False)
self.rep4_btn.setObjectName("rep4_btn")
self.gridLayout_2.addWidget(self.rep4_btn, 0, 0, 1, 1)
self.rep1_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep1_btn.setEnabled(False)
self.rep1_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep1_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep1_btn.setCheckable(False)
self.rep1_btn.setObjectName("rep1_btn")
self.gridLayout_2.addWidget(self.rep1_btn, 0, 1, 1, 1)
self.report_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.report_btn.setMinimumSize(QtCore.QSize(125, 40))
self.report_btn.setMaximumSize(QtCore.QSize(125, 40))
self.report_btn.setCheckable(False)
self.report_btn.setObjectName("report_btn")
self.gridLayout_2.addWidget(self.report_btn, 0, 2, 1, 1)
self.rep5_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep5_btn.setEnabled(False)
self.rep5_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep5_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep5_btn.setCheckable(False)
self.rep5_btn.setObjectName("rep5_btn")
self.gridLayout_2.addWidget(self.rep5_btn, 1, 0, 1, 1)
self.rep2_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep2_btn.setEnabled(False)
self.rep2_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep2_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep2_btn.setCheckable(False)
self.rep2_btn.setObjectName("rep2_btn")
self.gridLayout_2.addWidget(self.rep2_btn, 1, 1, 1, 1)
self.rep6_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep6_btn.setEnabled(False)
self.rep6_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep6_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep6_btn.setCheckable(False)
self.rep6_btn.setObjectName("rep6_btn")
self.gridLayout_2.addWidget(self.rep6_btn, 2, 0, 1, 1)
self.rep3_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep3_btn.setEnabled(False)
self.rep3_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep3_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep3_btn.setCheckable(False)
self.rep3_btn.setObjectName("rep3_btn")
self.gridLayout_2.addWidget(self.rep3_btn, 2, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 452, 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)
self.exit_btn.clicked.connect(MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.cralogo_lbl.setStatusTip(_translate("MainWindow", "www.cra.ir"))
self.exit_btn.setText(_translate("MainWindow", "خروج"))
self.mobile_btn.setText(_translate("MainWindow", "4"))
self.dataentry_btn.setText(_translate("MainWindow", "A"))
self.village_btn.setText(_translate("MainWindow", "2"))
self.fix_btn.setText(_translate("MainWindow", "3"))
self.post_btn.setText(_translate("MainWindow", "5"))
self.city_btn.setText(_translate("MainWindow", "1"))
self.rep4_btn.setText(_translate("MainWindow", "4"))
self.rep1_btn.setText(_translate("MainWindow", "1"))
self.report_btn.setText(_translate("MainWindow", "B"))
self.rep5_btn.setText(_translate("MainWindow", "5"))
self.rep2_btn.setText(_translate("MainWindow", "2"))
self.rep6_btn.setText(_translate("MainWindow", "6"))
self.rep3_btn.setText(_translate("MainWindow", "3"))
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_())
and my main python file is :
from LoginWindowUI import Ui_LoginWindow
from MainWindowUI import Ui_MainWindow
from PyQt5 import QtCore,QtGui,QtWidgets
import sys
import mysql.connector
open_main_window_flag = False
class Main_Window():
def __init__(self):
app = QtWidgets.QApplication(sys.argv)
self.MWindow = QtWidgets.QMainWindow()
self.ui_MWindow = Ui_MainWindow()
self.ui_MWindow.setupUi(self.MWindow)
self.MWindow.show()
sys.exit(app.exec_())
class login():
def __init__(self):
app = QtWidgets.QApplication(sys.argv)
self.LoginWindow = QtWidgets.QMainWindow()
self.ui_login = Ui_LoginWindow()
self.ui_login.setupUi(self.LoginWindow)
self.ui_login.login_btn.clicked.connect(self.loginform)
self.authentication_flag = False
self.LoginWindow.show()
sys.exit(app.exec_())
def loginform(self):
global open_main_window_flag
db_user = self.ui_login.username_ldt.text()
db_pass = self.ui_login.password_ldt.text()
if(db_user == 'admin' and db_pass=='admin'):
print('Welcome')
self.authentication_flag = True
else:
print('Try Again')
self.authentication_flag = False
if self.authentication_flag:
self.LoginWindow.close()
open_main_window_flag = True
if __name__ == '__main__':
mylogin = login()
if open_main_window_flag:
mymainwindow = Main_Window()
I want that when user enter "admin","admin" in username and password this window colsed and open second ui file .
You have the following errors:
There can only be one object of the QApplication, you are creating one for each window and that is incorrect.
Avoid using global variables, they are considered a bad practice and few cases are necessary. For more information read: Why are global variables evil?
Qt Designer does not offer a widget but a class that serves to fill a widget so PyQt recommend to create a suitable widget class and fill it with that class, for more information read: http://pyqt.sourceforge.net/Docs/PyQt5/designer.html
Going to the point, you can not use a flag because the process is asynchronous, in Qt you work with signals, that's the correct way to inform that there is a change, for example a way to keep the classes separate is to create a signal that indicates that you have logged in, close the window and open the other window.
from LoginWindowUI import Ui_LoginWindow
from MainWindowUI import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
class Main_Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Main_Window, self).__init__(parent)
self.setupUi(self)
class LoginWindow(QtWidgets.QMainWindow, Ui_LoginWindow):
logged = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(LoginWindow, self).__init__(parent)
self.setupUi(self)
self.login_btn.clicked.connect(self.authenticate)
#QtCore.pyqtSlot()
def authenticate(self):
db_user = self.username_ldt.text()
db_pass = self.password_ldt.text()
if db_user == 'admin' and db_pass=='admin':
self.logged.emit()
self.close()
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
login = LoginWindow()
w = Main_Window()
login.logged.connect(w.show)
login.show()
sys.exit(app.exec_())
if __name__ == '__main__': main()
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've been working on this code as part of my school project and i don't seem to be getting anywhere. My problem is when i try add the text from a file to my QlistWidget my program crashes. Firstly I want to know if this will add the files at the directory (each line in movieDir.txt is a directory) to the QlistWidget, secondly I'm not even sure if I'm using the right widget or if it should be QlistView. Also how would i make it so the currently selected item in the listWidget shows details in the text box (e.g. size of the file).
I'm also looking for this to be done on start up so if someone can please tell me where to the the addItems(self) that would be greatly appreciated. I'm new to using classes as we have been neglected to be taught this in school.
I'm running PyQt5 on windows 7
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'input.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(764, 500)
MainWindow.setMinimumSize(QtCore.QSize(764, 353))
MainWindow.setMaximumSize(QtCore.QSize(764, 500))
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralWidget)
self.verticalLayout.setContentsMargins(11, 11, 11, 11)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout_2.setSpacing(6)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.pushButton_5 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_2.addWidget(self.pushButton_5)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.verticalLayout_4 = QtWidgets.QVBoxLayout()
self.verticalLayout_4.setContentsMargins(11, 11, 11, 11)
self.verticalLayout_4.setSpacing(6)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout_4.setSpacing(6)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.listView = QtWidgets.QListView(self.centralWidget)
self.listView.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.listView.setViewMode(QtWidgets.QListView.ListMode)
self.listView.setObjectName("listView")
self.horizontalLayout_4.addWidget(self.listView)
self.textEdit = QtWidgets.QTextEdit(self.centralWidget)
self.textEdit.setObjectName("textEdit")
self.horizontalLayout_4.addWidget(self.textEdit)
self.verticalLayout_4.addLayout(self.horizontalLayout_4)
self.verticalLayout.addLayout(self.verticalLayout_4)
self.horizontalSlider = QtWidgets.QSlider(self.centralWidget)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.verticalLayout.addWidget(self.horizontalSlider)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setContentsMargins(11, 11, 11, 11)
self.verticalLayout_2.setSpacing(6)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout.setSpacing(6)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton_3 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout.addWidget(self.pushButton_3)
self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout_3.setSpacing(6)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.dial = QtWidgets.QDial(self.centralWidget)
self.dial.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.dial.setMouseTracking(True)
self.dial.setObjectName("dial")
self.horizontalLayout_3.addWidget(self.dial)
self.pushButton_4 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_3.addWidget(self.pushButton_4)
self.horizontalLayout.addLayout(self.horizontalLayout_3)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.verticalLayout.addLayout(self.verticalLayout_2)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 764, 21))
self.menuBar.setObjectName("menuBar")
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(MainWindow)
self.mainToolBar.setObjectName("mainToolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(MainWindow)
self.statusBar.setObjectName("statusBar")
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton_5.clicked.connect(self.addItem)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_5.setText(_translate("MainWindow", "Show Movies"))
self.pushButton_3.setText(_translate("MainWindow", "Fast-Forward"))
self.pushButton_2.setText(_translate("MainWindow", "Pause/Play"))
self.pushButton.setText(_translate("MainWindow", "Rewind"))
self.pushButton_4.setText(_translate("MainWindow", "Full Screen"))
def addItem(self):
with open('movieDir.txt', 'r') as movieDir:
for row in movieDir:
listWidget.addItem(row)
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_())
self.listWidget = QtWidgets.QListWidget(self.centralWidget)
self.listWidget.setObjectName("listWidget")
self.horizontalLayout_4.addWidget(self.listWidget)
with open('moviesDir.txt','r') as movieDir:
for movie in movieDir:
self.listWidget.addItem(movie)
This did what i asked, but it only shows it as text, is there a way to show the files at the directory instead of the directory as a string?
#This is the example code for loading files and content inside the file to QtGui.QListWidget
#It is PyQt4, but you can try with PyQt5 with small changes.
#If your are not expecting this answer, sorry.
import sys, os
from PyQt4 import QtGui, QtCore
class Window (QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.verticalLayout = QtGui.QVBoxLayout (self)
self.verticalLayout.setObjectName ('verticalLayout')
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName('horizontalLayout')
self.listWidget = QtGui.QListWidget(self)
self.listWidget.setObjectName('listView')
self.listWidget.setAlternatingRowColors (True)
self.horizontalLayout.addWidget(self.listWidget)
self.verticalLayout1 = QtGui.QVBoxLayout()
self.verticalLayout1.setSpacing(10)
self.verticalLayout1.setObjectName('verticalLayout')
self.pushButton = QtGui.QPushButton(self)
self.pushButton.setObjectName('pushButton')
self.pushButton.setText('Load File Content')
self.pushButton_2 = QtGui.QPushButton(self)
self.pushButton_2.setObjectName('pushButton_2')
self.pushButton_2.setText('Load File')
self.verticalLayout1.addWidget(self.pushButton)
self.verticalLayout1.addWidget(self.pushButton_2)
spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.verticalLayout1.addItem (spacerItem)
self.horizontalLayout.addLayout(self.verticalLayout1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.pushButton.clicked.connect (self.loadFileContent)
self.pushButton_2.clicked.connect (self.loadFiles)
def loadFileContent (self) :
openFiles = QtGui.QFileDialog.getOpenFileName (self, 'Open File', 'c:/', 'txt (*.txt)')
if openFiles :
data = open (str(openFiles), 'r')
dataList = data.readlines ()
self.listWidget.clear ()
for eachLine in dataList :
if len(eachLine.strip())!=0 :
self.listWidget.addItem(eachLine.strip())
def loadFiles (self) :
getDirectory = QtGui.QFileDialog.getExistingDirectory(self, 'Browse', 'C:/')
if getDirectory :
fileList = os.listdir (str(getDirectory))
if fileList :
self.listWidget.clear ()
for eachFile in fileList :
self.listWidget.addItem (eachFile)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
#Thanks,
#Subin Gopi