python tkinter GUI freezes and music player button won't worked for more than once










-1















How can I make the play button work as many times as possible but also being able to use the rest of the GUI and not having it freeze up.



I'd like to be able to use the play button more than once but it keeps saying




TypeError: can't pickle _tkinter.tkapp objects




and when I use threads it will say runTime Error:




Threads can only be started once




from tkinter import *
from tkinter import filedialog
import os
import winsound
import threading
import time
import multiprocessing

audio_list =

class Main(Frame):

def __init__(self, master):
Frame.__init__(self, master, bg="white")
self.audio_dictionary=audio_list
self.gui()
self.refresh()
Thread_1 = multiprocessing.Process(target=self.gui, args=(self,))
Thread_1.start()

def gui(self):
self.Thread_2 = multiprocessing.Process(target=self.play, args=(self,))
self.play_button= Button(text='Play', command=self.play)
self.play_button.grid(column=0, row=1, sticky='W')

stop_button= Button(text="Stop", command=self.stop)
stop_button.grid(row=1, column=0, columnspan=2, sticky='E')

self.display_songs = Listbox(bd=5, relief=GROOVE)
self.display_songs.grid(row=0, column=0, columnspan=2)

import_button = Button(text="Import", command=self.import_files)
import_button.grid(row=1, column=2, sticky='E')

self.status_window = Listbox(bd=5, relief=GROOVE)
self.status_window.grid(row=0, column=2)


def import_files(self):
self.selected_songs=filedialog.askopenfilenames(filetypes = [("wav file", "*")], title='Select wav files')
counter=0
for y in self.selected_songs:
x=os.path.basename(y)
self.audio_dictionary.append((x,y))
print(x)
self.display_songs.insert(counter, x)
counter+=1
for p in self.audio_dictionary:
print(p)

def play(self):
if self.Thread_2.is_alive() is True:
selection = self.display_songs.curselection()

for item in selection:
song=self.display_songs.get(item)

for c in self.audio_dictionary:
s=c[0]
if song==s:
direct=c[1]
else:
pass

print(direct)
winsound.PlaySound(direct, winsound.SND_FILENAME)
else:
pass

def refresh(self):
window.update()
window.after(100, self.refresh)

def stop(self):
print("Stopped Music")
winsound.PlaySound(None, winsound.SND_FILENAME)


window = Tk()
app = Main(window)
window.mainloop()









share|improve this question
























  • Could you post the complete traceback related to the posted code?

    – toti08
    Nov 13 '18 at 8:50











  • However I think you should link your button to the Thread, and not to the function itself. I mean, you want the button to start the Thread...Also I don't see where you start Thread2.

    – toti08
    Nov 13 '18 at 9:28











  • Possible duplicate of Tkinter: How to use threads to preventing main event loop from "freezing"

    – stovfl
    Nov 13 '18 at 9:35















-1















How can I make the play button work as many times as possible but also being able to use the rest of the GUI and not having it freeze up.



I'd like to be able to use the play button more than once but it keeps saying




TypeError: can't pickle _tkinter.tkapp objects




and when I use threads it will say runTime Error:




Threads can only be started once




from tkinter import *
from tkinter import filedialog
import os
import winsound
import threading
import time
import multiprocessing

audio_list =

class Main(Frame):

def __init__(self, master):
Frame.__init__(self, master, bg="white")
self.audio_dictionary=audio_list
self.gui()
self.refresh()
Thread_1 = multiprocessing.Process(target=self.gui, args=(self,))
Thread_1.start()

def gui(self):
self.Thread_2 = multiprocessing.Process(target=self.play, args=(self,))
self.play_button= Button(text='Play', command=self.play)
self.play_button.grid(column=0, row=1, sticky='W')

stop_button= Button(text="Stop", command=self.stop)
stop_button.grid(row=1, column=0, columnspan=2, sticky='E')

self.display_songs = Listbox(bd=5, relief=GROOVE)
self.display_songs.grid(row=0, column=0, columnspan=2)

import_button = Button(text="Import", command=self.import_files)
import_button.grid(row=1, column=2, sticky='E')

self.status_window = Listbox(bd=5, relief=GROOVE)
self.status_window.grid(row=0, column=2)


def import_files(self):
self.selected_songs=filedialog.askopenfilenames(filetypes = [("wav file", "*")], title='Select wav files')
counter=0
for y in self.selected_songs:
x=os.path.basename(y)
self.audio_dictionary.append((x,y))
print(x)
self.display_songs.insert(counter, x)
counter+=1
for p in self.audio_dictionary:
print(p)

def play(self):
if self.Thread_2.is_alive() is True:
selection = self.display_songs.curselection()

for item in selection:
song=self.display_songs.get(item)

for c in self.audio_dictionary:
s=c[0]
if song==s:
direct=c[1]
else:
pass

print(direct)
winsound.PlaySound(direct, winsound.SND_FILENAME)
else:
pass

def refresh(self):
window.update()
window.after(100, self.refresh)

def stop(self):
print("Stopped Music")
winsound.PlaySound(None, winsound.SND_FILENAME)


window = Tk()
app = Main(window)
window.mainloop()









share|improve this question
























  • Could you post the complete traceback related to the posted code?

    – toti08
    Nov 13 '18 at 8:50











  • However I think you should link your button to the Thread, and not to the function itself. I mean, you want the button to start the Thread...Also I don't see where you start Thread2.

    – toti08
    Nov 13 '18 at 9:28











  • Possible duplicate of Tkinter: How to use threads to preventing main event loop from "freezing"

    – stovfl
    Nov 13 '18 at 9:35













-1












-1








-1








How can I make the play button work as many times as possible but also being able to use the rest of the GUI and not having it freeze up.



I'd like to be able to use the play button more than once but it keeps saying




TypeError: can't pickle _tkinter.tkapp objects




and when I use threads it will say runTime Error:




Threads can only be started once




from tkinter import *
from tkinter import filedialog
import os
import winsound
import threading
import time
import multiprocessing

audio_list =

class Main(Frame):

def __init__(self, master):
Frame.__init__(self, master, bg="white")
self.audio_dictionary=audio_list
self.gui()
self.refresh()
Thread_1 = multiprocessing.Process(target=self.gui, args=(self,))
Thread_1.start()

def gui(self):
self.Thread_2 = multiprocessing.Process(target=self.play, args=(self,))
self.play_button= Button(text='Play', command=self.play)
self.play_button.grid(column=0, row=1, sticky='W')

stop_button= Button(text="Stop", command=self.stop)
stop_button.grid(row=1, column=0, columnspan=2, sticky='E')

self.display_songs = Listbox(bd=5, relief=GROOVE)
self.display_songs.grid(row=0, column=0, columnspan=2)

import_button = Button(text="Import", command=self.import_files)
import_button.grid(row=1, column=2, sticky='E')

self.status_window = Listbox(bd=5, relief=GROOVE)
self.status_window.grid(row=0, column=2)


def import_files(self):
self.selected_songs=filedialog.askopenfilenames(filetypes = [("wav file", "*")], title='Select wav files')
counter=0
for y in self.selected_songs:
x=os.path.basename(y)
self.audio_dictionary.append((x,y))
print(x)
self.display_songs.insert(counter, x)
counter+=1
for p in self.audio_dictionary:
print(p)

def play(self):
if self.Thread_2.is_alive() is True:
selection = self.display_songs.curselection()

for item in selection:
song=self.display_songs.get(item)

for c in self.audio_dictionary:
s=c[0]
if song==s:
direct=c[1]
else:
pass

print(direct)
winsound.PlaySound(direct, winsound.SND_FILENAME)
else:
pass

def refresh(self):
window.update()
window.after(100, self.refresh)

def stop(self):
print("Stopped Music")
winsound.PlaySound(None, winsound.SND_FILENAME)


window = Tk()
app = Main(window)
window.mainloop()









share|improve this question
















How can I make the play button work as many times as possible but also being able to use the rest of the GUI and not having it freeze up.



I'd like to be able to use the play button more than once but it keeps saying




TypeError: can't pickle _tkinter.tkapp objects




and when I use threads it will say runTime Error:




Threads can only be started once




from tkinter import *
from tkinter import filedialog
import os
import winsound
import threading
import time
import multiprocessing

audio_list =

class Main(Frame):

def __init__(self, master):
Frame.__init__(self, master, bg="white")
self.audio_dictionary=audio_list
self.gui()
self.refresh()
Thread_1 = multiprocessing.Process(target=self.gui, args=(self,))
Thread_1.start()

def gui(self):
self.Thread_2 = multiprocessing.Process(target=self.play, args=(self,))
self.play_button= Button(text='Play', command=self.play)
self.play_button.grid(column=0, row=1, sticky='W')

stop_button= Button(text="Stop", command=self.stop)
stop_button.grid(row=1, column=0, columnspan=2, sticky='E')

self.display_songs = Listbox(bd=5, relief=GROOVE)
self.display_songs.grid(row=0, column=0, columnspan=2)

import_button = Button(text="Import", command=self.import_files)
import_button.grid(row=1, column=2, sticky='E')

self.status_window = Listbox(bd=5, relief=GROOVE)
self.status_window.grid(row=0, column=2)


def import_files(self):
self.selected_songs=filedialog.askopenfilenames(filetypes = [("wav file", "*")], title='Select wav files')
counter=0
for y in self.selected_songs:
x=os.path.basename(y)
self.audio_dictionary.append((x,y))
print(x)
self.display_songs.insert(counter, x)
counter+=1
for p in self.audio_dictionary:
print(p)

def play(self):
if self.Thread_2.is_alive() is True:
selection = self.display_songs.curselection()

for item in selection:
song=self.display_songs.get(item)

for c in self.audio_dictionary:
s=c[0]
if song==s:
direct=c[1]
else:
pass

print(direct)
winsound.PlaySound(direct, winsound.SND_FILENAME)
else:
pass

def refresh(self):
window.update()
window.after(100, self.refresh)

def stop(self):
print("Stopped Music")
winsound.PlaySound(None, winsound.SND_FILENAME)


window = Tk()
app = Main(window)
window.mainloop()






python tkinter winsound






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 8:59









toti08

1,78941623




1,78941623










asked Nov 13 '18 at 8:07









TazzyBoy_TazzyBoy_

1




1












  • Could you post the complete traceback related to the posted code?

    – toti08
    Nov 13 '18 at 8:50











  • However I think you should link your button to the Thread, and not to the function itself. I mean, you want the button to start the Thread...Also I don't see where you start Thread2.

    – toti08
    Nov 13 '18 at 9:28











  • Possible duplicate of Tkinter: How to use threads to preventing main event loop from "freezing"

    – stovfl
    Nov 13 '18 at 9:35

















  • Could you post the complete traceback related to the posted code?

    – toti08
    Nov 13 '18 at 8:50











  • However I think you should link your button to the Thread, and not to the function itself. I mean, you want the button to start the Thread...Also I don't see where you start Thread2.

    – toti08
    Nov 13 '18 at 9:28











  • Possible duplicate of Tkinter: How to use threads to preventing main event loop from "freezing"

    – stovfl
    Nov 13 '18 at 9:35
















Could you post the complete traceback related to the posted code?

– toti08
Nov 13 '18 at 8:50





Could you post the complete traceback related to the posted code?

– toti08
Nov 13 '18 at 8:50













However I think you should link your button to the Thread, and not to the function itself. I mean, you want the button to start the Thread...Also I don't see where you start Thread2.

– toti08
Nov 13 '18 at 9:28





However I think you should link your button to the Thread, and not to the function itself. I mean, you want the button to start the Thread...Also I don't see where you start Thread2.

– toti08
Nov 13 '18 at 9:28













Possible duplicate of Tkinter: How to use threads to preventing main event loop from "freezing"

– stovfl
Nov 13 '18 at 9:35





Possible duplicate of Tkinter: How to use threads to preventing main event loop from "freezing"

– stovfl
Nov 13 '18 at 9:35












1 Answer
1






active

oldest

votes


















0














You should avoid using threads. Specifically don't make calls to Tkinter GUI methods from worker threads. But in this example you don't need any threads as the winsound function supports playing sounds asynchronously for you already:



winsound.PlaySound(filename, winsound.SND_FILENAME | winsound.SND_ASYNC)


As documented, if you want to stop the currently playing sound then pass None as the first argument.






share|improve this answer























  • THank you so miuch, i just found it tough doing this kind of stuff as it's my first real project/program. thank you

    – TazzyBoy_
    Nov 14 '18 at 6:36










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276441%2fpython-tkinter-gui-freezes-and-music-player-button-wont-worked-for-more-than-on%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You should avoid using threads. Specifically don't make calls to Tkinter GUI methods from worker threads. But in this example you don't need any threads as the winsound function supports playing sounds asynchronously for you already:



winsound.PlaySound(filename, winsound.SND_FILENAME | winsound.SND_ASYNC)


As documented, if you want to stop the currently playing sound then pass None as the first argument.






share|improve this answer























  • THank you so miuch, i just found it tough doing this kind of stuff as it's my first real project/program. thank you

    – TazzyBoy_
    Nov 14 '18 at 6:36















0














You should avoid using threads. Specifically don't make calls to Tkinter GUI methods from worker threads. But in this example you don't need any threads as the winsound function supports playing sounds asynchronously for you already:



winsound.PlaySound(filename, winsound.SND_FILENAME | winsound.SND_ASYNC)


As documented, if you want to stop the currently playing sound then pass None as the first argument.






share|improve this answer























  • THank you so miuch, i just found it tough doing this kind of stuff as it's my first real project/program. thank you

    – TazzyBoy_
    Nov 14 '18 at 6:36













0












0








0







You should avoid using threads. Specifically don't make calls to Tkinter GUI methods from worker threads. But in this example you don't need any threads as the winsound function supports playing sounds asynchronously for you already:



winsound.PlaySound(filename, winsound.SND_FILENAME | winsound.SND_ASYNC)


As documented, if you want to stop the currently playing sound then pass None as the first argument.






share|improve this answer













You should avoid using threads. Specifically don't make calls to Tkinter GUI methods from worker threads. But in this example you don't need any threads as the winsound function supports playing sounds asynchronously for you already:



winsound.PlaySound(filename, winsound.SND_FILENAME | winsound.SND_ASYNC)


As documented, if you want to stop the currently playing sound then pass None as the first argument.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 9:59









patthoytspatthoyts

23.7k24571




23.7k24571












  • THank you so miuch, i just found it tough doing this kind of stuff as it's my first real project/program. thank you

    – TazzyBoy_
    Nov 14 '18 at 6:36

















  • THank you so miuch, i just found it tough doing this kind of stuff as it's my first real project/program. thank you

    – TazzyBoy_
    Nov 14 '18 at 6:36
















THank you so miuch, i just found it tough doing this kind of stuff as it's my first real project/program. thank you

– TazzyBoy_
Nov 14 '18 at 6:36





THank you so miuch, i just found it tough doing this kind of stuff as it's my first real project/program. thank you

– TazzyBoy_
Nov 14 '18 at 6:36



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276441%2fpython-tkinter-gui-freezes-and-music-player-button-wont-worked-for-more-than-on%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế