Open dialog ( window) form file PyQt5 - python-3.x

( How can i link two file ( two dialog) )
when i click on Open Second i want to go to another page ( window)
Here is the code for two file
Home.py
from PyQt5 import QtGui
import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
btn = QPushButton('Open Second', self)
if __name__ == '__main__':
app = QApplication(sys.argv)
MW = MainWindow()
MW.show()
sys.exit(app.exec_())
SecondWindow.py
from PyQt5 import QtGui
import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel
class SecondWindow(QMainWindow):
def __init__(self):
super(SecondWindow, self).__init__()
if __name__ == '__main__':
app = QApplication(sys.argv)
MW = SecondWindow()
MW.show()
sys.exit(app.exec_())

Related

QObject: Cannot create children for a parent that is in a different thread. (Multithreading pyqt5)

I am trying to edit the text in 'QTextEdit' but I got the error which is in the title above. I know that I shouldn't edit the widgets in the main app from another thread and I searched for some answers, But I didn't get the full answer that I want
This is a sample that produce the same error that I have
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore
import sys
import time
import threading
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.move(50, 220)
self.resize(500, 500)
self.text_edit = QTextEdit(self)
self.text_edit.resize(200, 200)
self.btn = QPushButton('start', self)
self.btn.move(10, 250)
self.btn.clicked.connect(lambda x : self.thread_creator(self.test))
self.btn2 = QPushButton('print', self)
self.btn2.move(10, 290)
self.btn2.clicked.connect(lambda x : print('dad'))
def test(self):
for i in range(99):
self.text_edit.setText(f'dad {i}')
time.sleep(0.1)
def thread_creator(self, function):
self.thread = threading.Thread(target=function)
self.thread.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
ex.show()
sys.exit(app.exec_())

Issue in Pyqt with next and previous Pages

so I'm currently working on a little project but I have an issue and all what I've tried did not work. I have 2 files :
Page1test :
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QPushButton, QWidget, QLabel
import sys
from page2test import Page2
class Page1(QWidget):
def __init__(self):
super(Page1, self).__init__()
self.setWindowTitle("Page 1")
label1 = QLabel(self)
label1.setText("\n PAGE 1")
self.btn_inMyApp = QPushButton ('Next page', self)
self.btn_inMyApp.setGeometry(1500,800,275,125)
self.btn_inMyApp.clicked.connect(self.closePage1_OpenPage2)
self.show()
def closePage1_OpenPage2(self):
self.Open = Page2()
self.Open.showMaximized()
self.close()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Page1()
window.showMaximized()
sys.exit(app.exec_())
And page2test :
from PyQt5.QtWidgets import QPushButton, QWidget, QLabel
from Page1test import Page1
class Page2(QWidget):
def __init__(self):
super(Page2, self).__init__()
self.setWindowTitle("Test window principale")
label1 = QLabel(self)
label1.setText("\n Page2")
self.btn_inMyApp = QPushButton ('previous Page', self)
self.btn_inMyApp.setGeometry(1500,800,275,125)
self.btn_inMyApp.clicked.connect(self.closePage2_OpenPage1)
self.show()
def closePage2_OpenPage1(self):
self.Open = Page1()
self.Open.showMaximized()
self.close()
I run the code of Page1test : empty window with just a Qpushbutton "Next Page", goal : we click on it and it open Page 2 (and close Page 1). And, when we are in Page 2, we have A Qpushbutton with "Previous Page" and when we click on it, it opens page 1, and close Page 2. Like a loop.
But, when I run the code, it returns an error :
cannot import name 'Page2' from partially initialized module 'page2test' (most likely due to a circular import)
and I have no idea how to fix it...
If someone had an idea, it would be really helpful.
So, I've finally found the solution, that was not so difficult in the end. Here it is (if it can help someone) :
Instead of making 2 files, I've done just 1 file. Here is the code :
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QPushButton, QWidget, QLabel, QMainWindow
import sys
class MyApp(QWidget):
def __init__(self):
super(MyApp, self).__init__()
self.setWindowTitle("Page 1")
label1 = QLabel(self)
label1.setText("\n PAGE 1")
self.btn_inMyApp = QPushButton ('Page suivante', self)
self.btn_inMyApp.setGeometry(1500,800,275,125)
self.btn_inMyApp.clicked.connect(self.closePage1_OpenPage2)
self.show()
def btn1_onClicked(self):
pass
def closePage1_OpenPage2(self):
self.Open = NewApp()
self.Open.showMaximized()
self.close()
class NewApp(QMainWindow):
def __init__(self):
super(NewApp, self).__init__()
self.setWindowTitle("Test window principale")
label1 = QLabel(self)
label1.setText("\n Page2")
self.btn_inMyApp = QPushButton ('previous Page', self)
self.btn_inMyApp.setGeometry(1500,800,275,125)
self.btn_inMyApp.clicked.connect(self.closePage2_OpenPage1)
self.show()
def closePage2_OpenPage1(self):
self.Open = MyApp()
self.Open.showMaximized()
self.close()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.showMaximized()
sys.exit(app.exec_())
```

simulating save into menubar using Qtest

I am trying to simulate save functionality(Tool-Save) using Qtest but not able to find any pointer
#!/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.create_menu()
self.plain_textedit = QPlainTextEdit()
self.plain_textedit.setMouseTracking(True)
self.plain_textedit.viewport().installEventFilter(self)
main_layout = QVBoxLayout()
main_layout.addWidget(self.toolbar)
main_layout.addWidget(self.plain_textedit)
self.window = QWidget()
self.window.setLayout(main_layout)
self.setCentralWidget(self.window)
def create_menu(self):
"""
create menu bar into window
"""
self.toolbar = QToolBar()
self.toolbar.setStyleSheet("QToolBar {background: #ff8000}")
self.addToolBar(Qt.TopToolBarArea, self.toolbar)
menu = QMenu("&Tool", self)
self.menuBar().addMenu(menu)
self.actionSave = QAction(QIcon.fromTheme('document-save'),
"&Save", self, shortcut=QKeySequence(Qt.CTRL+Qt.Key_W),
triggered=self.save_data, enabled=True)
self.toolbar.addAction(self.actionSave)
menu.addAction(self.actionSave)
menu.addSeparator()
def save_data(self):
pass
def eventFilter(self, obj, event):
if obj is self.plain_textedit.viewport():
print ("plain_1")
def main():
app = QApplication([])
window = Window(app)
window.create_widget()
window.show()
app.exec_()
if __name__ == "__main__":
sys.exit(main())
#!/usr/bin/python
import os
env = os.environ
import unittest
import logging
from PySide2.QtWidgets import QApplication
from PySide2.QtTest import QTest
from PySide2.QtCore import Qt, QEvent
from debug2 import Window
class test(unittest.TestCase):
def setUp(self):
self.app = QApplication.instance()
if self.app is None:
self.app = QApplication([])
self.window = Window(self.app)
self.window.create_widget()
self.window.show()
#to open gui and helpful for debug
#self.app.exec_()
def tearDown(self):
self.app.deleteLater()
def test_save_data(self):
actions = self.window.menuBar().actions()
for action in actions:
sub_commands = action.menu().actions()
for sub_command in sub_commands:
if sub_command.text() == "&Save":
print (dir(QTest))
QTest.mouseClick(self.window.window, Qt.LeftButton)
#print (sub_command, sub_command.objectName(), sub_command.text())
if __name__ == '__main__':
unittest.main()

Disable mouse pointer in QGraphicsView

I want to disable the mouse pointer in a QGraphicsView.
What line of code do I need to add in the following example?
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGraphicsView
class GraphicsWindow(QGraphicsView):
def __init__(self, parent=None):
super().__init__(parent)
self.showFullScreen()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
graphics_window = GraphicsWindow()
graphics_window.show()
sys.exit(app.exec_())
Qt::BlankCursor A blank/invisible cursor, typically used when the cursor shape needs to be hidden.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGraphicsView
class GraphicsWindow(QGraphicsView):
def __init__(self, parent=None):
super().__init__(parent)
self.showFullScreen()
self.setCursor(Qt.BlankCursor) # < ------
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
graphics_window = GraphicsWindow()
graphics_window.show()
sys.exit(app.exec_())

pyqt5 video widget and table widget side by side

I want to have a table widget side by side to a video widget and a webview widget. The video and webview widget to be stacked vertically.
I tried to first have table widget and video widget side by side but the video widget is being hidden by the table widget. I'm able to hear audio so the video seems to be running but just doesn't seem to be displaying the video part.
What is wrong in the code?
Pasting the sample code. I'm yet to add the web widget.
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import QDir, Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QTableWidget,QVBoxLayout,
QTableWidgetItem, QLabel, QHBoxLayout,QGridLayout)
class Window(QWidget):
def __init__(self,):
super().__init__()
table1 = QTableWidget()
table1.setRowCount(2)
table1.setColumnCount(2)
table1.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
table1.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
table1.setItem(1,0, QTableWidgetItem("Cell (2,1)"))
table1.setItem(1,1, QTableWidgetItem("Cell (2,2)"))
self.VideoWidget = QVideoWidget()
self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.player.setMedia(QMediaContent(QUrl.fromLocalFile("test.mp4")))
self.player.play()
self.player.setVideoOutput(self.VideoWidget)
self.layout = QHBoxLayout()
self.layout.addWidget(table1)
self.layout.addWidget(self.VideoWidget)
self.setLayout(self.layout)
self.move(0,0)
self.resize(320, 240)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
app.exec_()
I was able to solve the problem by using QSplitter.
The code is as below:
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import QDir, Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QTableWidget,QVBoxLayout,
QTableWidgetItem, QHBoxLayout,QSplitter,QGroupBox)
from PyQt5.QtWebKit import *
from PyQt5.QtWebKitWidgets import *
class Window(QWidget):
def __init__(self,):
super().__init__()
self.v_layout = QVBoxLayout(self)
self.splitter = QSplitter(QtCore.Qt.Horizontal)
self.left = QGroupBox('Left')
self.table1 = QTableWidget()
self.table1.setRowCount(2)
self.table1.setColumnCount(2)
self.table1.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
self.table1.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
self.table1.setItem(1,0, QTableWidgetItem("Cell (2,1)"))
self.table1.setItem(1,1, QTableWidgetItem("Cell (2,2)"))
self.left_layout = QVBoxLayout(self.left)
self.left_layout.addWidget(self.table1)
self.right = QGroupBox('Right')
self.VideoWidget = QVideoWidget()
self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.player.setMedia(QMediaContent(QUrl.fromLocalFile("test.mp4")))
self.player.play()
self.player.setVideoOutput(self.VideoWidget)
self.webview = QWebView()
url = "https://www.google.com"
self.webview.load(QUrl(url))
self.right_layout = QVBoxLayout(self.right)
self.right_layout.addWidget(self.webview)
self.right_layout.addWidget(self.VideoWidget)
self.splitter.addWidget(self.left)
self.splitter.addWidget(self.right)
self.v_layout.addWidget(self.splitter)
self.resize(840,680)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
app.exec_()

Resources