I'm trying to create a digital soundboard that can play object-defined sounds using tkinter and the playsound module. When I'm testing it via VS code, it works without any problems. But after I compile it into an exe, the app works but there's no sound playing.
I tried changing my compilation mode from '--onefile' to '--onedir' in pyinstaller but it didn't work. I also place the sound files into the folder directory of the executable file, but to no avail.
Could there be something wrong with my code?
my code:
import tkinter
from tkinter import *
from PIL import ImageTk, Image
from playsound import playsound
win = Tk()
win.geometry('500x500')
win.maxsize(500,500)
win.title('Alarm Button')
def alarm():
playsound(r'Announcement.mp3')
btn_image = PhotoImage(file='BUTTON.png')
comp_logo = PhotoImage(file='aaaa.png')
Logo = Label(win, image = comp_logo).place(x=390, y=470)
Press_me = Button(win, image=btn_image, command=alarm, borderwidth = 0)
Press_me.place(x=45, y=15)
def handler(e):
alarm()
win.bind('<Return>', handler)
win.mainloop()
I tried it and it still works, maybe it's because of pyinstaller? Or you can try using auto-py-to-exe since it works for me and also easier to use: https://pypi.org/project/auto-py-to-exe/
Make sure that you use playsound 1.2.2 because it's more stable than the 1.3.0 version. You can do it by using:
pip uninstall playsound
Then:
pip install playsound==1.2.2
Also I see that you import tkinter 2 times? You should delete
import tkinter
Related
I am trying to convert a python script to a mac app so I can distribute it. I'm using cxFreeze to do this. After creating the app, I try to open it but it says the app quit unexpectedly and shows some report.
(code signature invalid (errno=1)
usr/local/lib/Python (no such file)
---
my script at.py:
import tkinter as tk
from tkinter import font
window = tk.Tk()
width=1
window.title('test')
window.geometry("425x500")
label_speed = tk.Label(
text="Speed"
)
label_speed.grid(row=1, column=1, columnspan = 5, stick="w")
window.mainloop()
And then my setup.py
from cx_Freeze import Executable, setup
base = None
if sys.platform == "win32":
base = "Win32GUI"
executables = [Executable("at.py", base=base)]
setup(
name="test",
version="0.1",
description="just for testing",
executables=executables,
)
I used the following commands to make the mac bundle or app.
python3 setup.py build then
python3 setup.py bdist_dmg
I had to use python3 instead of python because it wasn't working for me.
Thanks in advance for any tips and answers
There might be two different things going on. The first thing I know I have run into with cx_freeze is that it tried to map to where it thinks the python 2.x folder should be even if I specify to run on python 3.x. The other thing might be it was downloaded on to a different path. type $ where python to see where the file path should be. If you do $ open $FILEPATH and you see that its using python3 it might be worth reaching out to the maintainer of cx_freeze and see if they have any advice.
My imports:
import tkinter as tk
from PIL import ImageTk, Image
import os
My code:
image = Image.open("path/download.bmp")
root = tk.Tk()
photo = ImageTk.PhotoImage(image)
I tried all apt-get installation commands that I've found and nothing works. Please, help.
When I run commands like sudo apt-get install python3-pil.imagetk, I get
Package python3-pil.imagetk is not available, but is referred to by another package.
... Package 'python3-pil.imagetk' has no installation candidate
So... I didn't find a solution but I ended up just getting rid of ImageTk class, writing something as simple as
photo = PhotoImage(file="path/download.gif")
This solution also required me converting the .bmp to .gif .
I installed TkDnD as instructed here: How to Install and Use TkDnD with Python 2.7 Tkinter on OSX?
Mainly i followed this advice:
On Windows:
1) Copy the tkdnd2.8 directory to C:\Python27\tcl
2) Copy the TkinterDnD2 directory to C:\Python27\Lib\site-packages
I am using anaconda so I copied it into my environments directories (C:\ProgramData\Anaconda3\envs\gui)
And yet when i try to run this code:
import sys
if sys.version_info[0] == 2:
from Tkinter import *
else:
from tkinter import *
from TkinterDnD2 import *
def drop(event):
entry_sv.set(event.data)
root = TkinterDnD.Tk()
entry_sv = StringVar()
entry_sv.set('Drop Here...')
entry = Entry(root, textvar=entry_sv, width=80)
entry.pack(fill=X, padx=10, pady=10)
entry.drop_target_register(DND_FILES)
entry.dnd_bind('<<Drop>>', drop)
root.mainloop()
I get this error
Exception has occurred: RuntimeError
Unable to load tkdnd library.
File "drag_and_drop_GUI.py", line 10, in <module>
root = TkinterDnD.Tk()
Thinking it's a problem with anaconda I installed TkDnD in my system Python too (no virtual environments) but the issue still persists.
Any ideas ?
Try installing the tkdnd binary matching your python installation (64 bit or 32 bit).
I am starting to learn a bit about Python. I am trying to convert Python Tkinter app into exe file. Converting to exe works fine when Tkinter is not involved. I tryed sample file of setup.py and Tkinter app that you can find on official website of cx_Freeze [http://cx-freeze.readthedocs.io/en/latest/index.html] but still geting a lot of errors [like: KeyError: 'TCL_LIBRARY'] in CMD when runing build command. On official website is stated that Python 3.6 is supported.
Here is official example of setup.py:
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [
Executable('app.py', base=base)
]
setup(name='simple_Tkinter',
version='0.1',
description='Sample cx_Freeze Tkinter script',
executables=executables
)
And here is official example of test Tkinter app:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
from tkinter import Tk, Label, Button, BOTTOM
except ImportError:
from Tkinter import Tk, Label, Button, BOTTOM
root = Tk()
root.title('Button')
Label(text='I am a button').pack(pady=15)
Button(text='Button').pack(side=BOTTOM)
root.mainloop()
Instead of cx_freeze you could try and use Pyinstaller it will do the exact same job you are trying to accomplish.
From pip go ahead and type
pip install pyinstaller and then in your programs directory run pyinstaller yourprogram.py
To use Tkinter to open a dialog box I have the following Python 2.7 code:
from Tkinkter import Tk
from tkFileDialog import asksavesfinename
root = Tk().withdraw()
f = asksaveaskfilename()
This works just fine if I run the program under Idle.
However, if I run it, as root, from the LXTerminal, it fails with the exception “Client is not authorized to connect to Server…..”
Any help will be appreciated, Thanks.