PyGTK event definition class - pygtk

In many PyGTk tutorials, event handlers are defined like below.
window.connect("destroy", self.close)
button.connect("clicked", self.print_hello_world)
Is there any class encapsulating "destroy", "clicked" string literals as I want to access them as constants.

In small apps, we may write code like this:
class MyApp():
def __init__(self):
self.win = Gtk.Window()
self.win.set_size_request(400, 300)
self.win.connect('destroy', self.on_app_exit)
btn = Gtk.Button("hello")
btn.connect('clicked', self.on_button_clicked)
def run(self):
self.win.show_all()
Gtk.main()
def on_app_exit(self, window):
// do something.
Gtk.main_quit()
def on_button_clicked(self, btn):
print('hello, world')
def main():
app = MyApp()
app.run()
if __name__ == '__main__':
main()

Related

Python, tkinter, multiprocessing Variable name not defined problem

from multiprocessing import Process
from tkinter import *
def th():
p = Process(target=Button1)
p.start()
class Button1:
def __init__(self):
btn1.config(state="disabled")
btn1.update()
if __name__ == "__main__":
root = Tk()
root.title("test")
root.resizable(False, False)
btn1 = Button(root, text="시작", width=10, height=5, command=th)
btn1.grid(row=0, column=0, sticky=N+E+W+S, padx=5, pady=5)
root.mainloop()
btn1.config(state="disabled")
NameError: name 'btn1' is not defined
I would appreciate it if you could tell me how to solve it.
I want the button to be disabled when pressed using multi processing.

How to count lines in qscintilla?

I'm using QScintilla to make my own notepad for fun in pyqt5 python. I was wandering if there is a way to get the number of lines of a QScintilla() widget?
You have to use the lines() method, you can also use the linesChanged signal.
import sys
from PyQt5 import QtWidgets, Qsci
class Editor(Qsci.QsciScintilla):
def __init__(self, parent=None):
super().__init__(parent)
self.setText("Foo\nBar")
self.print_lines()
self.linesChanged.connect(self.handle_lines_changed)
def handle_lines_changed(self):
self.print_lines()
def print_lines(self):
print("total lines: {}".format(self.lines()))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Editor()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

How to execute code after threads qthreadpool?

I have a GUI that implements 3 buttons
One of the buttons has the "start" function tied, which contains 2 processes, in fact, these are two cycles that should work multithreaded, but I need to add more code to the "start" function that, for example, changes some widgets.
The problem is that if I add some code after self.threadpool.start(thread2) self.threadpool.start(thread3), then it is not executed upon completion of these threads, but before that, or not at all.
I wrote a toy example that fully describes my requirements from the program
import sys
import time
from PySide2.QtCore import QThreadPool, QRunnable, Slot
from PySide2.QtWidgets import QApplication, QWidget, QPushButton
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.threadpool = QThreadPool()
self.btn.clicked.connect(lambda: self.start())
def start(self):
t1 = Worker(th1)
t2 = Worker(th2)
self.btn.setEnabled(False)
self.threadpool.start(t1)
self.threadpool.start(t2)
self.btn.setEnabled(True)
def initUI(self):
self.btn = QPushButton('Start', self)
self.btn.resize(self.btn.sizeHint())
self.btn.move(50, 50)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('HELP')
self.show()
class Worker(QRunnable):
def __init__(self, fn):
super(Worker, self).__init__()
self.fn = fn
#Slot() # QtCore.Slot
def run(self):
self.fn()
def th1():
time.sleep(5)
print("th1")
def th2():
time.sleep(10)
print("th2")
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
I need the button to turn off before the streams start and turn on only after their completion.
The solution should be fair for any code I want to use after streams.
First, using the Slot decorator in the methods of a QRunnable is useless since it is not a QObject.
On the other hand, the idea of implementing a multithreading logic is that the main thread does not block, so you should not expect that after invoking start() it will be executed after it finishes executing the threads.
The solution is to create a QObject that emits a signal before and after the execution of the thread, and with that logic implement the GUI update:
import sys
import time
from PySide2.QtCore import QObject, QThreadPool, QRunnable, Signal, Slot
from PySide2.QtWidgets import QApplication, QWidget, QPushButton
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.active_workers = 0
self.threadpool = QThreadPool()
self.btn.clicked.connect(self.start)
#Slot()
def start(self):
t1 = Worker(th1)
t1.signaller.started.connect(self.on_started)
t1.signaller.finished.connect(self.on_finished)
t2 = Worker(th2)
t2.signaller.started.connect(self.on_started)
t2.signaller.finished.connect(self.on_finished)
self.btn.setEnabled(False)
self.threadpool.start(t1)
self.threadpool.start(t2)
def initUI(self):
self.btn = QPushButton("Start", self)
self.btn.resize(self.btn.sizeHint())
self.btn.move(50, 50)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle("HELP")
self.show()
#Slot()
def on_started(self):
self.active_workers += 1
#Slot()
def on_finished(self):
self.active_workers -= 1
self.btn.setEnabled(self.active_workers == 0)
class Signaller(QObject):
started = Signal()
finished = Signal()
class Worker(QRunnable):
def __init__(self, fn):
super(Worker, self).__init__()
self.signaller = Signaller()
self.fn = fn
def run(self):
self.signaller.started.emit()
self.fn()
self.signaller.finished.emit()
def th1():
time.sleep(5)
print("th1")
def th2():
time.sleep(10)
print("th2")
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

Calling method from other class

I'm very new to PyQt5, and I'm trying to make an interactive gui for plotting of data. However, this problem may well be completely unrelated to PyQt5 and more a problem with my understanding of object oriented programming in general.
I have a MainWindow class, a SupportClass1 and a SupportClass2. When I make an instance of SupportClass1, I want to call the method DoSomething in the MainWindow class by referring to the object window, but I get the error message NameError: name 'window' is not defined.
I have no problems creating a method in the SupportClass2 and calling that from the MainWindow class so I get the impression that I have not instantiated the MainWindow class correctly which I don't understand as I thought I had defined window as an instace of the MainWindow class.
Can anyone help me understand what is wrong in my logic and how to solve this problem?
from PyQt5 import QtWidgets, QtCore
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import sys
import os
from random import randint
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.graphWidget = pg.PlotWidget()
self.x = list(range(100))
self.y = [randint(0,100) for _ in range(100)]
self.graphWidget.setBackground('w')
pen = pg.mkPen(color=(255, 0, 0))
self.data_line = self.graphWidget.plot(self.x, self.y, pen=pen)
self.button = QPushButton('Test')
self.button.clicked.connect(self.InstantiateSupportClasses)
self.gui_box = QVBoxLayout()
self.gui_box.addWidget(self.graphWidget)
self.gui_box.addWidget(self.button)
self.setLayout(self.gui_box)
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Test application')
self.show()
def InstantiateSupportClasses(self):
supp_class2 = SupportClass2()
print(supp_class2.GetVariable())
supp_class1 = SupportClass1()
def DoSomething(self):
print('I did something!')
class SupportClass1():
def __init__(self):
window.DoSomething
class SupportClass2():
def __init__(self):
self.some_variable = 5
def GetVariable(self):
return self.some_variable
def main():
app = QApplication(sys.argv)
app.setStyle('Fusion')
window = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()```
I see that you are using the "window" object from the "SupportClass1" class.
but that class does not recognize this object one solution is to insert that object to the "SupportClass1()"
from PyQt5 import QtWidgets, QtCore
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import sys
import os
from random import randint
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.graphWidget = pg.PlotWidget()
self.x = list(range(100))
self.y = [randint(0,100) for _ in range(100)]
self.graphWidget.setBackground('w')
pen = pg.mkPen(color=(255, 0, 0))
self.data_line = self.graphWidget.plot(self.x, self.y, pen=pen)
self.button = QPushButton('Test')
self.button.clicked.connect(self.InstantiateSupportClasses)
self.gui_box = QVBoxLayout()
self.gui_box.addWidget(self.graphWidget)
self.gui_box.addWidget(self.button)
self.setLayout(self.gui_box)
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Test application')
self.show()
def InstantiateSupportClasses(self):
supp_class2 = SupportClass2()
print(supp_class2.GetVariable())
supp_class1 = SupportClass1(self)
def DoSomething(self):
print('I did something!')
class SupportClass1():
def __init__(self, window):
window.DoSomething()
class SupportClass2():
def __init__(self):
self.some_variable = 5
def GetVariable(self):
return self.some_variable
def main():
app = QApplication(sys.argv)
app.setStyle('Fusion')
window = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

how to transmit true value to formal parameter in decorator function(pyqtSlot())?

first, see code below:
import sys
from PyQt5.QtCore import (Qt, pyqtSignal, pyqtSlot)
from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider,
QVBoxLayout, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def printLabel(self, str):
print(str)
#pyqtSlot(int)
def on_sld_valueChanged(self, value):
self.lcd.display(value)
self.printLabel(value)
def initUI(self):
self.lcd = QLCDNumber(self)
self.sld = QSlider(Qt.Horizontal, self)
vbox = QVBoxLayout()
vbox.addWidget(self.lcd)
vbox.addWidget(self.sld)
self.setLayout(vbox)
self.sld.valueChanged.connect(self.on_sld_valueChanged)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Signal & slot')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
I'm a little puzzled about how the true value in sld is transmitted to the formal parameter 'value' in the slot function : def sld_valChanged(self, value).
Because i can't see something like this: self.sld.valueChanged.connect(partial(self.sld_valChanged, self.sld.value))
Could someone explain that?