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()
Related
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?
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()
I'm trying to make a custom widget getting more and more descriptive with each sub class. For instance below, I'm adding to a list of boxes as I make them. I want do_stuff() to only run once but I want it to run on initialization. My problem is that I need all of the __setupUi() functions to run before I finish it. Any help is appreciated.
from PyQt5 import QtCore, QtWidgets
class layer4:
def __init__(self):
self.boxes = []
self.__setupUi()
# I want to run this once AFTER all of the children (regardless of how deep it goes) have run
self.do_stuff()
def __setupUi(self):
self.qsb = QtWidgets.QSpinBox()
self.boxes.append(self.qsb)
def do_stuff(self):
print(self.boxes)
class layer3(layer4):
def __init__(self):
super().__init__()
self.__setupUi()
def __setupUi(self):
self.qsb2 = QtWidgets.QSpinBox()
self.boxes.append(self.qsb2)
class layer2(layer3):
def __init__(self):
super().__init__()
self.__setupUi()
def __setupUi(self):
self.datetimebox = QtWidgets.QDateTimeEdit()
self.boxes.append(self.datetimebox)
class layer1(layer2):
def __init__(self):
super().__init__()
self.__setupUi()
def __setupUi(self):
self.other = QtWidgets.QDateTimeEdit()
self.boxes.append(self.other)
So if I run
t = layer2()
It should return 2 QSpinboxes and 1 datetimeedit
The issue that I'm facing is when I want to split the functionality of the menubar into multiple files (classes), each of them specific for handling options (File/Help/Edit and so on).
In the Main UI class I have:
class MyFrame(QMainWindow):
def __init__(self):
super().__init__()
self.menu_bar = self.menuBar()
# Create menu
self.add_menu()
def add_menu(self):
help_menu = MenuHelp(self)
def getMenuBar(self):
return self.menu_bar
In the MenuHelp (class):
class MenuHelp(QMenu):
def __init__(self, parrent_widget):
super(MenuHelp, self).__init__()
self.menu_variable = parrent_widget.getMenuBar().addMenu('Help')
about_action = self.menu_variable.addAction('About')
about_action.setStatusTip('About')
about_action.triggered.connect(self.handle_trigger)
def handle_trigger(self):
print('Im here')
The menubar is correctly shown, but handle_trigger method is never called, any ideas on what am I doing wrong?
You must pass a parent to your QMenu. You must change:
class MenuHelp(QMenu):
def __init__(self, parrent_widget):
super(MenuHelp, self).__init__()
to:
class MenuHelp(QMenu):
def __init__(self, parrent_widget):
super(MenuHelp, self).__init__(parrent_widget)
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)