class MainPage(QMainWindow):
def __init__(self):
super(MainPage, self).__init__()
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.second_page=SecondPage()
self.ui.btngiris.clicked.connect(self.open2page)
self.second_page.ui1.profiltakibiinstapy.toggled.connect(self.start)
def open2page(self):
def start(self):
self.a=Thread1()
self.a.start()
class SecondPage(QMainWindow):
def __init__(self):
super(SecondPage, self).__init__()
self.ui1=Ui_MainWindow2()
self.ui1.setupUi(self)
def instagramprofiltakip(self):
self.browser = webdriver.Edge()
self.browser.maximize_window()
self.browser.get("https://www.instagram.com/")
time.sleep(5)
print(str(self.ui1.lnlinstagramkullaniciadi.text()))
print(self.ui1.lnlinstagramsifre.text())
class Thread1(QThread):
sonuc=pyqtSignal(object)
def __init__(self):
super().__init__()
def run(self):
basla=SecondPage()
basla.instagramprofiltakip()
print(str(self.ui1.lnlinstagramkullaniciadi.text()))
print(self.ui1.lnlinstagramsifre.text())
It does not give me this data, google opens, but the data in the second application does not appear.
where am i doing wrong?
Related
This is the current code I am using:
class Opening(QDialog):
def __init__(self):
super(Opening, self).__init__()
loadUi("reminder2.ui", self)
self.startbutton.clicked.connect(self.gotomain)
def gotomain(self):
main = MainWindow()
widget.addWidget(main)
widget.setCurrentIndex(widget.currentIndex()+1)
class MainWindow(QDialog):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("reminder.ui",self)
self.typebutton.clicked.connect(self.med)
self.searchbutton.clicked.connect(self.medd)
self.med2.hide()
self.med3.hide()
self.med4.hide()
self.med5.hide()
self.med6.hide()
self.med7.hide()
self.med8.hide()
self.addbutton.clicked.connect(self.clickAdd)
def med(self):
self.stackedWidget.setCurrentWidget(self.typemed)
def medd(self):
self.stackedWidget.setCurrentWidget(self.searchmed)
def clickAdd(self):
self.med2.show()
Attempting to write a class that will show the progress of a threaded process. I need to use this class for all "file load" operations; however I am having trouble making it global.
fileloader.py:
from PyQt5.QtCore import pyqtSlot, pyqtSignal
from PyQt5.QtWidgets import QDialog
from PyQt5.uic import loadUi
class FileLoader(QDialog):
completeSig = pyqtSignal()
def __init__(self, parent=None):
super(FileLoader, self).__init__(parent)
self.filename = ""
self.clientcode = ""
self.thread = ""
loadUi("GlobalUI/fileloader.ui", self)
self.prgLoader.setValue(0)
#pyqtSlot()
def on_btnCancel_clicked(self):
self.close()
def closeEvent(self, e):
self.thread.stop()
def loadData(self):
self.thread.totalSig.connect(self.prgLoader.setMaximum)
self.thread.countSig.connect(self.prgLoader.setValue)
self.thread.finished.connect(self.completed)
self.thread.start()
def completed(self):
self.completeSig.emit()
self.close()
loader.py
from PyQt5.QtCore import pyqtSignal, QThread
from fileloader import FileLoader
class DataLoader(QThread):
totalSig = pyqtSignal(int)
countSig = pyqtSignal(int)
def __init__(self, parent=None):
super(DataLoader, self).__init__(parent)
self.threadactive = True
self.commitsize = 300
self.rowdata = []
def run(self):
print("Code Will Go Here For Loading the File")
def stop(self):
self.threadactive = False
self.wait()
class PatDataLoader():
def load(self, clientcode, filename):
fl = FileLoader()
fl.clientcode = clientcode
fl.filename = filename
fl.thread = DataLoader()
fl.loadData()
I am calling PatDataLoader.load("test","test.txt") from another module. The problem I am running into is the application crashes with QThread: Destroyed while thread is still running as there seems to be a problem with the thread process I am passing to the fileloader. Am I not putting these pieces together properly?
main.py:
from lmdb.patloader import PatDataLoader
class PPSReportsApp(QMainWindow):
def __init__(self, *args):
super(PPSReportsApp, self).__init__(*args)
loadUi("GlobalUI/ppsreportswindow.ui", self)
#self.showMaximized()
#pyqtSlot()
def on_actionTest_triggered(self):
pl = PatDataLoader()
pl.load("TEST","testfile.txt")
In your code pl is a local variable so it will be deleted when it finishes executing on_actionTest_triggered which is an instant possibly generating that problem. On the other hand, no load should be a static method because it does not use the self. self.thread must be None, it is better than ""
How can you prevent pl from being deleted before it is finished processing?
fl is a QDialog so you can use exec_().
fileloader.py
class FileLoader(QDialog):
completeSig = pyqtSignal()
def __init__(self, parent=None):
super(FileLoader, self).__init__(parent)
self.filename = ""
self.clientcode = ""
self.thread = None
loadUi("GlobalUI/fileloader.ui", self)
self.prgLoader.setValue(0)
#pyqtSlot()
def on_btnCancel_clicked(self):
self.close()
def closeEvent(self, e):
if self.thread:
self.thread.stop()
def loadData(self):
if self.thread:
self.thread.totalSig.connect(self.prgLoader.setMaximum)
self.thread.countSig.connect(self.prgLoader.setValue)
self.thread.finished.connect(self.completed)
self.thread.start()
def completed(self):
self.completeSig.emit()
self.close()
loader.py
class DataLoader(QThread):
totalSig = pyqtSignal(int)
countSig = pyqtSignal(int)
def __init__(self, parent=None):
super(DataLoader, self).__init__(parent)
self.threadactive = True
self.commitsize = 300
self.rowdata = []
def run(self):
self.totalSig.emit(1000)
print("Code Will Go Here For Loading the File")
# emulate process
for i in range(1000):
if self.threadactive:
QThread.msleep(10)
self.countSig.emit(i)
def stop(self):
self.threadactive = False
self.quit()
self.wait()
class PatDataLoader():
#staticmethod
def load(clientcode, filename):
fl = FileLoader()
fl.clientcode = clientcode
fl.filename = filename
fl.thread = DataLoader()
fl.loadData()
fl.exec_() # <---
main.py
#pyqtSlot()
def on_actionTest_triggered(self):
PatDataLoader.load("TEST","testfile.txt")
Im using PyQt and I need to access a list item from another class method, how do I do this?
I want to use the self.list_item in the item_click(self, item) method and use it in another class method.
Class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.LoadList()
self.show()
def LoadList(self):
self.ui = Ui_List()
self.ui.setupUi(self)
self.ui.listWidget.itemClicked.connect(self.item_click)
self.ui.pushButton_Edit.clicked.connect(self.edit_project_btn)
def item_click(self, item):
self.list_item = str(item.text())
self.ui.pushButton_Edit.setEnabled(True)
def edit_project_btn(self):
self.dialog = ProjectEdit()
self.dialog.show()
class ProjectEdit(QtWidgets.QDialog):
def __init__(self):
super(ProjectEdit, self).__init__()
self.ui = Ui_Edit()
self.ui.setupUi(self)
self.ui.pushButton_ok.clicked.connect(self.close)
### access self.list_item here ####
First of all, you should declare list_item and dialog in your __init__ method:
class Window(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__()
self.list_item = None
self.dialog = None
self.LoadList()
self.show()
Then you can just pass the parameter to your class constructor
def edit_project_btn(self):
self.dialog = ProjectEdit(self.list_item)
self.dialog.show()
class ProjectEdit(QtWidgets.QDialog):
def __init__(self, list_item):
super(ProjectEdit, self).__init__()
self.ui = Ui_Edit()
self.ui.setupUi(self)
self.ui.pushButton_ok.clicked.connect(self.close)
self.list_item = list_item
I cutted out code for better understanding my issue. The question is if there is some way how to create something like public method and how or whether I'm totally misunderstood OOP concept.
I have a major class PlayerWindow which only plays video. Then there is class ControlsWindow serving only for my developing, testing and maintenance purposes (launched when fullscreen off). Therefore, I want to be it particularly. Can not figure out, how to call method play() from the ControlsWindow class as well like from inside because when I initialize ControlsWindow with instance of PlayerWindow then I get infinite loop.
class ControlsWindow(QtGui.QWidget):
def __init__(self):
super(ControlsWindow, self).__init__()
self.playPauseButton = QtGui.QPushButton('Play', self)
self.show()
class PlayerWindow(QtGui.QWidget):
def __init__(self):
super(PlayerWindow, self).__init__()
# ...
self.mediaPlayer = self.playerInstance.media_player_new()
# ...
self.initUI()
self.play()
def initUI(self):
# ...
self.show()
self.controls_window = ControlsWindow()
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_Return:
self.toggleControlsWindow()
def toggleControlsWindow(self):
if self.isFullScreen():
self.showNormal()
self.controls_window = ControlsWindow()
else:
self.controls_window.close()
self.showFullScreen()
def play(self):
self.mediaPlayer.play()
def main():
app = QtGui.QApplication(sys.argv)
player_window = PlayerWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
You can pass an instance of PlayerWindow to the class constructor of ControlsWindow:
class ControlsWindow(QtGui.QWidget):
def __init__(self, parent): # Notice the new parent argument
super(ControlsWindow, self).__init__()
self.parent = parent
self.playPauseButton = QtGui.QPushButton('Play', self)
self.show()
# Now you can call the parent's (PlayerWindow) play() function:
self.parent.play()
class PlayerWindow(QtGui.QWidget):
def __init__(self):
super(PlayerWindow, self).__init__()
# ...
self.mediaPlayer = self.playerInstance.media_player_new()
# ...
self.initUI()
self.play()
def initUI(self):
# ...
self.show()
self.controls_window = ControlsWindow(self) # Pass a reference of PlayerWindow
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_Return:
self.toggleControlsWindow()
def toggleControlsWindow(self):
if self.isFullScreen():
self.showNormal()
self.controls_window = ControlsWindow(self) # Pass a reference of PlayerWindow
else:
self.controls_window.close()
self.showFullScreen()
def play(self):
self.mediaPlayer.play()
def main():
app = QtGui.QApplication(sys.argv)
player_window = PlayerWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Hope this helps!
Assuming that I understand you correctly and your code can be simplified to something like:
# QtGui.QWidget
class B:
pass
# ControlsWindow
class Test(B):
def __init__(self):
# You want to call Actual.play here?
pass
# PlayerWindow
class Actual(B):
def __init__(self):
# Your self.mediaPlayer = self.playerInstance.media_player_new()
self.some_variable = 42
def play(self):
print(self.some_variable)
If you'd like to call Actual.play method from inside Test class you can either:
make Actual.play static and self.mediaPlayer a class variable
class Test(B):
def __init__(self):
# Here you go!
Actual.play()
class Actual(B):
# mediaPlayer = self.playerInstance.media_player_new()
some_variable = 42
def __init__(self):
pass
#staticmethod
def play():
print(Actual.some_variable)
or pass a reference to PlayerWindow object to your ControlsWindow class instance
class B:
pass
class Test(B):
def __init__(self, actual: Actual):
# Here you go!
actual.play()
class Actual(B):
def __init__(self):
self.some_variable = 42
def play(self):
print(self.some_variable)
This is only a part of my code. When I clicked btn_convert or btn_save function (self.convertThread.start and self.convert_and_save) working. But when I clicked btn_convert_save, working only self.open. The question is, why after click on btn_convert_save starting not all 3 function ?
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__() ...
def home(self):
self.saveThread = SaveThread()
self.convertThread = ConvertThread()
btn_convert.clicked.connect(self.convertThread.start)
btn_save.clicked.connect(self.saveThread.start)
btn_convert_save.clicked.connect(self.convert_and_save) ...
def convert_and_save(self):
self.open()
self.convertThread.start
self.saveThread.start
#self.convert()
#self.save_file()
class SaveThread(QtCore.QThread):
def __init__(self):
super(SaveThread, self).__init__()
def run(self):...
class ConvertThread(QtCore.QThread):
def __init__(self):
super(ConvertThread, self).__init__()
def run(self):...
you forgot the brackets, instead of
self.convertThread.start
write
self.convertThread.start()