Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44473 > unrolled thread

Unwanted window spawns when using Tkinter with multiprocessing.

Started byalternative00@rocketmail.com
First post2013-04-28 11:33 -0700
Last post2013-04-30 05:31 -0700
Articles 18 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Unwanted window spawns when using Tkinter with multiprocessing. alternative00@rocketmail.com - 2013-04-28 11:33 -0700
    Re: Unwanted window spawns when using Tkinter with multiprocessing. Dave Angel <davea@davea.name> - 2013-04-28 17:18 -0400
    Re: Unwanted window spawns when using Tkinter with multiprocessing. alternative00@rocketmail.com - 2013-04-28 15:23 -0700
      Re: Unwanted window spawns when using Tkinter with multiprocessing. Dave Angel <davea@davea.name> - 2013-04-28 19:10 -0400
    Re: Unwanted window spawns when using Tkinter with multiprocessing. alternative00@rocketmail.com - 2013-04-28 16:40 -0700
      Re: Unwanted window spawns when using Tkinter with multiprocessing. Dave Angel <davea@davea.name> - 2013-04-28 20:24 -0400
      Re: Unwanted window spawns when using Tkinter with multiprocessing. Chris Angelico <rosuav@gmail.com> - 2013-04-29 13:03 +1000
    Re: Unwanted window spawns when using Tkinter with multiprocessing. alternative00@rocketmail.com - 2013-04-29 08:31 -0700
      Re: Unwanted window spawns when using Tkinter with multiprocessing. MRAB <python@mrabarnett.plus.com> - 2013-04-29 18:03 +0100
    Re: Unwanted window spawns when using Tkinter with multiprocessing. alternative00@rocketmail.com - 2013-04-29 10:32 -0700
      Re: Unwanted window spawns when using Tkinter with multiprocessing. Chris Angelico <rosuav@gmail.com> - 2013-04-30 03:45 +1000
    Re: Unwanted window spawns when using Tkinter with multiprocessing. alternative00@rocketmail.com - 2013-04-29 11:44 -0700
      Re: Unwanted window spawns when using Tkinter with multiprocessing. Chris Angelico <rosuav@gmail.com> - 2013-04-30 08:00 +1000
    Re: Unwanted window spawns when using Tkinter with multiprocessing. alternative00@rocketmail.com - 2013-04-29 18:17 -0700
      Re: Unwanted window spawns when using Tkinter with multiprocessing. Dave Angel <davea@davea.name> - 2013-04-29 22:05 -0400
    Re: Unwanted window spawns when using Tkinter with multiprocessing. alternative00@rocketmail.com - 2013-04-30 05:22 -0700
      Re: Unwanted window spawns when using Tkinter with multiprocessing. Dave Angel <d@davea.name> - 2013-04-30 08:30 -0400
    Re: Unwanted window spawns when using Tkinter with multiprocessing. alternative00@rocketmail.com - 2013-04-30 05:31 -0700

#44473 — Unwanted window spawns when using Tkinter with multiprocessing.

Fromalternative00@rocketmail.com
Date2013-04-28 11:33 -0700
SubjectUnwanted window spawns when using Tkinter with multiprocessing.
Message-ID<7df67006-2176-42cb-a8ce-95a72339e7e2@googlegroups.com>
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 ?

[toc] | [next] | [standalone]


#44477

FromDave Angel <davea@davea.name>
Date2013-04-28 17:18 -0400
Message-ID<mailman.1141.1367183951.3114.python-list@python.org>
In reply to#44473
On 04/28/2013 02:33 PM, alternative00@rocketmail.com wrote:
> 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 ?
>

If you can't post in clear English, then give us some other clues to 
compensate.  Could you post some code, define some function names, and 
use fewer pronouns and indirection?

for example: "it spawns new windows" -- What spawns new windows, exactly?


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#44478

Fromalternative00@rocketmail.com
Date2013-04-28 15:23 -0700
Message-ID<e4c2be7e-2036-4409-a98f-935cf812d755@googlegroups.com>
In reply to#44473
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.

[toc] | [prev] | [next] | [standalone]


#44479

FromDave Angel <davea@davea.name>
Date2013-04-28 19:10 -0400
Message-ID<mailman.1142.1367190638.3114.python-list@python.org>
In reply to#44478
On 04/28/2013 06:23 PM, alternative00@rocketmail.com 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

[toc] | [prev] | [next] | [standalone]


#44480

Fromalternative00@rocketmail.com
Date2013-04-28 16:40 -0700
Message-ID<979db8bf-4d5c-4c7f-8a05-cfade946026f@googlegroups.com>
In reply to#44473
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.

[toc] | [prev] | [next] | [standalone]


#44481

FromDave Angel <davea@davea.name>
Date2013-04-28 20:24 -0400
Message-ID<mailman.1143.1367195083.3114.python-list@python.org>
In reply to#44480
On 04/28/2013 07:40 PM, alternative00@rocketmail.com 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

[toc] | [prev] | [next] | [standalone]


#44485

FromChris Angelico <rosuav@gmail.com>
Date2013-04-29 13:03 +1000
Message-ID<mailman.1144.1367204642.3114.python-list@python.org>
In reply to#44480
On Mon, Apr 29, 2013 at 9:40 AM,  <alternative00@rocketmail.com> 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, at least not on purpose. I'm running on Windows 7  64 bits.

Using multiprocessing on Windows has some requirements:

http://docs.python.org/2/library/multiprocessing.html#windows

If you take care of those restrictions, you should be able to do this.
As Dave pointed out, one of the requirements is for your module to be
importable.

ChrisA

[toc] | [prev] | [next] | [standalone]


#44506

Fromalternative00@rocketmail.com
Date2013-04-29 08:31 -0700
Message-ID<c3e24fc5-e593-4e69-bb57-5bd78dd3c6f2@googlegroups.com>
In reply to#44473
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.

[toc] | [prev] | [next] | [standalone]


#44508

FromMRAB <python@mrabarnett.plus.com>
Date2013-04-29 18:03 +0100
Message-ID<mailman.1156.1367254991.3114.python-list@python.org>
In reply to#44506
On 29/04/2013 16:31, alternative00@rocketmail.com 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()

[toc] | [prev] | [next] | [standalone]


#44510

Fromalternative00@rocketmail.com
Date2013-04-29 10:32 -0700
Message-ID<d2dc1e2c-61b3-4209-93b8-47ef84c8c79d@googlegroups.com>
In reply to#44473
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 ? 

[toc] | [prev] | [next] | [standalone]


#44511

FromChris Angelico <rosuav@gmail.com>
Date2013-04-30 03:45 +1000
Message-ID<mailman.1157.1367257510.3114.python-list@python.org>
In reply to#44510
On Tue, Apr 30, 2013 at 3:32 AM,  <alternative00@rocketmail.com> 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

[toc] | [prev] | [next] | [standalone]


#44516

Fromalternative00@rocketmail.com
Date2013-04-29 11:44 -0700
Message-ID<9733b6b7-671d-45e1-87da-1fdbdd0ed7d0@googlegroups.com>
In reply to#44473
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.

[toc] | [prev] | [next] | [standalone]


#44524

FromChris Angelico <rosuav@gmail.com>
Date2013-04-30 08:00 +1000
Message-ID<mailman.1166.1367272826.3114.python-list@python.org>
In reply to#44516
On Tue, Apr 30, 2013 at 4:44 AM,  <alternative00@rocketmail.com> wrote:
> 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.

I don't know what you mean by your "clause", and I think we have a
language barrier here. (Though your English is *way* better than my
French.) But for a simple rule of thumb, the only things you should
have flush left are "def" and "class" statements, and the one "if"
that checks __name__. Everything else should be indented.

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#44530

Fromalternative00@rocketmail.com
Date2013-04-29 18:17 -0700
Message-ID<9d19b7ba-29c2-40f8-a7f5-95e56d75e6a9@googlegroups.com>
In reply to#44473
I thought 'clause' was reffering to the 'if __name__ == "__main__":' thing in English, but apparently not. 
Well except the import and the 'globalization' of my variables, every thing is idented.

[toc] | [prev] | [next] | [standalone]


#44532

FromDave Angel <davea@davea.name>
Date2013-04-29 22:05 -0400
Message-ID<mailman.1171.1367287546.3114.python-list@python.org>
In reply to#44530
On 04/29/2013 09:17 PM, alternative00@rocketmail.com wrote:
> I thought 'clause' was reffering to the 'if __name__ == "__main__":' thing in English, but apparently not.
> Well except the import and the 'globalization' of my variables, every thing is idented.
>

No clue whom you think you're replying to, but all your replies so far 
are appearing in the thread as replies to your OP.  Please use a little 
context from the message you think you're responding to, so we have an 
idea what you're talking about.

Did you ever see MRAB's message, where he told you exactly what the 
change requested was?


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()

All of those lines were at the left margin in your earlier posted code, 
and you give us no reason to expect you've made them conditional.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#44537

Fromalternative00@rocketmail.com
Date2013-04-30 05:22 -0700
Message-ID<814cb4a9-6ae5-42df-a697-b4ce8e2e4c56@googlegroups.com>
In reply to#44473
 > Dave A.

Yeah I'm using MRAB's code, my current code is : 


#Initalisation
global event
global hitkey


#Functions
def key(event): 
     
     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__":
    
    
     fenetre = Tk() 
     frame = Frame(fenetre, width=200, height=100)
     

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

[toc] | [prev] | [next] | [standalone]


#44538

FromDave Angel <d@davea.name>
Date2013-04-30 08:30 -0400
Message-ID<mailman.1177.1367325048.3114.python-list@python.org>
In reply to#44537
On 04/30/2013 08:22 AM, alternative00@rocketmail.com wrote:
>   > Dave A.
>
> Yeah I'm using MRAB's code, my current code is :
>
>
> #Initalisation
> global event
> global hitkey
>
>
> #Functions
> def key(event):
>
>       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__":
>
>
>       fenetre = Tk()
>       frame = Frame(fenetre, width=200, height=100)
>
>
>       frame.focus_set()
>       frame.bind("<Key>", key)
>       frame.pack()
>       fenetre.mainloop()
>

And you're still getting a new window ?  Wow!


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#44539

Fromalternative00@rocketmail.com
Date2013-04-30 05:31 -0700
Message-ID<78554f08-6107-4076-a534-317ff77a6dbc@googlegroups.com>
In reply to#44473
 > Dave A.

No, not a new window, but my player function doesn't play any sound anymore.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web