Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unwanted window spawns when using Tkinter with multiprocessing.

Reply
Thread Tools

Unwanted window spawns when using Tkinter with multiprocessing.

 
 
alternative00@rocketmail.com
Guest
Posts: n/a
 
      04-28-2013
Hi everyone,

I'm trying to use multiprocessing to avoid Python's GIL but with Tkinter, instead of running my main function, it spawns new windows. In fact, my fuction is used everytime I press a specified key, but with multiprocessing I only get a new window when I hit a key. Does anyone have a solution ?
 
Reply With Quote
 
 
 
 
alternative00@rocketmail.com
Guest
Posts: n/a
 
      04-28-2013
Sorry for my bad english.

Here's my code :

def key(event):

instance = 'Instance'
touche = event.char
instance = multiprocessing.Process(target=player, args=(hitkey,))
instance.start()



def player(hitkey):


winsound.PlaySound(hitkey + '.wav', winsound.SND_FILENAME|winsound.SND_NOWAIT|winsound .SND_ASYNC)

'key' is the tkinter function wich gets the pressed key.
'player' is the function playing a specific wav file depending on wich key is pressed, that's why its argument is 'hitkey'. It uses the winsound module.

What spawns new windows is theorically the multiprocessing line of code, even if it's inside the 'key' function.
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      04-28-2013
On 04/28/2013 06:23 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Sorry for my bad english.
>
> Here's my code :
>
> def key(event):
>
> instance = 'Instance'
> touche = event.char
> instance = multiprocessing.Process(target=player, args=(hitkey,))
> instance.start()
>
>
>
> def player(hitkey):
>
>
> winsound.PlaySound(hitkey + '.wav', winsound.SND_FILENAME|winsound.SND_NOWAIT|winsound .SND_ASYNC)
>
> 'key' is the tkinter function wich gets the pressed key.
> 'player' is the function playing a specific wav file depending on wich key is pressed, that's why its argument is 'hitkey'. It uses the winsound module.
>
> What spawns new windows is theorically the multiprocessing line of code, even if it's inside the 'key' function.
>


Do you have an

if __name__ == "__main__":

clause in your main script? Are you bypassing the gui event loop on the
secondary process? Otherwise, it's your code that's launching the extra
window.

And what OS are you running on?


--
DaveA
 
Reply With Quote
 
alternative00@rocketmail.com
Guest
Posts: n/a
 
      04-28-2013
Well I saw this clause on most of the multiprocessing examples I saw but the reason it was here wasn't explained so I just ignored it (yeah stupid I know). I don't think I bypassed anything, at least not on purpose. I'm running on Windows 7 64 bits.
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      04-29-2013
On 04/28/2013 07:40 PM, (E-Mail Removed) wrote:
> Well I saw this clause on most of the multiprocessing examples I saw but the reason it was here wasn't explained so I just ignored it (yeah stupid I know). I don't think I bypassed anything,


Yes, you skipped the essential if clause.

The child process is started with a different __name__. So if the
__name__ is not "__main__", then you should NOT call any of the GUI
startup code.

Probably you should do little or nothing in the top-level code of the
child process. But we can't give specific advice without seeing what
that code now looks like. What code do you have at top level, and if it
calls functions, what do they look like?

The way you get that code to be different in the child is with that if
statement that you omitted.

> at least not on purpose. I'm running on Windows 7 64 bits.
>



--
DaveA
 
Reply With Quote
 
alternative00@rocketmail.com
Guest
Posts: n/a
 
      04-29-2013
My full code is :


#Import
from tkinter import *
import wave
import winsound
import multiprocessing

#Initialisation
fenetre=Tk()
frame = Frame(fenetre, width=200, height=100)
instance = 'Instance'


#Fonctions

def key(event):

instance = 'Instance'
hitkey = event.char
instance = multiprocessing.Process(target=player, args=(hitkey,))
instance.start()



def player(hitkey):


winsound.PlaySound(hitkey + '.wav', winsound.SND_FILENAME|winsound.SND_NOWAIT|winsound .SND_ASYNC)



#TK
frame.focus_set()
frame.bind("<Key>", key)
frame.pack()
fenetre.mainloop()

The problem is that I don't know where to put that clause.
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      04-29-2013
On 29/04/2013 16:31, (E-Mail Removed) wrote:
> My full code is :
>
>
> #Import
> from tkinter import *
> import wave
> import winsound
> import multiprocessing
>
> #Initialisation
> fenetre=Tk()
> frame = Frame(fenetre, width=200, height=100)
> instance = 'Instance'
>
>
> #Fonctions
>
> def key(event):
>
> instance = 'Instance'
> hitkey = event.char
> instance = multiprocessing.Process(target=player, args=(hitkey,))
> instance.start()
>
>
>
> def player(hitkey):
>
>
> winsound.PlaySound(hitkey + '.wav', winsound.SND_FILENAME|winsound.SND_NOWAIT|winsound .SND_ASYNC)
>
>
>
> #TK
> frame.focus_set()
> frame.bind("<Key>", key)
> frame.pack()
> fenetre.mainloop()
>
> The problem is that I don't know where to put that clause.
>

I hope this helps:


#Import
from tkinter import *
import wave
import winsound
import multiprocessing


#Fonctions

def key(event):
instance = 'Instance'
hitkey = event.char
instance = multiprocessing.Process(target=player, args=(hitkey,))
instance.start()


def player(hitkey):
winsound.PlaySound(hitkey + '.wav',
winsound.SND_FILENAME|winsound.SND_NOWAIT|winsound .SND_ASYNC)


if __name__ == "__main__":
# This part will be run if the file is run as the main script.
#
# The multiprocessing module will import this file to run the
# "player" function, but __name__ won't be "__main__" when it does
# so, therefore this bit of code won't be run.

#Initialisation
fenetre = Tk()
frame = Frame(fenetre, width=200, height=100)
instance = 'Instance'

#TK
frame.focus_set()
frame.bind("<Key>", key)
frame.pack()
fenetre.mainloop()

 
Reply With Quote
 
alternative00@rocketmail.com
Guest
Posts: n/a
 
      04-29-2013
It definetly helped, windows don't pop up anymore, but now it doesn't make any sound anymore. Could it be because of a local (non-global) variable ?
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      04-29-2013
On Tue, Apr 30, 2013 at 3:32 AM, <(E-Mail Removed)> wrote:
> It definetly helped, windows don't pop up anymore, but now it doesn't make any sound anymore. Could it be because of a local (non-global) variable ?


Did you read what I linked you to? There are rules to using
multiprocessing; more of them on Windows.

ChrisA
 
Reply With Quote
 
alternative00@rocketmail.com
Guest
Posts: n/a
 
      04-29-2013
Yeah I did, but I globalized my variables, I've got only functions, and not methods, and my clause seems to work so I don't know why it doesn't work.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
when I read gzipped response from web-servers, GzipReader returnssometimes 'invalid compressed data -- crc error' henry.jykim@gmail.com Ruby 0 04-08-2013 03:51 PM
os.system() on Windows in Tkinter app spawns console window Kevin Walzer Python 2 08-14-2011 05:15 PM
WINDOWS flaw spawns dozens of attacks Au79 Computer Support 0 01-07-2006 01:07 AM
Web Reference class spawns temporary assembly when instantiated? Nathan Craddock ASP .Net Web Services 1 10-13-2005 03:10 PM
Tkinter+popen4+windows leads to unwanted window Patrick L. Nolan Python 0 12-18-2003 07:15 AM



Advertisments