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).
Related
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
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.
In my script I am using tkinterdnd2 library to achieve drag and drop functionality from Windows explorer into my tkinter UI.
from tkinterdnd2 import TkinterDnD, DND_FILES
import tkinter as tk
class TkWindow:
def __init__(self):
self.window = TkinterDnD.Tk()
self.tbox = tk.Listbox(self.window)
self.tbox.pack(fill=tk.BOTH)
self.tbox.drop_target_register(DND_FILES)
self.tbox.dnd_bind('<<Drop>>', self.tk_files_dropped)
self.window.mainloop()
def tk_files_dropped(self, event):
messagebox.showinfo("x", event.data)
TkWindow()
When I launch the script - everything works.
But when I freeze the project to a single EXE with PyInstaller, and run it, I get this error:
I tried this solutions already:
I added the pyinstaller-hook as instructed in the tkinterdnd2 repository:
from PyInstaller.utils.hooks import collect_data_files, eval_statement
datas = collect_data_files('tkinterdnd2')
I add --collect-all tkinterdnd2 when executing build command.
I tried copying tkdnd2.8 to tcl8.6 as mentioned in this answer
I tried getting rid of venv and installing all the packages directly into base python interpreter.
I used --collect-all TkinterDnD2 with upperCase.
To convert a Python file to an exe, run pyinstaller.exe --collect-all TkinterDnD2 --windowed yor_app.py. This will create files and folders of the program that was created. You will find a file named TCL. Copy the tkdnd folder into it and then run the exe file
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 .
Today I've started learning python. I only ever used PHP for various things and never had to bother with building exe files.
Using some internet Python programming tutorials and a little of my own editing I came up with random "How many times you've clicked" application as shown below
import winsound
from tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self,master)
self.pack()
self.counts = 0
self.create_widgets()
def create_widgets(self):
Label(self, text = "Test Label").pack()
self.button = Button(self, text = "click", command = self.update_text).pack()
self.result = Label(self, text = "Clicked 0 times")
self.result.pack()
def update_text(self):
self.counts += 1
self.result["text"] = "Clicked " + str(self.counts) + " times"
winsound.PlaySound('sound1.wav', winsound.SND_FILENAME | winsound.SND_ASYNC)
root = Tk()
root.title("Title")
root.geometry("300x120")
app = Application(root)
root.mainloop
Application works fine but the problems started when I tried to compile it to a single exe file for a convenience reasons as I intend to write small "making something a bit easier" programs.
Py2Exe doesn't want to compile the program with bundle_files 1 at all.
PyInstaller did compile it with --onefile, but upon executing, application gives you nothing more than tkinter errors.
Is there a way of creating small one exe files with GUI or is it a dead end?
I'm not going to lie but I really loved the idea of learning python and being able to use it in both web and desktop applications which wasn't very possible in PHP.
I'm using Python 3.4 and tested builtin py2exe and development PyInstaller for Python 3.3 - 3.4.
Edit:
Sample setup.py for py2exe and the error
from distutils.core import setup
import py2exe
setup( windows=[{"script": "text.py"}],
options = {"py2exe": {"bundle_files": 1}
})
Error
C:\Users\SEJBR\Desktop\Python>python setup.py py2exe
running py2exe
1 missing Modules
------------------
? readline imported from cmd, code, pdb
OOPS: tkinter 2
PyInstaller compilation error
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named 'Tkinter'
2515 ERROR: TCL/TK seams to be not properly installed on this system
Well the workaround was quite simple.. I downgraded to Python 2.7.9 and PyInstaller compiled application perfectly with no problems.
I just used
from __future__ import print_function
from __future__ import division
To use Python 3.X print function and apply division changes. If anyone comes up with real fix please answer the question.