python tkinter GUI freezes and music player button won't worked for more than once
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
add a comment |
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
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
add a comment |
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
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
python tkinter winsound
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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