I'm working on a music player app, but i came across a problem which I don't know how to solve. I want audio to fade in in given interval (0.6 second for example) when audio is started or unpaused and also fade out in the same interval when it is paused. I tried doing it with QThread to keep my ui responsive while gradually lowering volume with for loop (iterating 10 times and every time lowering volume and sleeping for certaing time, 0.6 / 10 seconds). But it doesn't work, buttons are still unclickable and update only after fade in or fade out. If anyone managed to solve a problem like this please help, would greatly appreciate it! Here's simple code for playing audio in pyqt:
import sys
from PyQt6 import QtWidgets
from PyQt6.QtCore import QUrl
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QStyle
class MainWindowUi(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._setup_ui()
self.setWindowTitle('sound fading')
self.setGeometry(100, 100, 100, 100)
def _setup_ui(self):
self.central_widget = QWidget(self)
self.central_widget_layout = QVBoxLayout(self.central_widget)
self.setCentralWidget(self.central_widget)
self.player = AudioPlayer()
self.user_action = -1 # 0 - stopped, 1 - playing, 2 - paused
self.play_button = QPushButton()
self.play_button.clicked.connect(self.play_pause_button_clicked)
self.play_icon = self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPlay)
self.pause_icon = self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPause)
self.play_button.setIcon(self.play_icon)
self.central_widget_layout.addWidget(self.play_button)
def play(self):
print("Play")
self.play_button.setIcon(self.pause_icon)
self.user_action = 1
self.player.setSource(QUrl("Some audio file path goes here."))
self.player.play()
def pause(self):
print("Pause")
self.play_button.setIcon(self.play_icon)
self.user_action = 2
self.player.pause()
def unpause(self):
print("Unpause")
self.play_button.setIcon(self.pause_icon)
self.user_action = 1
self.player.play()
def play_pause_button_clicked(self):
if self.user_action <= 0:
self.play()
elif self.user_action == 1:
self.pause()
elif self.user_action == 2:
self.unpause()
class AudioPlayer(QMediaPlayer):
def __init__(self, parent=None):
super().__init__(parent)
self.audio_output = QAudioOutput()
self.setAudioOutput(self.audio_output)
self.audioOutput().setVolume(0.3)
self.current_volume = self.audio_output.volume()
def play(self):
super().play()
def pause(self):
super().pause()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindowUi()
mainWindow.show()
sys.exit(app.exec())
So I made it work with QPropertyAnimation! Still working on key values and easing curves but does is much, much closer to the result I want.
import sys
from PyQt6 import QtWidgets
from PyQt6.QtCore import QUrl, QPropertyAnimation, QEasingCurve
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QStyle
from audio_player import AudioPlayer
class MainWindowUi(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._setup_ui()
self.setWindowTitle('sound fading')
self.setGeometry(100, 100, 100, 100)
def _setup_ui(self):
self.central_widget = QWidget(self)
self.central_widget_layout = QVBoxLayout(self.central_widget)
self.setCentralWidget(self.central_widget)
self.player = AudioPlayer()
self.user_action = -1 # 0 - stopped, 1 - playing, 2 - paused
self.play_button = QPushButton()
self.play_button.clicked.connect(self.play_pause_button_clicked)
self.play_icon = self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPlay)
self.pause_icon = self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPause)
self.play_button.setIcon(self.play_icon)
self.central_widget_layout.addWidget(self.play_button)
def play(self):
print("Play")
self.play_button.setIcon(self.pause_icon)
self.user_action = 1
self.player.setSource(QUrl("some audio file path"))
self.player.play()
def pause(self):
print("Pause")
self.play_button.setIcon(self.play_icon)
self.user_action = 2
self.player.pause()
def unpause(self):
print("Unpause")
self.play_button.setIcon(self.pause_icon)
self.user_action = 1
self.player.play()
def play_pause_button_clicked(self):
if self.user_action <= 0:
self.play()
elif self.user_action == 1:
self.pause()
elif self.user_action == 2:
self.unpause()
class AudioPlayer(QMediaPlayer):
def __init__(self, parent=None):
super().__init__(parent)
self.audio_output = QAudioOutput()
self.audio_output.volumeChanged.connect(lambda: print(self.audio_output.volume()))
self.setAudioOutput(self.audio_output)
self.audioOutput().setVolume(.5)
self.current_volume = self.audio_output.volume()
self.fade_in_anim = QPropertyAnimation(self.audio_output, b"volume")
self.fade_in_anim.setDuration(1400)
self.fade_in_anim.setStartValue(0.01)
self.fade_in_anim.setEndValue(self.current_volume)
self.fade_in_anim.setEasingCurve(QEasingCurve.Type.Linear)
self.fade_in_anim.setKeyValueAt(0.01, 0.01)
self.fade_out_anim = QPropertyAnimation(self.audio_output, b"volume")
self.fade_out_anim.setDuration(600)
self.fade_out_anim.setStartValue(self.current_volume)
self.fade_out_anim.setEndValue(0)
self.fade_out_anim.setEasingCurve(QEasingCurve.Type.Linear)
self.fade_out_anim.setKeyValueAt(0.01, self.current_volume)
self.fade_out_anim.finished.connect(super().pause)
def play(self):
self.audio_output.setVolume(0.01)
self.fade_in_anim.setEndValue(self.current_volume)
super().play()
self.fade_in_anim.start()
def pause(self):
self.current_volume = self.audio_output.volume()
self.fade_out_anim.setStartValue(self.current_volume)
self.fade_out_anim.start()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindowUi()
mainWindow.show()
sys.exit(app.exec())
Related
I'm making a custom QTreeView with QFileSystem model, and I have a MouseMoveEvent set up to print the path of the item that is hovered over.
I'm way down the rabbit hole and doing all kinds of weird things to make this work.
Here is the latest minimal reproducible code:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.resize(500, 300)
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.myList = CustomTreeWidget()
self.myList.model.setRootPath("/Volumes/Home/User/Desktop/testsrc")
self.myList.setObjectName("/Volumes/Home/User/Desktop/testsrc")
self.layout.addWidget(self.myList)
class CustomTreeWidget(QTreeView):
def __init__(self):
super().__init__()
self.model = QFileSystemModel()
self.model.setFilter(QDir.NoDotAndDotDot | QDir.Files)
self.setModel(self.model)
self.setAlternatingRowColors(True)
self.setDragDropMode(QAbstractItemView.DragDrop)
self.setIndentation(0)
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.setDragEnabled(True)
self.setAcceptDrops(True)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.setMouseTracking(True)
self.model.directoryLoaded.connect(self._runwhenloaded)
def _runwhenloaded(self):
self.setRootIndex(self.model.index(self.objectName()))
self.model.setRootPath(self.objectName())
def mouseMoveEvent(self, event):
prev = ""
if self.selectedIndexes():
prev = self.selectedIndexes()[0]
x = event.x()
y = event.y()
self.setSelection(QRect(x, y, 1, 1), QItemSelectionModel.ClearAndSelect)
self.setCurrentIndex(self.selectedIndexes()[0])
print(self.model.filePath(self.currentIndex()))
if prev:
self.setCurrentIndex(prev)
# pos = QCursor.pos()
# indexat = self.indexAt(pos).row() # why -1?
# print(indexat) # why -1?
# print(self.indexAt(pos).row())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Obviously this example is not proper at all, as it destroys multiple selections and scrolls to the previously selected item whenever the mouse moves, and just a hack in general.
I've gone through many iterations and read everything I could put my hands on but I can't figure it out.
The closest answer seems to be HERE, but it's in C and I don't understand it.
So the question is: How to print the file path when the mouse hovers over an item in this QTreeView?
A possible solution is to create an event filter that tracks the hover events and, according to that information, emits a signal that has the QModelIndex:
import sys
from PyQt5.QtCore import (
pyqtSignal,
pyqtSlot,
Qt,
QDir,
QEvent,
QModelIndex,
QObject,
QPersistentModelIndex,
QStandardPaths,
)
from PyQt5.QtWidgets import (
QAbstractItemView,
QApplication,
QFileSystemModel,
QMainWindow,
QTreeView,
)
from PyQt5 import sip
class HoverViewHelper(QObject):
hovered = pyqtSignal(QModelIndex)
def __init__(self, view):
super().__init__(view)
self._current_index = QPersistentModelIndex()
if not isinstance(view, QAbstractItemView):
raise TypeError(f"The {view} must be of type QAbstractItemView")
self._view = view
self.view.viewport().setAttribute(Qt.WA_Hover)
self.view.viewport().installEventFilter(self)
#property
def view(self):
return self._view
def eventFilter(self, obj, event):
if sip.isdeleted(self.view):
return True
if self.view.viewport() is obj:
if event.type() in (QEvent.HoverMove, QEvent.HoverEnter):
p = event.pos()
index = self.view.indexAt(p)
self._update_index(index)
elif event.type() == QEvent.HoverLeave:
if self._current_index.isValid():
self._update_index(QModelIndex())
return super().eventFilter(obj, event)
def _update_index(self, index):
pindex = QPersistentModelIndex(index)
if pindex != self._current_index:
self._current_index = pindex
self.hovered.emit(QModelIndex(self._current_index))
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.model = QFileSystemModel()
self.model.setRootPath(QDir.rootPath())
self.view = QTreeView()
self.view.setModel(self.model)
path = QStandardPaths.writableLocation(QStandardPaths.DocumentsLocation)
self.view.setRootIndex(self.model.index(path))
self.setCentralWidget(self.view)
helper = HoverViewHelper(self.view)
helper.hovered.connect(self.handle_hovered)
#pyqtSlot(QModelIndex)
def handle_hovered(self, index):
if not index.isValid():
return
path = self.model.filePath(index)
print(f"path: {path}")
def main():
app = QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
app.exec_()
if __name__ == "__main__":
main()
I have an application where I run some process in a second thread. At some points during this process, given a certain condition is met, another dialog window opens, which halts the process until you confirm something. This causes the following error message:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x1f9c82383d0), parent's thread is QThread(0x1f9c7ade2a0), current thread is QThread(0x1f9c8358800)
Interestingly, if you also move your cursor over the MainWindow while the process is running, and before the new dialog pops up, it also produces this error message a couple of times:
QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
Very strange. Because it only occurs if you move your cursor over the MainWindow.
Now, in my application, I actually load an interface for the new dialog that pops up using PyQt5.uic.loadUi, and this hasn't caused any problems. However, when I was creating the example for this post, another issue occurred, due to the fact that I was setting the layout of the new dialog during its initialization:
QObject::setParent: Cannot set parent, new parent is in a different thread
Which results in the application crashing:
Process finished with exit code -1073741819 (0xC0000005)
I'm obviously doing something wrong here regarding the threading I would guess, but I don't know what. I am especially baffled by the fact that I cannot set the layout of the new dialog during its initialization, while using loadUi is totally fine. Here is my example code:
import sys
import time
import numpy as np
from PyQt5.QtCore import QObject, pyqtSignal, QThread
from PyQt5.QtWidgets import (
QDialog, QApplication, QPushButton, QGridLayout, QProgressBar, QLabel
)
class SpecialDialog(QDialog):
def __init__(self):
super().__init__()
btn = QPushButton('pass variable')
btn.clicked.connect(self.accept)
layout = QGridLayout()
layout.addWidget(btn)
# self.setLayout(layout)
self.variable = np.random.randint(0, 100)
class Handler(QObject):
progress = pyqtSignal(int)
finished = pyqtSignal(int)
def __init__(self):
super().__init__()
self._isRunning = True
self._success = False
def run(self):
result = None
i = 0
while i < 100 and self._isRunning:
if i == np.random.randint(0, 100):
dialog = SpecialDialog()
dialog.exec_()
result = dialog.variable
time.sleep(0.01)
i += 1
self.progress.emit(i)
if i == 100:
self._success = True
self.finished.emit(result)
def stop(self):
self._isRunning = False
class MainWindow(QDialog):
def __init__(self):
super().__init__()
btn = QPushButton('test')
btn.clicked.connect(self.run_test)
self.pbar = QProgressBar()
self.resultLabel = QLabel('Result:')
layout = QGridLayout(self)
layout.addWidget(btn)
layout.addWidget(self.pbar)
layout.addWidget(self.resultLabel)
self.setLayout(layout)
self.handler = None
self.handler_thread = QThread()
self.result = None
def run_test(self):
self.handler = Handler()
self.handler.moveToThread(self.handler_thread)
self.handler.progress.connect(self.progress)
self.handler.finished.connect(self.finisher)
self.handler_thread.started.connect(self.handler.run)
self.handler_thread.start()
def progress(self, val):
self.pbar.setValue(val)
def finisher(self, result):
self.result = result
self.resultLabel.setText(f'Result: {result}')
self.pbar.setValue(0)
self.handler.stop()
self.handler.progress.disconnect(self.progress)
self.handler.finished.disconnect(self.finisher)
self.handler_thread.started.disconnect(self.handler.run)
self.handler_thread.terminate()
self.handler = None
if __name__ == '__main__':
app = QApplication(sys.argv)
GUI = MainWindow()
GUI.show()
sys.exit(app.exec_())
EDIT
I forgot to mention that I already found this post, which may be related to my problem, however, I don't undestand the reasoning of the solution in the top answer, and more importantly, I don't speak what I believe is C++.
If we analyze the case before equality happens it is the same case after that equality happens unless the initial progress is different, with this it indicates that when equality happens a signal must be sent to the GUI to ask for the information, and when you have that information, launch the same task but with an initial progress equal to what you had before the signal was emitted.
from functools import partial
import sys
import time
import numpy as np
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, QThread, QTimer
from PyQt5.QtWidgets import (
QDialog,
QApplication,
QPushButton,
QGridLayout,
QProgressBar,
QLabel,
)
class SpecialDialog(QDialog):
def __init__(self):
super().__init__()
btn = QPushButton("pass variable")
btn.clicked.connect(self.accept)
layout = QGridLayout()
layout.addWidget(btn)
# self.setLayout(layout)
self.variable = np.random.randint(0, 100)
class Handler(QObject):
progress = pyqtSignal(int)
finished = pyqtSignal()
resultChanged = pyqtSignal(int)
requestSignal = pyqtSignal()
def __init__(self):
super().__init__()
self._isRunning = True
self._success = False
self.iter = 0
self.result = None
#pyqtSlot()
def start(self):
self._isRunning = True
self._success = False
self.iter = 0
self.result = None
self.task()
#pyqtSlot()
#pyqtSlot(int)
def task(self, value=None):
if value is not None:
self.result = value
while self.iter < 100 and self._isRunning:
if self.iter == np.random.randint(0, 100):
self.requestSignal.emit()
return
time.sleep(0.01)
self.iter += 1
self.progress.emit(self.iter)
if self.iter == 100:
self._success = True
if self.result:
self.resultChanged.emit(self.result)
self.finished.emit()
def stop(self):
self._isRunning = False
class MainWindow(QDialog):
def __init__(self):
super().__init__()
btn = QPushButton("test")
btn.clicked.connect(self.run_test)
self.pbar = QProgressBar()
self.resultLabel = QLabel("Result:")
layout = QGridLayout(self)
layout.addWidget(btn)
layout.addWidget(self.pbar)
layout.addWidget(self.resultLabel)
self.setLayout(layout)
self.handler = None
self.handler_thread = QThread()
self.result = None
def run_test(self):
self.handler = Handler()
self.handler.moveToThread(self.handler_thread)
self.handler.progress.connect(self.progress)
self.handler.resultChanged.connect(self.on_result_changed)
self.handler.finished.connect(self.on_finished)
self.handler.requestSignal.connect(self.onRequestSignal)
self.handler_thread.started.connect(self.handler.start)
self.handler_thread.start()
def progress(self, val):
self.pbar.setValue(val)
def onRequestSignal(self):
dialog = SpecialDialog()
dialog.exec_()
wrapper = partial(self.handler.task, dialog.variable)
QTimer.singleShot(0, wrapper)
#pyqtSlot(int)
def on_result_changed(self, result):
self.result = result
self.resultLabel.setText(f"Result: {result}")
#pyqtSlot()
def on_finished(self):
self.pbar.setValue(0)
self.handler.stop()
self.handler_thread.quit()
self.handler_thread.wait()
if __name__ == "__main__":
app = QApplication(sys.argv)
GUI = MainWindow()
GUI.show()
sys.exit(app.exec_())
I have an application where I run some process in a second thread and at some point, given a certain condition, another dialog window opens, which halts the process until you confirm something. This causes the following error message:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x1f9c82383d0), parent's thread is QThread(0x1f9c7ade2a0), current thread is QThread(0x1f9c8358800)
Interestingly, if you also move your cursor over the MainWindow while the process is running, and before the new dialog pops up, it also produces this error message a couple of times:
QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
Very strange. Because it only occurs if you move your cursor over the MainWindow.
Now, in my application, I actually load an interface for the new dialog that pops up using PyQt5.uic.loadUi, and this hasn't caused any problems. However, when I was creating the example for this post, another issue occurred, due to the fact that I was setting the layout of the new dialog during its initialization:
QObject::setParent: Cannot set parent, new parent is in a different thread
Which results in the application crashing:
Process finished with exit code -1073741819 (0xC0000005)
I'm obviously doing something wrong here regarding the threading I would guess, but I don't know what. I am especially baffled by the fact that I cannot set the layout of the new dialog during its initialization, while using loadUi is totally fine. Here is my example code:
import sys
import time
import numpy as np
from PyQt5.QtCore import QObject, pyqtSignal, QThread
from PyQt5.QtWidgets import (
QDialog, QApplication, QPushButton, QGridLayout, QProgressBar, QLabel
)
class SpecialDialog(QDialog):
def __init__(self):
super().__init__()
btn = QPushButton('pass variable')
btn.clicked.connect(self.accept)
layout = QGridLayout()
layout.addWidget(btn)
# self.setLayout(layout)
self.variable = np.random.randint(0, 100)
class Handler(QObject):
progress = pyqtSignal(int)
finished = pyqtSignal(int)
def __init__(self):
super().__init__()
self._isRunning = True
self._success = False
def run(self):
result = None
i = 0
while i < 100 and self._isRunning:
if i == 50:
dialog = SpecialDialog()
dialog.exec_()
result = dialog.variable
time.sleep(0.01)
i += 1
self.progress.emit(i)
if i == 100:
self._success = True
self.finished.emit(result)
def stop(self):
self._isRunning = False
class MainWindow(QDialog):
def __init__(self):
super().__init__()
btn = QPushButton('test')
btn.clicked.connect(self.run_test)
self.pbar = QProgressBar()
self.resultLabel = QLabel('Result:')
layout = QGridLayout(self)
layout.addWidget(btn)
layout.addWidget(self.pbar)
layout.addWidget(self.resultLabel)
self.setLayout(layout)
self.handler = None
self.handler_thread = QThread()
self.result = None
def run_test(self):
self.handler = Handler()
self.handler.moveToThread(self.handler_thread)
self.handler.progress.connect(self.progress)
self.handler.finished.connect(self.finisher)
self.handler_thread.started.connect(self.handler.run)
self.handler_thread.start()
def progress(self, val):
self.pbar.setValue(val)
def finisher(self, result):
self.result = result
self.resultLabel.setText(f'Result: {result}')
self.pbar.setValue(0)
self.handler.stop()
self.handler.progress.disconnect(self.progress)
self.handler.finished.disconnect(self.finisher)
self.handler_thread.started.disconnect(self.handler.run)
self.handler_thread.terminate()
self.handler = None
if __name__ == '__main__':
app = QApplication(sys.argv)
GUI = MainWindow()
GUI.show()
sys.exit(app.exec_())
EDIT
I forgot to mention that I already found this post, which may be related to my problem, however, I don't undestand the reasoning of the solution in the top answer, and more importantly, I don't speak what I believe is C++.
You cannot create or modify a GUI element from a secondary thread and this is signaled by the error message.
You have to redesign the Handler class, with your requirement you must divide run into 2 methods, the first method will generate progress up to 50% where the GUI will open the dialogue, obtain the result and launch the second method.
import sys
import time
import numpy as np
from functools import partial
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, QThread, QTimer
from PyQt5.QtWidgets import (
QDialog,
QApplication,
QPushButton,
QGridLayout,
QProgressBar,
QLabel,
)
class SpecialDialog(QDialog):
def __init__(self):
super().__init__()
btn = QPushButton("pass variable")
btn.clicked.connect(self.accept)
layout = QGridLayout()
layout.addWidget(btn)
# self.setLayout(layout)
self.variable = np.random.randint(0, 100)
class Handler(QObject):
progress = pyqtSignal(int)
finished = pyqtSignal(int)
def __init__(self):
super().__init__()
self._isRunning = True
self._success = False
#pyqtSlot()
def task1(self):
i = 0
while i <= 50 and self._isRunning:
time.sleep(0.01)
i += 1
self.progress.emit(i)
#pyqtSlot(int)
def task2(self, result):
i = 50
while i < 100 and self._isRunning:
time.sleep(0.01)
i += 1
self.progress.emit(i)
if i == 100:
self._success = True
self.finished.emit(result)
def stop(self):
self._isRunning = False
class MainWindow(QDialog):
def __init__(self):
super().__init__()
btn = QPushButton("test")
btn.clicked.connect(self.run_test)
self.pbar = QProgressBar()
self.resultLabel = QLabel("Result:")
layout = QGridLayout(self)
layout.addWidget(btn)
layout.addWidget(self.pbar)
layout.addWidget(self.resultLabel)
self.setLayout(layout)
self.handler = None
self.handler_thread = QThread()
self.result = None
def run_test(self):
self.handler = Handler()
self.handler.moveToThread(self.handler_thread)
self.handler.progress.connect(self.progress)
self.handler.finished.connect(self.finisher)
self.handler_thread.started.connect(self.handler.task1)
self.handler_thread.start()
#pyqtSlot(int)
def progress(self, val):
self.pbar.setValue(val)
if val == 50:
dialog = SpecialDialog()
dialog.exec_()
result = dialog.variable
wrapper = partial(self.handler.task2, result)
QTimer.singleShot(0, wrapper)
def finisher(self, result):
self.result = result
self.resultLabel.setText(f"Result: {result}")
self.pbar.setValue(0)
self.handler.stop()
self.handler_thread.quit()
self.handler_thread.wait()
if __name__ == "__main__":
app = QApplication(sys.argv)
GUI = MainWindow()
GUI.show()
sys.exit(app.exec_())
I'm making a first come first served scheduler using pyqt5.
I have the main window which includes textbox asking the user for scheduler type, when button "GO" is clicked, the second window is opened which asks the user for more information about the scheduler like process numbers and burst time .
When button "GO" on the second window is clicked, a Gantt chart should appear after processing the information.
The problem is that the button on the second window is not working at all.
I have found a similar question, but I didn't quite got the solution, plus I'm not using JSON.
Code:
import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import QMainWindow,QApplication, QComboBox, QDialog,QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox, QHBoxLayout,QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox, QTextEdit,QVBoxLayoutQSpinBox, QTextEdit, QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
import matplotlib.pyplot as plt
import numpy as np
import linked_list
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My Program")
self.setWindowIcon(QtGui.QIcon("download.jpg"))
self.setGeometry(50,50,500,300)
self.home()
self.show()
def home(self):
self.label2=QLabel(self)
self.label2.setText("Type of Scheduler")
self.label2.move(10,50)
self.textbox2=QLineEdit(self)
self.textbox2.move(100,50)
self.button=QPushButton("Go",self)
self.button.move(0,200)
self.button.clicked.connect(self.runcode)
def runcode(self):
schedular_type=self.textbox2.text()
if(schedular_type=="FCFS"):
self.close()
self.fcfs=Window2(self)
Self.fcfs.__init__()
class Window2(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My Program")
self.setWindowIcon(QtGui.QIcon("download.jpg"))
self.setGeometry(50,50,500,300)
self.home()
self.show()
def home(self):
self.label1=QLabel(self)
self.label1.setText("No of Processes")
self.label1.move(10,0) #col ,row
self.textbox=QLineEdit(self)
self.textbox.move(100,0)
self.label3=QLabel(self)
self.label3.setText("Processess Names")
self.label3.move(10,100)
self.label4=QLabel(self)
self.label4.setText("Burst Time")
self.label4.move(120,100)
self.label5=QLabel(self)
self.label5.setText("Arrival Time")
self.label5.move(200,100)
self.names=QLineEdit(self)
self.names.move(10,150)
# self.names.resize(100,160)
self.burst=QLineEdit(self)
self.burst.move(120,150)
#self.burst.resize(100,160)
self.arrival=QLineEdit(self)
self.arrival.move(250 ,150)
#self.arrival.resize(100,160)
#self.textEdit=QTextEdit(self)
#self.textEdit.move(20,250)
self.button=QPushButton("Go",self)
self.button.move(0,200)
self.button.clicked.connect(self.fcfs)
def fcfs(self):
//
def main():
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Set a parent to second window, also you are calling ____init____ method of Window2 twice.
Here is the fixed code :
import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import (QMainWindow, QApplication, QComboBox,
QDialog, QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox,
QHBoxLayout, QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox,
QTextEdit, QVBoxLayout)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
import matplotlib.pyplot as plt
import numpy as np
import linked_list
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My Program")
self.setWindowIcon(QtGui.QIcon("download.jpg"))
self.setGeometry(50, 50, 500, 300)
self.home()
self.show()
def home(self):
self.label2 = QLabel(self)
self.label2.setText("Type of Scheduler")
self.label2.move(10, 50)
self.textbox2 = QLineEdit(self)
self.textbox2.move(100, 50)
self.button = QPushButton("Go", self)
self.button.move(0, 200)
self.button.clicked.connect(self.runcode)
def runcode(self):
schedular_type = self.textbox2.text()
if(schedular_type == "FCFS"):
self.close()
fcfs = Window2(self)
# Do not call __init__ explicitly below
# fcfs.__init__()
class Window2(QMainWindow):
def __init__(self,parent=None):
super().__init__(parent)
# always try to set a parent
self.setWindowTitle("My Program")
self.setWindowIcon(QtGui.QIcon("download.jpg"))
self.setGeometry(50, 50, 500, 300)
self.home()
self.show()
def home(self):
self.label1 = QLabel(self)
self.label1.setText("No of Processes")
self.label1.move(10, 0) # col ,row
self.textbox = QLineEdit(self)
self.textbox.move(100, 0)
self.label3 = QLabel(self)
self.label3.setText("Processess Names")
self.label3.move(10, 100)
self.label4 = QLabel(self)
self.label4.setText("Burst Time")
self.label4.move(120, 100)
self.label5 = QLabel(self)
self.label5.setText("Arrival Time")
self.label5.move(200, 100)
self.names = QLineEdit(self)
self.names.move(10, 150)
# self.names.resize(100,160)
self.burst = QLineEdit(self)
self.burst.move(120, 150)
# self.burst.resize(100,160)
self.arrival = QLineEdit(self)
self.arrival.move(250, 150)
# self.arrival.resize(100,160)
# self.textEdit=QTextEdit(self)
# self.textEdit.move(20,250)
self.button = QPushButton("Go", self)
self.button.move(0, 200)
self.button.clicked.connect(self.fcfs)
def fcfs(self):
no_of_process = self.textbox.text()
process_names = self.names.text()
burst_time = self.burst.text()
arrival_time = self.arrival.text()
names_list = process_names.split()
burst_list = burst_time.split()
arrival_list = arrival_time.split()
# integer conversion
burst_list = [int(i) for i in burst_list]
arrival_list = [int(i) for i in arrival_list]
no_of_process = int(no_of_process)
ls = LinkedList()
i = 0
while i < no_of_process:
ls.append(names_list[i], burst_list[i], arrival_list[i])
i = i + 1
time = arrival_list[0]
j = 0
start = []
end = []
name = []
while j < (ls.size()):
while(time < arrival_list[j]):
time = time + 1
start.append(time)
time = time + burst_list[j]
end.append(time)
name.append(names_list[j])
j = j + 1
waiting_time = 0
k = 0
average_waiting = 0
while k < (ls.size()):
waiting_time = waiting_time + \
end[k] - arrival_list[k] - burst_list[k]
average_waiting = waiting_time / ls.size()
k = k + 1
x = name
begin = np.array(start)
end = np.array(end)
plt.barh(range(len(begin)), end - begin, left=begin)
plt.yticks(range(len(begin)), x)
#plt.text(0.6,1.5,('average waiting is ',average_waiting))
plt.annotate(("average waiting is", average_waiting), xy=(0.5, 1.49))
plt.show()
def main():
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
the crucial part is:
def __init__(self,parent=None):
super().__init__(parent)
raiseTemmie.py
import random
import sys
import time
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QWidget, QLabel, QApplication
class Example(QWidget):
size=100
imgNum=0
imgQuantity=2
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setStyleSheet("background-color:transparent;")
self.setGeometry(100, 100, 100, 100)
self.setWindowFlags(Qt.SplashScreen | Qt.WindowStaysOnTopHint)
self.label=QLabel(self)
self.pixmaps=[QPixmap('left.png'),QPixmap('right.png'),QPixmap('stand.png')]
for x in range(len(self.pixmaps)):
self.pixmaps[x]=self.pixmaps[x].scaled(self.size,self.size,Qt.KeepAspectRatio)
self.label.setPixmap(self.pixmaps[2])
self.resize(self.pixmaps[2].width(),self.pixmaps[2].height())
self.show()
def moving(self):
distance=random.randint(10,40)
direct =[random.randint(-1,2),random.randint(-1,2)]
for x in range(0,distance):
self.changeFoot()
self.move(self.x()+5*direct[0],self.y()+5*direct[1])
time.sleep(0.05)
# self.changeTimer.stop()
def changeFoot(self):
if self.imgNum<self.imgQuantity-1:
self.imgNum+=1
else :
self.imgNum=0
self.label.setPixmap(self.pixmaps[self.imgNum])
def mousePressEvent(self, QMouseEvent):
self.label.setPixmap(self.pixmaps[2])
self.changeTimer.stop()
changeTimer=QTimer()
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
sys.exit()
if QKeyEvent.key() == Qt.Key_G:
self.moving()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
I thought this code would switch the pixmap and move it simultaneously but it do not work well.
The timer start after moving finished. What is the problem?
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
sys.exit()
if QKeyEvent.key() == Qt.Key_G:
self.moving()
if I press 'G', it starts the changeTimer and calls moving()
A possible solution is to use qApp.processEvents() to update the data, this is recommended when tasks require little time as is your case, since the maximum time of the loop is 0.05sec*40=2sec.
def moving(self):
distance=random.randint(10,40)
direct =[random.randint(-1,2),random.randint(-1,2)]
for x in range(0,distance):
self.changeFoot()
self.move(self.x()+5*direct[0],self.y()+5*direct[1])
time.sleep(0.05)
qApp.processEvents()
The solution may fail if time grows, another solution is to implement the timer and separate the tasks correctly as I show below:
class Example(QWidget):
size=100
imgNum=0
imgQuantity=2
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setStyleSheet("background-color:transparent;")
self.setGeometry(100, 100, 100, 100)
self.setWindowFlags(Qt.SplashScreen | Qt.WindowStaysOnTopHint)
self.label=QLabel(self)
self.pixmaps=[QPixmap('left.png'),QPixmap('right.png'),QPixmap('stand.png')]
for x in range(len(self.pixmaps)):
self.pixmaps[x]=self.pixmaps[x].scaled(self.size,self.size,Qt.KeepAspectRatio)
self.label.setPixmap(self.pixmaps[2])
self.resize(self.pixmaps[2].width(),self.pixmaps[2].height())
self.changeTimer=QTimer(self)
self.changeTimer.timeout.connect(self.onTimeout)
self.show()
def moving(self):
self.distance= random.randint(10,40)
self.direct = QPoint(random.randint(-1,2), random.randint(-1,2))
self.changeTimer.start(50)
def onTimeout(self):
if self.distance == 0:
self.changeTimer.stop()
else:
self.changeFoot()
self.move(self.pos() + self.direct)
self.distance -= 1
def changeFoot(self):
if self.imgNum<self.imgQuantity-1:
self.imgNum+=1
else :
self.imgNum=0
self.label.setPixmap(self.pixmaps[self.imgNum])
def mousePressEvent(self, QMouseEvent):
self.label.setPixmap(self.pixmaps[2])
self.changeTimer.stop()
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
self.close()
if QKeyEvent.key() == Qt.Key_G:
self.moving()
Note: It is advisable to use self.close() instead of sys.exit() since the first gives time for the program to free resources and close properly.