Python - reference class each other (information flow) - python

I would like to know, what is the concept of information flow in GUI based apps, or any other app with same problem. When you have two seperate classes and their objects, how is the messeging process done between them. For example you have a GUI and AppLogic.
Scenario 1: Button is pressed -> GUI is processing event -> calls AppLogic method image_clicked()
Scenario 2: HTTPServer gets a message -> AppLogic receives image -> AppLogic calls GUI method render_image()
The problem is that you cannot reference classes each other because the first class does not know the second one (here AppLogic does not know GUI class):
class AppLogic():
gui : GUI
def image_clicked(self):
pass #not important
class GUI():
app_logic : AppLogic
def render_image(self):
pass #not important
I know this is more like go to school and study problem, but I would like to know how these problems are sovled, or some common practices. At least link with some detailed information. I am not able to name the problem right to find the answer.
Edit:
I can use this code without explicit type declaration and it works. But when I want to call functions of gui in AppLogic class definition, intellisense does not hint anything, because it does not know the type of attribute gui. And I don't think that it is good practice to use code like that.
class AppLogic():
def __init__(self) -> None:
self.gui = None
def image_clicked(self):
pass #not important
class GUI():
def __init__(self) -> None:
self.app_logic = None
def render_image(self):
pass #not important
app = AppLogic()
gui = GUI()
app.gui = gui
gui.app_logic = app

You need to initialize your variables.
gui = Gui()
then you can call the methods
For example:
class AppLogic:
gui: Gui
def image_clicked(self):
gui = Gui()
gui.render_image()
class Gui:
logic: AppLogic
def render_image(self) :
pass
Or you can initialize your variable directly
gui: Gui = Gui()
I hope this answers your question

from LogicClass import Logic
class Gui:
logic: AppLogic = AppLogic()
def render_image(self) :
pass
and:
from GuiClass import Gui
class AppLogic:
gui: Gui
def image_clicked(self):
gui = Gui()
gui.render_image()

from Gui import Gui
class Logic:
def __init__(self):
self.gui = Gui()
if __name__ == "__main__":
Logic()
and
class Gui:
def __init__(self):
print("GUI")

Related

Tkinter callbacks to main module

I've built a simple single-script GUI application in Tkinter which works as intended. Since the code for the Tk controls takes up half of the 600 lines script I'm trying to separate the GUI from the main application by putting it in a separate module.
In main.py file I have:
from mytkfile import MainWindow
def main():
"""Main program entry point."""
decks = initialize()
app = App(decks)
app.view.root.mainloop()
if __name__ == '__main__':
main()
In mytkfile.py there is just a huge MainWindow class, which is currently is handling the callbacks to the different interface elements:
import tkinter as tk
from tkinter import ttk
class MainWindow:
def __init__(self, deck):
self.root = tk.Tk()
# ...
The App class in main.py, simplified, looks like this:
class App:
def __init__(self, decks):
self.decks = decks
# ...
self.view = MainWindow(self.deck)
self.updateview()
def updateview(self):
# call several self.view.update_methods
The main window is drawn and basically works as it used to, because MainWindow handles a lot of the data logic itself. However, I would like to change that and have App handle everything and just ask MainWindow to update itself. I am trying to achieve this without App passing itself to MainWindow when instantiating it, because as I understand it a View should not know about its Controller, and should not be calling its own methods to handle data logic.
Problem is, how can I handle e.g. button callbacks in Tk when MainWindow doesn't know anything about App?
After reading various articles on the observer design pattern in Python I've concluded that it's indeed impossible to at least slightly couple the Tkinter view MainWindow class to the model/controller App class in my code. It had been solved by the App class passing itself to the MainWindow, with Tk buttons notifying clicks to callback functions inside App. Works like a charm.

Combining functional and OOP in tkinter

I made an app using tkinter which basically take user input and upon clicking the appropriate button, it will run a function to process them and return an output.
The program works as intended, however I am confused on how appropriate it is to combine functional and OOP style of program building.
My app consists of two python file. The first file, lets call it GUI.py, looks like this:
from secondfile import *
class Window:
def __init__(self, master):
self.master = master
.................
def proceed():
self.newWindow = tk.Toplevel(self.master)
self.app = Window2(self.newWindow, self)
class Window2:
........
thisThread = threading.Thread(
target =simple_func_but_expensive,
args = (....))
def run_the_prog2():
thr.start()
def main():
root = tk.Tk()
root.geometry(....)
root.title(.....)
app = Window(root)
root.mainloop()
if __name__ == '__main__':
main()
The second file (secondfile.py) consists of:
def supporting_function_1(....):
.........
def supporting_function_2(....):
.........
def supporting_function_...(....):
.........
def supporting_function_20(....):
.........
def simple_func_but_expensive(.......):
.........
I was thinking of restructuring the second file with class, but each function in secondfile.py don't really share instance variable that much. Also, the program is able to run another instance of simple_func_but_expensive() because of root.mainloop() so I don't think OOP is necessary in the function file.
However, it seems that most programs use OOP and I see it as more computer science appropriate. Any opinion on this?

How to properly design a multi-window application in Qt (PyQt)?

The most obvious approach for me is to declare each window (window, dialog or widget) in the constructor and call the show() method when needed. Something like this:
class MultiWindowApp():
def __init__(self):
self.window_1 = self.init_window_1()
self.window_2 = self.init_window_2()
def init_window_1(self):
gui = uic.loadUi(...)
# other settings
return gui
def init_window_2(self):
gui = uic.loadUi(...)
# other settings
return gui
def show_window_1(self):
self.window_1.show()
def show_window_2(self):
self.window_2.show()
Nevertheless, it does not seem to be memory efficient, because I store the windows in the memory all the time, even when I am not showing it.
Alternative solution that comes to my mind is to create a separate class for each window (or other widget) and have one placeholder for all in the main class. Assign an instance of the respective class and delete on closing the window. A minimal example below:
class Window_1(QWidget):
def __init__(self):
QWidget.__init__(self)
uic.loadUi(...)
# other settings
self.show()
class Window_2(QWidget):
def __init__(self):
QWidget.__init__(self)
uic.loadUi(...)
# other settings
self.show()
class MultiWindowApp():
def __init__(self):
self.widget_placeholder = None
def show_window_1(self):
self.widget_placeholder = Window_1()
def show_window_2(self):
self.widget_placeholder = Window_1()
This would be a bit slower, but I would avoid keeping in memory unnecessary stuff. Nevertheless, I still have a feeling that there is a better way. What is the proper way of designing such an application?
I didn't run the examples above, so there can be some errors, but I think that the concepts behind them are clear.

Programmatically press the `X` button on the toobar?

I know I can intercept pressing the X button with protocol("WM_DELETE_WINDOW", do_something) however I am having a hard time figuring out how to activate this button or at least the protocol that triggers when this button is pressed.
Here is the situation. I have 2 classes. My main Tk class and my Menu class. When I am setting up the command to close the program with an exit button from the menu I want this button to do exactly the same thing as the X button on the Tk class.
Now I know I could simply call the controller that was passed to the menu class and then call the method I built to handle the close event however I am trying to build this menu class in such a way that I do not need to do this from the menu class. This will allow me to use the menu class on any app I build with little to no editing.
I have not been able to find a post or some documentation that tells me how I can programmatically activate the "WM_DELETE_WINDOW" protocol.
Here is an image if it is unclear what I want. Simply I want the exit button to do exactly what the X button does.
Main class:
import tkinter as tk
import PIP_MENU
class PIP(tk.Tk):
def __init__(self):
super().__init__()
PIP_MENU.start(self)
self.protocol("WM_DELETE_WINDOW", self.handle_close)
def handle_close(self):
print("Closing")
self.quit()
if __name__ == '__main__':
PIP().mainloop()
Menu class on separate .py file:
import tkinter as tk
class Menu(tk.Menu):
def __init__(self, controller):
super().__init__()
self.controller = controller
controller.config(menu=self)
file_menu = tk.Menu(self, tearoff=0)
self.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Exit", command=self.handle_exit)
def handle_exit(self):
# What can I do here that will be handled by
# protocol "WM_DELETE_WINDOW" of the main class?
# All I can find is destroy() and quit()
# But both of these do not get handled by "WM_DELETE_WINDOW".
def start(controller):
Menu(controller)
I have not been able to find a post or some documentation that tells me how I can programmatically active the "WM_DELETE_WINDOW" protocol.
You can't. By definition, the WM_DELETE_WINDOW protocol comes from the window manager.
Catching the protocol handler is designed to give you an opportunity to override its behavior. It is not designed to be a way to trigger some code no matter how the application is destroyed. If you want to run some code when the window is destroyed, whether that is by the user clicking the control on the window frame or through some other way, the correct way to do that is to bind to the <Destroy> event on the root window.
You have to be careful, in that any binding on the root window will be triggered for every widget. Therefore, your binding should only run when event.widget is the same as the root window.
The following example illustrates the technique. There is a method handle_close which is called whenever the window is destroyed. Whether you close the window by clicking on the control on the window frame, or whether you click on the "Close me!" button, the code still runs.
import tkinter as tk
class Example(tk.Tk):
def __init__(self):
super().__init__()
self.bind("<Destroy>", self.handle_close)
button = tk.Button(self, text="Close me!", command=self.destroy)
button.pack()
def handle_close(self, event):
if event.widget == self:
print("Closing")
self.quit()
example = Example()
example.mainloop()
I don't believe there's a method that invokes a specific protocol, since protocol seems to be a specific event watch. Here's a snippet from the module's class Tk:
class Tk(Misc, Wm):
"""Toplevel widget of Tk which represents mostly the main window
of an application. It has an associated Tcl interpreter."""
def _loadtk(self):
...
self.protocol("WM_DELETE_WINDOW", self.destroy)
As you can see, by default the module itself sets the protocol to destroy(). The protocol() method only seeks to replace the default function (at the absence of a function, it just removes the function):
def wm_protocol(self, name=None, func=None):
"""Bind function FUNC to command NAME for this widget.
Return the function bound to NAME if None is given. NAME could be
e.g. "WM_SAVE_YOURSELF" or "WM_DELETE_WINDOW"."""
if callable(func):
command = self._register(func)
else:
command = func
return self.tk.call(
'wm', 'protocol', self._w, name, command)
protocol = wm_protocol
but to achieve what you want you should be able to reference back to the same handling method with this:
def handle_exit(self):
self.controller.handle_close()
Of course, this is not as versatile since you must explicitly know the handler in your main window.
Thought I have accepted Bryan's answer I did manage to come to a workaround I think is fine here.
If I pass the method that is being used to deal with window closing to my menu class and then check if something has been passed I can then decide on weather or not to use the exit method I made or self.controller.destroy() with an if statement.
Here is my solution.
Main file:
import tkinter as tk
from tkinter import messagebox
import PIP_MENU
class PIP(tk.Tk):
def __init__(self):
super().__init__()
PIP_MENU.start(self, self.handle_close)
self.protocol("WM_DELETE_WINDOW", self.handle_close)
def handle_close(self):
x = messagebox.askquestion("DERP", "Do you want to close without saving?")
if x == "yes":
self.destroy()
if __name__ == '__main__':
PIP().mainloop()
Menu file:
import tkinter as tk
class Menu(tk.Menu):
def __init__(self, controller, exit_handler=None):
super().__init__()
self.controller = controller
self.exit_handler = exit_handler
controller.config(menu=self)
file_menu = tk.Menu(self, tearoff=0)
self.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Exit", command=self.handle_exit)
def handle_exit(self):
if self.exit_handler != None:
self.exit_handler()
else:
self.controller.quit()
def start(controller, exit_handler=None):
return Menu(controller, exit_handler)

PySide: Emiting signal from QThread in new syntax

I'm trying (and researching) with little success to emit a signal from a working Qthread to the main window. I don't seem to understand how I should go about this in the new syntax.
Here's a simple example.
from PySide.QtCore import *
from PySide.QtGui import *
import sys
import time
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
button = QPushButton("Test me!")
layout = QVBoxLayout()
layout.addWidget(button)
self.setLayout(layout)
#self.button.clicked.connect(self.test) ----> 'Dialog' object has no attribute 'button'
self.connect(button, SIGNAL('clicked()'), self.test)
self.workerThread = WorkerThread()
def test(self):
self.workerThread.start()
QMessageBox.information(self, 'Done!', 'Done.')
class WorkerThread(QThread):
def __init__(self, parent=None):
super(WorkerThread, self).__init__(parent)
def run(self):
time.sleep(5)
print "Thread done!"
app = QApplication(sys.argv)
dialog = Dialog()
dialog.show()
app.exec_()
I understand that if I didn't have another thread I'd create the signal inside the Dialog class and connect it in the __init__ but how can I create a custom signal that can be emitted from WorkerThread and be used test()?
As a side question. You can see it commented out of the code that the new syntax for connecting the signal errors out. Is it something in my configurations?
I'm on OsX El Capitan, Python 2.7
Any help is highly appreciated! Thanks a lot
TL:DR: I'd like to emmit a signal from the WorkerThread after 5 seconds so that the test function displays the QMessageBox only after WorkingThread is done using the new syntax.
Ok, it's been a long day trying to figure this out. My main resource was this: http://www.matteomattei.com/pyside-signals-and-slots-with-qthread-example/
In the new syntax, in order to handle signals from different threads, you have to create a class for your signal like so:
class WorkerThreadSignal(QObject):
workerThreadDone = Signal()
This is how the WorkerThread end up looking like:
class WorkerThread(QThread):
def __init__(self, parent=None):
super(WorkerThread, self).__init__(parent)
self.workerThreadSignal = WorkerThreadSignal()
def run(self):
time.sleep(3)
self.workerThreadSignal.workerThreadDone.emit()
And for the connections on the Dialog class:
self.workerThread = WorkerThread()
self.buttonn.clicked.connect(self.test)
and:
self.workerThreadSignal = WorkerThreadSignal()
self.workerThread.workerThreadSignal.workerThreadDone.connect(self.success)
def success(self):
QMessageBox.warning(self, 'Warning!', 'Thread executed to completion!')
So the success method is called once the signal is emitted.
What took me the longest to figure out was this last line of code. I originally thought I could connect directly to the WorkerThreadSignal class but, at least in this case, it only worked once I backtracked it's location. From the Dialog init to WorkerThread init back to the WorkerThreadSignal. I took this hint from the website mentioned above.
I find strange that I have to create the same local variables on both classes, maybe there's a way to create one global variable I can refer to instead all the current solution but it works for now.
I hope this helps someone also stuck in this process!
PS: The syntax problem for the connection was also solved. So everything is written with the new syntax, which is great.

Categories

Resources