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
Related
I'm having this problem and I have no ideea why is not working.
I'm pretty new to python so I will appreciate if you can help me
It's a very simple GUI, first page open another page when clicking on a button and on a second page I need to open a browse window and take the path as string
Thats the Main Page
from PyQt5 import QtCore, QtGui, QtWidgets
from testGui import Ui_Test
class Ui_PragrammingGUI(object):
def open_test_window(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_Test()
self.ui.setupUi(self.window)
self.ui.get_all_serials()
self.window.show()
def setupUi(self, PragrammingGUI):
PragrammingGUI.setObjectName("PragrammingGUI")
PragrammingGUI.resize(237, 177)
self.centralwidget = QtWidgets.QWidget(PragrammingGUI)
self.centralwidget.setObjectName("centralwidget")
self.test_2 = QtWidgets.QPushButton(self.centralwidget)
self.test_2.setGeometry(QtCore.QRect(60, 100, 121, 31))
self.test_2.setObjectName("test_2")
self.test_2.clicked.connect(self.open_test_window)
PragrammingGUI.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(PragrammingGUI)
self.menubar.setGeometry(QtCore.QRect(0, 0, 237, 20))
self.menubar.setObjectName("menubar")
PragrammingGUI.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(PragrammingGUI)
self.statusbar.setObjectName("statusbar")
PragrammingGUI.setStatusBar(self.statusbar)
self.retranslateUi(PragrammingGUI)
QtCore.QMetaObject.connectSlotsByName(PragrammingGUI)
def retranslateUi(self, PragrammingGUI):
_translate = QtCore.QCoreApplication.translate
PragrammingGUI.setWindowTitle(_translate("PragrammingGUI", "Programming"))
self.test_2.setText(_translate("PragrammingGUI", "TEST"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
PragrammingGUI = QtWidgets.QMainWindow()
ui = Ui_PragrammingGUI()
ui.setupUi(PragrammingGUI)
PragrammingGUI.show()
sys.exit(app.exec_())
and the one which have browse in it is (ignore the get_all_serials method):
import sys
import os
from cameraID import CameraID
from PyQt5.QtWidgets import QFileDialog, QDialog
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Test(QDialog):
def browse_file_src(self):
print('in the function')
file_name = QFileDialog.getOpenFileName(self, 'Open file', '/home/qauser/')
self.lineEdit.setText(file_name[0])
def get_all_serials(self):
self.serial = CameraID()
self.serial.getAllSerial()
return_list = self.serial.getAllSerial()
if len(return_list) > 0:
for i in return_list:
self.comboBox.addItem(i)
else:
self.comboBox.addItem("None")
def setupUi(self, Test):
Test.setObjectName("Test")
Test.resize(397, 108)
self.centralwidget = QtWidgets.QWidget(Test)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 10, 291, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setPlaceholderText("Chose file")
self.browseBtn = QtWidgets.QPushButton(self.centralwidget)#, clicked=lambda: self.browseBtn)
self.browseBtn.setGeometry(QtCore.QRect(310, 10, 80, 23))
self.browseBtn.setObjectName("browseBtn")
self.browseBtn.clicked.connect(self.browseBtn)
self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton.setGeometry(QtCore.QRect(10, 40, 51, 21))
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton_2.setGeometry(QtCore.QRect(60, 40, 71, 21))
self.radioButton_2.setObjectName("radioButton_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(310, 40, 80, 23))
self.pushButton.setObjectName("pushButton")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(120, 40, 181, 23))
self.comboBox.setObjectName("comboBox")
Test.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Test)
self.menubar.setGeometry(QtCore.QRect(0, 0, 397, 20))
self.menubar.setObjectName("menubar")
Test.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Test)
self.statusbar.setObjectName("statusbar")
Test.setStatusBar(self.statusbar)
self.retranslateUi(Test)
QtCore.QMetaObject.connectSlotsByName(Test)
def retranslateUi(self, Test):
_translate = QtCore.QCoreApplication.translate
Test.setWindowTitle(_translate("Test", "MainWindow"))
self.browseBtn.setText(_translate("Test", "Browse"))
self.radioButton.setText(_translate("Test", "Ok"))
self.radioButton_2.setText(_translate("Test", "Failed"))
self.pushButton.setText(_translate("Test", "Start"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Test = QtWidgets.QMainWindow()
ui = Ui_Test()
ui.setupUi(Test)
Test.show()
sys.exit(app.exec_())
I think the issue is this: self.browseBtn.clicked.connect(self.browseBtn).
You are trying to connect the click event to the button itself.
You probably want to do something like: self.browseBtn.clicked.connect(self.method_to_call_when_clicked) where method_to_call_when_clicked is replaced by the name of the method you wish to call when it is clicked.
An example already in your code is:
self.test_2.clicked.connect(self.open_test_window)
I have two windows, first window is 'FirstWindow' and second is 'Calendar'. first window has one push button (pbSelectDate) and one label (lbDate). calendar window has calendar (CalendarBox) and one push button (pbSelect).
when i click select (pbSelectDate) from first window, calendar window pops up. I select the date and press select button (pbSelect) in the calendar window. i want to print the selected date to the label (lbDate) in the first window.
thanks in advance. :)
this is the first window script.
from PyQt5 import QtCore, QtGui, QtWidgets
from Calendar import Ui_CalendarWindow
class Ui_FirstWindow(object):
def Open_Calendar(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_CalendarWindow()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(654, 242)
self.centralwidget = QtWidgets.QWidget(FirstWindow)
self.centralwidget.setObjectName("centralwidget")
self.lbDate = QtWidgets.QLabel(self.centralwidget)
self.lbDate.setGeometry(QtCore.QRect(330, 70, 281, 131))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.lbDate.setFont(font)
self.lbDate.setLayoutDirection(QtCore.Qt.LeftToRight)
self.lbDate.setObjectName("lbDate")
self.pbSelectDate = QtWidgets.QPushButton(self.centralwidget)
self.pbSelectDate.setGeometry(QtCore.QRect(80, 100, 191, 61))
self.pbSelectDate.setObjectName("pbSelectDate")
self.pbSelectDate.clicked.connect(self.Open_Calendar)
FirstWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(FirstWindow)
self.statusbar.setObjectName("statusbar")
FirstWindow.setStatusBar(self.statusbar)
self.retranslateUi(FirstWindow)
QtCore.QMetaObject.connectSlotsByName(FirstWindow)
def retranslateUi(self, FirstWindow):
_translate = QtCore.QCoreApplication.translate
FirstWindow.setWindowTitle(_translate("FirstWindow", "MainWindow"))
self.lbDate.setText(_translate("FirstWindow", "Sep"))
self.pbSelectDate.setText(_translate("FirstWindow", "Select Date"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
FirstWindow = QtWidgets.QMainWindow()
ui = Ui_FirstWindow()
ui.setupUi(FirstWindow)
FirstWindow.show()
sys.exit(app.exec_())
this is the Calendar window script..
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_CalendarWindow(object):
def PickedDate(self,var):
self.selecteddate = self.CalendarBox.selectedDate()
#print(self.selecteddate.toString('MMM')+'-'+self.selecteddate.toString('yyyy'))
def setupUi(self, CalendarWindow):
CalendarWindow.setObjectName("CalendarWindow")
CalendarWindow.resize(512, 458)
self.centralwidget = QtWidgets.QWidget(CalendarWindow)
self.centralwidget.setObjectName("centralwidget")
self.CalendarBox = QtWidgets.QCalendarWidget(self.centralwidget)
self.CalendarBox.setGeometry(QtCore.QRect(20, 20, 464, 289))
self.CalendarBox.setObjectName("CalendarBox")
self.pbSelect = QtWidgets.QPushButton(self.centralwidget)
self.pbSelect.setGeometry(QtCore.QRect(160, 330, 181, 60))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pbSelect.setFont(font)
self.pbSelect.setObjectName("pbSelect")
self.pbSelect.clicked.connect(self.PickedDate)
CalendarWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(CalendarWindow)
self.statusbar.setObjectName("statusbar")
CalendarWindow.setStatusBar(self.statusbar)
self.retranslateUi(CalendarWindow)
QtCore.QMetaObject.connectSlotsByName(CalendarWindow)
def retranslateUi(self, CalendarWindow):
_translate = QtCore.QCoreApplication.translate
CalendarWindow.setWindowTitle(_translate("CalendarWindow", "MainWindow"))
self.pbSelect.setText(_translate("CalendarWindow", "Select"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
CalendarWindow = QtWidgets.QMainWindow()
ui = Ui_CalendarWindow()
ui.setupUi(CalendarWindow)
CalendarWindow.show()
sys.exit(app.exec_())
Do not modify the code generated by Qt Designer but create another class that inherits from the appropriate widget and use the initial class to fill it.
QLabel.setText(const QString )
Setting the text clears any previous content.
from PyQt5 import QtCore, QtGui, QtWidgets
#from Calendar import Ui_CalendarWindow
class Ui_CalendarWindow(object):
def setupUi(self, CalendarWindow):
CalendarWindow.setObjectName("CalendarWindow")
CalendarWindow.resize(512, 458)
self.centralwidget = QtWidgets.QWidget(CalendarWindow)
self.centralwidget.setObjectName("centralwidget")
self.CalendarBox = QtWidgets.QCalendarWidget(self.centralwidget)
self.CalendarBox.setGeometry(QtCore.QRect(20, 20, 464, 289))
self.CalendarBox.setObjectName("CalendarBox")
self.pbSelect = QtWidgets.QPushButton(self.centralwidget)
self.pbSelect.setGeometry(QtCore.QRect(160, 330, 181, 60))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pbSelect.setFont(font)
self.pbSelect.setObjectName("pbSelect")
# self.pbSelect.clicked.connect(self.PickedDate)
CalendarWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(CalendarWindow)
self.statusbar.setObjectName("statusbar")
CalendarWindow.setStatusBar(self.statusbar)
self.retranslateUi(CalendarWindow)
QtCore.QMetaObject.connectSlotsByName(CalendarWindow)
def retranslateUi(self, CalendarWindow):
_translate = QtCore.QCoreApplication.translate
CalendarWindow.setWindowTitle(_translate("CalendarWindow", "MainWindow"))
self.pbSelect.setText(_translate("CalendarWindow", "Select"))
class Ui_FirstWindow(object):
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(654, 242)
self.centralwidget = QtWidgets.QWidget(FirstWindow)
self.centralwidget.setObjectName("centralwidget")
self.lbDate = QtWidgets.QLabel(self.centralwidget)
self.lbDate.setGeometry(QtCore.QRect(330, 70, 281, 131))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.lbDate.setFont(font)
self.lbDate.setLayoutDirection(QtCore.Qt.LeftToRight)
self.lbDate.setObjectName("lbDate")
self.pbSelectDate = QtWidgets.QPushButton(self.centralwidget)
self.pbSelectDate.setGeometry(QtCore.QRect(80, 100, 191, 61))
self.pbSelectDate.setObjectName("pbSelectDate")
# self.pbSelectDate.clicked.connect(self.Open_Calendar)
FirstWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(FirstWindow)
self.statusbar.setObjectName("statusbar")
FirstWindow.setStatusBar(self.statusbar)
self.retranslateUi(FirstWindow)
QtCore.QMetaObject.connectSlotsByName(FirstWindow)
def retranslateUi(self, FirstWindow):
_translate = QtCore.QCoreApplication.translate
FirstWindow.setWindowTitle(_translate("FirstWindow", "MainWindow"))
self.lbDate.setText(_translate("FirstWindow", "Sep"))
self.pbSelectDate.setText(_translate("FirstWindow", "Select Date"))
class CalendarWindow(QtWidgets.QMainWindow, Ui_CalendarWindow): # +
def __init__(self):
super(CalendarWindow, self).__init__()
self.setupUi(self)
class FirstWindow(QtWidgets.QMainWindow, Ui_FirstWindow): # +
def __init__(self):
super(FirstWindow, self).__init__()
self.setupUi(self)
self.pbSelectDate.clicked.connect(self.Open_Calendar)
def Open_Calendar(self):
self.window = CalendarWindow()
self.window.setupUi(self.window)
self.window.show()
self.window.pbSelect.clicked.connect(self.PickedDate)
def PickedDate(self): # , var
self.selecteddate = self.window.CalendarBox.selectedDate()
# print(self.selecteddate.toString('MMM')+'-'+self.selecteddate.toString('yyyy'))
self.lbDate.setText(self.selecteddate.toString('ddd-MMM-yyyy')) # <---
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = FirstWindow()
w.show()
sys.exit(app.exec_())
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_())
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.8.2
#
# 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(733, 497)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.My_2 = QtWidgets.QPushButton(self.centralWidget)
self.My_2.setGeometry(QtCore.QRect(0, 30, 101, 41))
self.My_2.setStyleSheet("")
self.My_2.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("../../Major Project/Add a game button 1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.My_2.setIcon(icon)
self.My_2.setIconSize(QtCore.QSize(100, 100))
self.My_2.setObjectName("My_2")
self.My_3 = QtWidgets.QPushButton(self.centralWidget)
self.My_3.setGeometry(QtCore.QRect(0, 80, 101, 41))
self.My_3.setObjectName("My_3")
self.My_3.clicked.connect(self.editbutton) #EDIT BUTTON
self.My_4 = QtWidgets.QPushButton(self.centralWidget)
self.My_4.setGeometry(QtCore.QRect(60, 260, 31, 31))
self.My_4.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("../../Major Project/settings button.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.My_4.setIcon(icon1)
self.My_4.setIconSize(QtCore.QSize(32, 32))
self.My_4.setObjectName("My_4")
self.My_5 = QtWidgets.QPushButton(self.centralWidget)
self.My_5.setGeometry(QtCore.QRect(0, 210, 101, 41))
self.My_5.setObjectName("My_5")
self.line_3 = QtWidgets.QFrame(self.centralWidget)
self.line_3.setGeometry(QtCore.QRect(100, 0, 20, 341))
self.line_3.setFrameShape(QtWidgets.QFrame.VLine)
self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_3.setObjectName("line_3")
self.timeEdit = QtWidgets.QTimeEdit(self.centralWidget)
self.timeEdit.setGeometry(QtCore.QRect(0, 300, 101, 22))
self.timeEdit.setObjectName("timeEdit")
self.scrollArea = QtWidgets.QScrollArea(self.centralWidget)
self.scrollArea.setGeometry(QtCore.QRect(130, 30, 571, 271))
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 552, 269))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.graphicsView_2 = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
self.graphicsView_2.setGeometry(QtCore.QRect(160, 30, 101, 81))
self.graphicsView_2.setObjectName("graphicsView_2")
self.graphicsView = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
self.graphicsView.setGeometry(QtCore.QRect(30, 30, 101, 81))
self.graphicsView.setObjectName("graphicsView")
self.line_2 = QtWidgets.QFrame(self.scrollAreaWidgetContents)
self.line_2.setGeometry(QtCore.QRect(10, 10, 531, 20))
self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_2.setObjectName("line_2")
self.pushButton = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton.setGeometry(QtCore.QRect(40, 110, 81, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton_2.setGeometry(QtCore.QRect(170, 110, 81, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 733, 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)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.My_3.setText(_translate("MainWindow", "Edit"))
self.My_5.setText(_translate("MainWindow", "Help"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
def editbutton(self):
if __name__ == "__main__":
import sys, editui
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = editui.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
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_())
I need help with this: the window keeps exiting when My_3 button is pressed, after both windows opening for a split second. editui.py is a generic auto-generated file with PyQt, so there's that.
If anyone can help me out, thanks!
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_())