Why is Threading using the class as an arg? - python

I am trying to use a thread to run a function with multiple arguments. Whenever I tried to execute the code, it would say I was providing 1 too many arguments for the function. In my last attempt, I used 1 less argument than the function needed, and voila it works by using the class itself as an argument. Here is my code.
import threading
import sys
import tkinter
class Window():
'''Class to hold a tkinter window'''
def __init__(self, root):
'''Makes a button'''
button1 = tkinter.Button(root,
text = ' Print ',
command = self.Printer
)
button1.pack()
def Function(x,y,z):
'''function called by the thread'''
print(x)
print(y)
print(z)
def Printer(self):
'''function called by the button to start the thread'''
print('thread started')
x = threading.Thread(target=self.Function, args=('spam', 'eggs'))
x.daemon = True
x.start()
root = tkinter.Tk()
Screen = Window(root)
root.mainloop()
Here is the resulting output. Normally I would expect some kind of error from this; note that I only specified 2 arguments when the function calls for three!
thread started
<__main__.Window object at 0x000001A488CFF848>
spam
eggs
What is causing this to happen? Using python 3.7.5 in IDLE, if that is making a difference.

Function is a method, so call self.function implicitly provides self as a first argument. If that is not your intended behavior either consider switch to a static method or use a function.

Related

Python multiprocessing asynchronous callback

I'm writing a program with a GUI using TKinter, in which the user can click a button and a new process is started to perform work using multiprocess.Process. This is necessary so the GUI can still be used while the work is being done, which can take several seconds.
The GUI also has a text box where the status of the program is displayed when things happen. This is often straight forward, with each function calling an add_text() function which just prints text in the text box. However, when add_text() is called in the separate process, the text does not end up in the text box.
I've thought about using a Pipe or Queue, but that would require using some sort of loop to check if anything has been returned from the process and that would also cause the main (GUI) process to be unusable. Is there some way to call a function in one process that will do work in another?
Here's an simple example of what I'm trying to do
import time
import multiprocessing as mp
import tkinter as tk
textbox = tk.Text()
def add_text(text):
# Insert text into textbox
textbox.insert(tk.END, text)
def worker():
x = 0
while x < 10:
add_text('Sleeping for {0} seconds'.format(x)
x += 1
time.sleep(1)
proc = mp.Process(target=worker)
# Usually happens on a button click
proc.start()
# GUI should still be usable here
The asyncronous things actually require loop.
You could attach function to the TkInter's loop by using Tk.after() method.
import Tkinter as tk
class App():
def __init__(self):
self.root = tk.Tk()
self.check_processes()
self.root.mainloop()
def check_processes(self):
if process_finished:
do_something()
else:
do_something_else()
self.after(1000, check_processes)
app=App()
I ended up using a multiprocessing.Pipe by using TKinter's after() method to perform the looping. It loops on an interval and checks the pipe to see if there's any messages from the thread, and if so it inserts them into the text box.
import tkinter
import multiprocessing
def do_something(child_conn):
while True:
child_conn.send('Status text\n')
class Window:
def __init__(self):
self.root = tkinter.Tk()
self.textbox = tkinter.Text()
self.parent_conn, child_conn = multiprocessing.Pipe()
self.process = multiprocessing.Process(target=do_something, args=(child_conn,))
def start(self):
self.get_status_updates()
self.process.start()
self.root.mainloop()
def get_status_updates()
status = self.check_pipe()
if status:
self.textbox.add_text(status)
self.root.after(500, self.get_status_updates) # loop every 500ms
def check_pipe():
if self.parent_conn.poll():
status = self.parent_conn.recv()
return status
return None

Tkinter after method executing immediately

The TKinter 'after' method is executing immediately, then pausing for the 3 second time after execution. If I also use the 'after' method in my CheckStatus function, it goes onto a rapid loop and never gets to the mainloop().
What am I doing wrong? the documentation says the function would be called after the pause time, but its actually happening before. I want to call CheckStatus every second for a hardware input on Raspberry Pi, as well as have the normal mainloop responding to user events.
from tkinter import *
def DoClick(entries):
global ButCount
ButCount += 1
print("ButCount", ButCount, "TimeCount", TimeCount)
def newform(root):
L1 = Label(root, text = "test of 'after' method which seems to call before time")
L1.pack()
def CheckStatus():
global TimeCount
TimeCount += 1
print("In CheckStatus. ButCount", ButCount, "TimeCount", TimeCount)
# root.after(3000, DoTime())
root = Tk()
ButCount = 0
TimeCount = 0
if __name__ == '__main__':
FormData = newform(root)
root.bind('<Return>', (lambda event, e=FormData: fetch(e)))
b1 = Button(root, text='Click me', command=(lambda e=FormData: DoClick(e)))
b1.pack()
print("Before root.after(")
root.after(3000, CheckStatus())
print("Done root.after(")
root.mainloop()
You are using after incorrectly. Consider this line of code:
root.after(3000, CheckStatus())
It is exactly the same as this:
result = CheckStatus()
root.after(3000, result)
See the problem? after requires a callable -- a reference to the function.
The solution is to pass a reference to the function:
root.after(3000, CheckStatus)
And even though you didn't ask, for people who might be wondering how to pass arguments: you can include positional arguments as well:
def example(a,b):
...
root.after(3000, example, "this is a", "this is b")
You've got one bug in your code, with:
root.after(3000, CheckStatus())
which needs to be:
root.after(3000, CheckStatus)
# ^^ parens removed.
Passing in CheckStatus() actually calls the func rather than passing in its reference.
It also sounds like you want to call CheckStatus over and over again. You can do that with a recursive call in CheckStatus. You've already got:
# root.after(3000, DoTime())
in your code, in CheckStatus(). Perhaps you would want to change that to:
root.after(3000, CheckStatus)
to get you your async checking.
Also, depending on what you're actually trying to do, you may want that "recursive" call to be conditional.

How to execute a class method from another class with the method passed a parameter in Python

I'm a beginner in learning python..
I'm looking for help in solving an OOP problem
My main program has something simplified like below:
class abc(Frame):
def _init_(self, master)
Frame.__init__(self)
self.B1 = Mybutton(self.master, self.cmd)
def cmd(self):
print("hello world")
In the main program, I import Mybutton class in another file, which is simplified as below:
class Mybutton():
def _init_(self, parent, command):
self.command = command
def A_ramdom_fcn(self):
...
self.command() ------------------>> here I want to execute the command
in class abc, not in class Mybutton.
How to execute a method from another class that is passed as an instance method, you may ask why not just execute it in class abc, but I have event attached to button press, it needs to do a roundabout to achieve this..
First, fix the typos: missing : in abc's init method, and it should be __init__ (with two underscores) for both classes.
It seems like you've gotten yourself turned around. You've set things up correctly using composition: an abc has a Mybutton, and it looks like you correctly pass the function to Mybutton so that it can execute it. In fact, your code will work as written if you do, for example
a = abc(master) # no context for what master is, but I assume you have it
a.B1.A_ramdom_fcn()
With the way you've set things up, you don't want to import and make instances of Mybutton in your main program (what abc would they belong to?). You want to import and make instances of abc. You then access their internal Mybutton like I've shown in the example above. This works because when you pass self.cmd to the Mybutton constructor while inside the abc constructor, it's already a bound method of the abc you're constructing.
As an addendum, it looks like you might be having an XY problem with regards to why you need such a roundabout method. Is there any reason why you can't simply pass abc.cmd to the button press handler?
Theoretically, what you are trying is possible, you can capture the object method into variable and call it later (python 3):
class Window:
def __init__(self):
self.my_button = Mybutton(self.cmd)
def cmd(self):
print("hello world")
class Mybutton:
def __init__(self, command):
self.command = command
def a_ramdom_fcn(self):
self.command.__call__()
win = Window()
win.my_button.a_ramdom_fcn()
I assume you are trying to make the generic Button class which doesn't know what to do when it's clicked and you want to put the actual logic into your Window class.
That makes sense, but it would be even better to extract the logic into the third, Command class. This allows us to limit the Window responsibility and also avoid the trick with method-as-variable (the command we pass to the button object is just another object):
class HelloWorldCommand:
def execute(self):
print("Hello world")
class Window:
def __init__(self):
self.my_button = Mybutton(
HelloWorldCommand()
)
class Mybutton:
def __init__(self, command):
self.command = command
def a_ramdom_fcn(self):
self.command.execute()
win = Window()
win.my_button.a_ramdom_fcn()

Tkinter - StringVar() trace not executing command

I wrote this code to be a version manager, but it doesn't execute the command changeDir(). Why?
https://pastebin.com/VSnhzRzF
You forgot to pass a 'name' argument to changeDir function. And there's no exception because your statement has no effect!
Snippet to represent the problem:
import sys
def exec_smth():
# execution without effect
exec('write_smth')
try:
# execution with exception because of missing argument
exec('write_smth()')
except TypeError as error:
# now we pass an argument
exec('write_smth("I failed because of %s" % error )')
def write_smth(smth):
sys.stdout.write(smth)
exec_smth()
Anyway, outside your __init__ function there're no StringVars at all thanks to garbage collector, so your code would fail anyway!
There're even more problems, because you never bind any of your sv{} to a widget and expect something in return! But ok, let's try to do things with exec:
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entries = []
for _ in range(5):
exec('self.sv{} = tk.StringVar()'.format(_))
exec('self.sv{}.trace("w", self.change_sv)'.format(_))
exec('self.entries.append(tk.Entry(self, text="", textvariable=self.sv{}))'.format(_))
for entry in self.entries:
entry.pack()
def change_sv(*args):
# get id of a variable (you can't rely on that (0-9)!)
idx = args[1][-1:]
# get new value
value = getattr(args[0], 'sv{}'.format(idx)).get()
# result
print('Value changed in self.sv%s to %s!' % (idx, value))
app = App()
app.mainloop()
Output:
As you see - we always need a reference to StringVars and I think that option with a list of them is far better!
Note: If you need to pass something to callback function - use a lambda function! All code tested with Python 3.
Links:
The Variable Classes
Tkinter Callbacks
Behavior of exec function in Python 2 and Python 3

Python TypeError when using Bind for a wx.Button

I have a class called WxFrame which creates a wxPython frame. I added a method called createRunButton which receives self and pydepp, which is the object of a PyDEPP class
import wx
class WxFrame(wx.Frame):
def __init__(self, parent, title):
super(WxFrame, self).__init__(parent, title=title)
self.Maximize()
self.Show()
def createRunButton(self,pydepp):
#pydepp.run()
self.runButton = wx.Button(self, label="Run")
self.Bind(wx.EVT_BUTTON, pydepp.run, self.runButton
This is the PyDEPP class:
class PyDEPP:
def run(self):
print "running"
I instantiate and run it with:
import wx
from gui.gui import WxFrame
from Depp.Depp import PyDEPP
class PyDEPPgui():
"""PyDEPPgui create doc string here ...."""
def __init__(self,pydepp):
self.app = wx.App(False)
##Create a wxframe and show it
self.frame = WxFrame(None, "Cyclic Depp Data Collector - Ver. 0.1")
self.frame.createRunButton(pydepp)
self.frame.SetStatusText('wxPython GUI successfully initialised')
if __name__=='__main__':
#Launch the program by calling the PyDEPPgui __init__ constructor
pydepp = PyDEPP()
pydeppgui = PyDEPPgui(pydepp)
pydeppgui.app.MainLoop()
The error I get when running the above code is:
TypeError: run() takes exactly 1 argument (2 given)
However, if I comment out the bind and uncomment the line pydepp.run(), then it works fine.
The answer is obvious I'm sure, but I have never studied CompSci or OO coding.
The event gets passed as an argument to the callback function. This should work:
class PyDEPP:
def run(self, event):
print "running"
When the event is triggered two arguments are passed to the callback function run(): the object which has triggered the event, and a wxEvent object. Since run only accepts one argument in your code the interpreter is giving that error which tells you that you've provided too many arguments.
Replace
run(self): # Expects one argument, but is passed two. TypeError thrown
with
run(self, event): # Expects two arguments, gets two arguments. All is well
and it should work.
This is one instance where the error tells you a lot about what's wrong with the code. Given that "run() takes exactly 1 argument (2 given)", you immediately know that either you've accidentally passed an extra argument, or run should be expecting another.

Categories

Resources