simulating save into menubar using Qtest - pyqt

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()

Related

Why pushButton don't connect?

When i'm closing window, pushButtons don't connecting
I want window to be open, when first is closed
But pushButtons don't connecting
It is test.py
import sys
import sqlite3
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QMainWindow
from easter import Ui_Form
from window_3 import App_2
class App(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Test")
self.show()
def closeEvent(self, event):
Example().__init__()
class Example(QMainWindow, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
# It is don't working
self.buttonGroup.buttonClicked.connect(self.add)
self.show()
def add(self, sender):
# It doesn't matter
con = sqlite3.connect("top.db")
cur = con.cursor()
idd = list(cur.execute("select id from Top").fetchall())
a = cur.execute('update top set score = ? where id = ?', (int(sender.text()), idd[-1][0]))
con.commit()
App_2().__init__()
self.close()

pyqt5 trying to use QGridLayout to organise my QLabel, QLineEdit, QPushButton, and "Pop-up" QLabel

I am trying to get the "Game Name:" (QLabel), input box (QLineEdit), and QPushButton on one line and the "Pop-up" QLabel) to appear on the bottom
but am having difficulties with get QGridLayout to work
With this code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QLineEdit, QGridLayout, QGroupBox, QDialog
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("Project PiBu!!")
self.createGridLayout()
self.windowLayout = QVBoxLayout()
self.windowLayout.addWidget(self.horizontalGroupBox)
self.setLayout(self.windowLayout)
self.game_name = QLabel("Game Name:", self)
self.game_line_edit = QLineEdit(self)
self.search_button = QPushButton("Search", self)
self.search_button.clicked.connect(self.on_click)
self.game = QLabel(self)
self.show()
def createGridLayout(self):
self.horizontalGroupBox = QGroupBox()
self.layout = QGridLayout()
self.layout.setColumnStretch(1, 4)
self.layout.setColumnStretch(2, 4)
self.layout.addWidget(self.game_name, 0, 0)
self.layout.addWidget(self.game_line_edit, 0, 1)
self.layout.addWidget(self.search_button, 0, 2)
self.layout.addWidget(self.game, 1, 0)
self.horizontalGroupBox.setLayout(layout)
#pyqtSlot()
def on_click(self):
self.game.setText(self.game_line_edit.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
I am getting this error:
AttributeError: 'Window' object has no attribute 'game_name'
Why?
have a feeling its something simple rather than something more complicated but maybe I'm wrong
Please help!!!
Thank you!
You call the createGridLayout method earlier than you define the variables used in it.
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QHBoxLayout,
QVBoxLayout, QPushButton, QLabel, QLineEdit,
QGridLayout, QGroupBox, QDialog)
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("Project PiBu!!")
# self.createGridLayout()
# self.windowLayout = QVBoxLayout()
# self.windowLayout.addWidget(self.horizontalGroupBox)
# self.setLayout(self.windowLayout)
self.game_name = QLabel("Game Name:", self)
self.game_line_edit = QLineEdit(self)
self.search_button = QPushButton("Search", self)
self.search_button.clicked.connect(self.on_click)
self.game = QLabel(self)
self.createGridLayout() # < --
self.windowLayout = QVBoxLayout() # < --
self.windowLayout.addWidget(self.horizontalGroupBox) # < --
self.setLayout(self.windowLayout) # < --
self.show()
def createGridLayout(self):
self.horizontalGroupBox = QGroupBox()
self.layout = QGridLayout()
self.layout.setColumnStretch(1, 4)
self.layout.setColumnStretch(2, 4)
# AttributeError: 'Window' object has no attribute 'game_name'
self.layout.addWidget(self.game_name, 0, 0)
self.layout.addWidget(self.game_line_edit, 0, 1)
self.layout.addWidget(self.search_button, 0, 2)
self.layout.addWidget(self.game, 1, 0)
self.horizontalGroupBox.setLayout(self.layout) # +++ self.
#pyqtSlot()
def on_click(self):
self.game.setText(self.game_line_edit.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

Spin box not showing up

I'm unsure why all aspects of my GUI are showing up apart from the spin box (the code for it is within the home function).
I've tried moving it to the init(self) function, but that doesn't work. I thought it would be intuitive for it to be within the home function as that is where all my other GUI (e.g. buttons) resides.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox
from temperature import MplWindow
from filament import MplWindow1
from highvoltage import MplWindow2
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Temperature Control')
self.setWindowIcon(QIcon('adn.png'))
extractAction = QAction('&Quit', self)
extractAction.setShortcut('Ctrl+Q')
extractAction.setStatusTip('leave the app')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(extractAction)
self.matplWindow = MplWindow()
self.matplWindow1 = MplWindow1()
self.matplWindow2 = MplWindow2()
self.home()
def home(self):
btn = QPushButton('quit', self)
btn.clicked.connect(self.close_application)
btn.resize(btn.sizeHint())
btn.move(200, 260)
button = QPushButton('Temperature',self)
button.clicked.connect(self.opengraph)
button.move(100,50)
button = QPushButton('Filament voltage',self)
button.clicked.connect(self.openfilament)
button.move(100,80)
button = QPushButton('High voltage',self)
button.clicked.connect(self.openhigh)
button.move(100,110)
self.doubleSpinBox = QtWidgets.QDoubleSpinBox()
self.doubleSpinBox.setGeometry(180, 110, 62, 22)
self.show()
def opengraph(self):
self.matplWindow.funAnimation()
def openfilament(self):
self.matplWindow1.funAnimation1()
def openhigh(self):
self.matplWindow2.funAnimation2()
def close_application(self):
choice = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if choice == QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
Gui = window()
sys.exit(app.exec_())
I worked it out - I moved the code to the init function.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox, QLabel, QVBoxLayout
from temperature import MplWindow # +++
from filament import MplWindow1
from highvoltage import MplWindow2
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Temperature Control')
self.setWindowIcon(QIcon('adn.png'))
extractAction = QAction('&Quit', self)
extractAction.setShortcut('Ctrl+Q')
extractAction.setStatusTip('leave the app')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(extractAction)
self.matplWindow = MplWindow() # +++
self.matplWindow1 = MplWindow1()
self.matplWindow2 = MplWindow2()
# vBoxLayout = QVBoxLayout()
self.label = QLabel("Set point Temp:", self)
self.label.move(50,150)
self.spinBox = QDoubleSpinBox(self)
self.spinBox.move(70,150)
self.home()
def home(self):
btn = QPushButton('quit', self)
btn.clicked.connect(self.close_application)
btn.resize(btn.sizeHint())
btn.move(200, 260)
button = QPushButton('Temperature',self)
button.clicked.connect(self.opengraph)
button.move(100,50)
button = QPushButton('Filament voltage',self)
button.clicked.connect(self.openfilament)
button.move(100,80)
button = QPushButton('High voltage',self)
button.clicked.connect(self.openhigh)
button.move(100,110)
self.show()
def opengraph(self):
self.matplWindow.funAnimation() # +++
def openfilament(self):
self.matplWindow1.funAnimation1()
def openhigh(self):
self.matplWindow2.funAnimation2()
def close_application(self):
choice = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if choice == QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
Gui = window()
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_()

Open dialog ( window) form file PyQt5

( 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_())

Resources