I have followed the specific kind of import rather than writing *.
from PyQt4.QtGui
What else am I missing?
import sys
from PyQt4.QtGui import QMainWindow, QSizePolicy, QWidget, QVBoxLayout, QAction,\
QKeySequence, QLabel, QItemSelectionModel, QMessageBox, QFileDialog, QFrame, \
QDockWidget, QProgressBar, QProgressDialog
from PyQt4.QtCore import SIGNAL, QSettings, QSize, QPoint, QVariant, QFileInfo, QTimer, pyqtSignal, QObject
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent)
self.scene = QtGui.QGraphicsScene(self)
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
self.setScene(self.scene)
self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
self.viewport().setCursor(QtCore.Qt.CrossCursor)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self._pan = False
self._draw = False
self._moved = False
self._sel = False
self.pen = None
self.penid = None
self.cmap = None
self.penwidth = 4
self._redoStack = []
self._histStates = []
self._baseRects = []
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
I added the following lines to the code and that solved the error:
from PyQt4 import QtGui
from PyQt4 import QtCore
It looks like this now:
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtGui import QMainWindow, QSizePolicy, QWidget, QVBoxLayout, QAction,\
QKeySequence, QLabel, QItemSelectionModel, QMessageBox, QFileDialog, QFrame, \
QDockWidget, QProgressBar, QProgressDialog
from PyQt4.QtCore import SIGNAL, QSettings, QSize, QPoint, QVariant, QFileInfo, QTimer, pyqtSignal, QObject
Related
If you enter an age less than 18 or more than 80 and click on the button, a modal window will light up, but the button will be in the pressing mode and will turn dark, the same with RadioButton will only be highlighted in blue, is it possible to remove it somehow? When you click on another field, everything is fine
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt,QEvent, Signal)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform, QFocusEvent)
from PySide6.QtWidgets import (QApplication, QGridLayout, QHBoxLayout, QLabel,
QLineEdit, QMainWindow, QSizePolicy, QWidget, QRadioButton, QPushButton, QVBoxLayout, QMessageBox)
class QTApp(QWidget):
def __init__(self):
super(QTApp, self).__init__()
self.LE_sample_input_01 = QLineEdit()
self.LE_sample_input_02 = QLineEdit()
self.LE_sample_input_01.setPlaceholderText('Age')
self.RadioButton = QRadioButton('Something')
self.Button = QPushButton('Click')
self.Button.setStyleSheet("QPushButton:pressed {background-color: #b3b3ba;}")
layout = QVBoxLayout(self)
layout.addWidget(self.LE_sample_input_01)
layout.addWidget(self.LE_sample_input_02)
layout.addWidget(self.RadioButton)
layout.addWidget(self.Button)
self.LE_sample_input_01.installEventFilter(self)
def eventFilter(self, obj, event):
if event.type() == QEvent.FocusOut and QFocusEvent.reason(event) == Qt.MouseFocusReason:
if obj is self.LE_sample_input_01:
try:
age = int(self.LE_sample_input_01.text())
if age < 18 or age > 80:
error = QMessageBox()
error.setWindowTitle('Age error')
error.setText('Age entered incorrectly (from 18 to 80 years)')
error.setIcon(QMessageBox.Warning)
error.addButton('Ok',QMessageBox.AcceptRole)
error.exec()
obj.setFocus()
obj.selectAll()
return True
except: pass
return False
if __name__ == "__main__":
app = QApplication()
qt_app = QTApp()
qt_app.show()
app.exec()
from PyQt5 import QtMultimedia, QtMultimediaWidgets
from PyQt5.QtCore import QDir, Qt, QUrl, QSizeF
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer, QMediaPlaylist
from PyQt5.QtMultimediaWidgets import QVideoWidget, QGraphicsVideoItem
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget, QStackedWidget,
QStackedLayout, QGraphicsView, QGraphicsScene)
from PyQt5.QtWidgets import QMainWindow,QWidget, QPushButton, QAction
from PyQt5.QtGui import QIcon
import sys, glob
class VerySimpleMediaPlayer(QWidget):
def __init__(self, parent):
super().__init__(parent)
self.media_player = QtMultimedia.QMediaPlayer(self)
self.media_widget = QtMultimediaWidgets.QVideoWidget(self)
self.media_player.setVideoOutput(self.media_widget)
self.media_widget.show()
self.media_player.setMedia(QMediaContent(QUrl.fromLocalFile('avideo.mp4')))
self.media_player.mediaStatusChanged.connect(self.look)
layout = QVBoxLayout()
self.real = QStackedWidget()
self.button = QPushButton()
self.button.setText('Push Me PLS!')
self.real.addWidget(self.button)
self.button.clicked.connect(self.clicked)
self.real.addWidget(self.media_widget)
layout.addWidget(self.real)
self.setLayout(layout)
self.awidget = QWidget()
bx = QVBoxLayout()
lbl = QLabel()
lbl.setText('Video Has Finished!')
bx.addWidget(lbl)
self.awidget.setLayout(bx)
self.real.addWidget(self.awidget)
def clicked(self):
self.media_player.play()
self.real.setCurrentIndex(self.real.currentIndex() + 1)
def look(self, state):
if state == 7:
self.real.setCurrentIndex(self.real.currentIndex() + 1)
app = QApplication(sys.argv)
player = QMainWindow()
player.setCentralWidget(VerySimpleMediaPlayer(player))
player.show()
sys.exit(app.exec_())
It works good but the only problem is that the videos are playing horizontally even if the video is a vertical video like 800 x 1600.
Is there any way to rotate videos either automatically or non-automatically?
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QButtonGroup, QMainWindow, QPushButton, QTextEdit, QWidget, QSizeGrip, QLabel, QFormLayout, QScrollArea, QGroupBox, QVBoxLayout, QDialog, QHBoxLayout, QLineEdit
from PyQt5.QtGui import *
from PyQt5.QtCore import QRect
import sys
class main_window(QMainWindow):
def __init__ (self):
super(main_window, self).__init__()
self.setWindowTitle("Polynomial Equation Solver")
self.setFixedSize(957, 681)
self.setStyleSheet("background-image : url(Background1.jpg)")
degree_textbox = QTextEdit(self)
degree_textbox.setGeometry(460, 25, 200, 40)
degree_textbox.setStyleSheet("background-image : url(Text boxes background.jpg)")
app = QApplication(sys.argv)
window=main_window()
window.show()
app.exec_()
I am trying to identify object name and perform some operation. is there any name to make sure if we are in proper widget. or identify widget name
#!/usr/bin/python
from PySide2.QtCore import Qt, QEvent
from PySide2.QtWidgets import QApplication
from PySide2.QtGui import (QTextCharFormat, QIcon, QKeySequence,
QBrush, QColor, QTextDocument, QFont,
QTextCursor, QTextBlockFormat, QFontDatabase)
from PySide2.QtWidgets import (QPlainTextEdit, QSizePolicy, QApplication, QLabel, QGridLayout, QMessageBox, QToolBar, QTextEdit, QCheckBox, QAction,
QTableWidget, QTableWidgetItem, QHeaderView, QMenu,
QWidget)
from PySide2.QtCore import Qt, QSize, QRegExp, QFile, QTextStream, QRect
from PySide2.QtWidgets import (QMainWindow, QVBoxLayout,
QPlainTextEdit, QGridLayout, QGroupBox,
QListWidget, QHBoxLayout, QLabel, QLineEdit,
QMenuBar, QPushButton, QMessageBox, QDialog,
QTextEdit, QVBoxLayout, QWidget, QFormLayout,
QCheckBox, QDialogButtonBox,
QTableWidget, QTableWidgetItem, QHeaderView)
import sys
class Window(QMainWindow):
def __init__(self, app):
super().__init__()
def create_widget(self):
self.plain_textedit = QPlainTextEdit()
self.plain_textedit.setMouseTracking(True)
self.plain_textedit.viewport().installEventFilter(self)
self.another_textedit = QPlainTextEdit()
self.another_textedit.setMouseTracking(True)
self.another_textedit.viewport().installEventFilter(self)
main_layout = QVBoxLayout()
main_layout.addWidget(self.plain_textedit)
main_layout.addWidget(self.another_textedit)
self.window = QWidget()
self.window.setLayout(main_layout)
self.setCentralWidget(self.window)
def eventFilter(self, obj, event):
if obj is self.plain_textedit.viewport():
print ("plain_1")
elif obj is self.another_textedit.viewport():
print ("plain_2")
#Reseting format for single click when user use shift+click select data
def main():
app = QApplication([])
window = Window(app)
window.create_widget()
window.show()
app.exec_()
if __name__ == "__main__":
sys.exit(main())
After my previous problem, I have my widgets on my main window. But this lasts doesn't come with any data: the datagridview haven't columns and the treeview are invisible or without my hard coded items. Here's my code:
app.py
#!/usr/bin/env python
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, qApp, QWidget, QMainWindow, QGridLayout, QMenuBar, QAction, QToolBar, QStatusBar
from views import Main
class TerraSoft(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle('TerraSoft')
self.setWindowState(Qt.WindowMaximized)
exitAct = QAction(QIcon('exit24.png'), 'Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.triggered.connect(qApp.quit)
fileMenu = self.menuBar().addMenu('File')
fileMenu.addAction(exitAct)
toolbar = self.addToolBar('Main')
toolbar.addAction(exitAct)
main = Main()
self.setCentralWidget(main)
self.statusBar().showMessage('Bienvenue dans TerraSoft')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = TerraSoft()
ex.show()
sys.exit(app.exec_())
.views.py
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, qApp, QAction, QSplitter, QMenuBar, QToolBar, QGridLayout, QStatusBar
from Family.views import FamilyTreeView
from Specie.views import EventsTableView
class Main(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
familyTreeView = FamilyTreeView(self)
eventsTableView = EventsTableView(self)
HSplitter = QSplitter(Qt.Horizontal)
HSplitter.addWidget(familyTreeView)
HSplitter.addWidget(eventsTableView)
grid = QGridLayout()
grid.addWidget(HSplitter)
self.setLayout(grid)
module: Family.views.py
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QApplication, QWidget, QTreeView
families = [
("Craspedocephalus", [
("puniceus", []),
("trigonocephalus", [])
]),
("Trimeresurus", [
("albolabris", [])
]),
("Elapidé", [])
]
class FamilyTreeView(QWidget):
"""description of class"""
def __init__(self, *args):
QWidget.__init__(self, *args)
self.familyList = QTreeView()
self.familyList.setMaximumWidth(300)
self.model = QStandardItemModel()
self.addItems(self.model, families)
self.familyList.setModel(self.model)
self.model.setHorizontalHeaderLabels([self.tr("Familles")])
def addItems(self, parent, elements):
for text, children in elements:
item = QStandardItem(text)
parent.appendRow(item)
if children:
self.addItems(item, children)
module: specie.views.py
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTreeView
class EventsTableView(QTableWidget):
"""description of class"""
def __init__(self, *args):
QTableWidget.__init__(self, *args)
self.eventsTable = QTableWidget()
self.eventsTable.setColumnCount(3)
self.eventsTable.setHorizontalHeaderLabels(('Date', 'Catégorie', 'Description'))
enter code here
The result of this code gives:
hope you can help me again,
thanks per advance !
Not really sure why you want to derive FamilyTreeView from QWidget. I worked out a solution deriving it from QTreeView, directly. Your addItems method works correctly, but I used invisibleRootItem member of QStandardItem as the upper parent (i.e. root).
class FamilyTreeView(QTreeView):
"""description of class"""
def __init__(self, *args):
QTreeView.__init__(self, *args)
the_model = QStandardItemModel()
the_model.setHorizontalHeaderLabels([self.tr("Familles")])
self.addItems(the_model.invisibleRootItem(), families)
self.setModel(the_model)
def addItems(self, parent, elements):
for text, children in elements:
item = QStandardItem(text)
parent.appendRow(item)
if children:
self.addItems(item, children)
Your EventsTableView class has some issues too. It inherits from QTableWidget (i.e. is a QTableWidget) but yet defines an inner QTableWidget, which is unnecessary. Here is a better version:
class EventsTableView(QTableWidget):
"""description of class"""
def __init__(self, *args):
QTableWidget.__init__(self, *args)
self.setColumnCount(3)
self.setHorizontalHeaderLabels(('Date', 'Catégorie', 'Description'))