tkinter Canvas window not opening from Powershell - python

When running on Win10 the following script against the Python 3.8.2 command line, it works just fine, opens a Tk window and displays a triangle:
from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=400, height=400)
canvas.pack()
canvas.create_polygon(10, 10, 10, 60, 50, 35)
However, when running the same script from the PowerShell command line with
& "C:/Program Files (x86)/Python38-32/python.exe" c:/Users/Me/sample.py
The script terminates without error and without opening the Tk window.
How can I get the Tk window to open while calling the Python interpreter from PowerShell command line?

I believe the issue is that you forgot to add
tk.mainloop()
at the end of your script which keeps the window running.
Let me know if adding this fixes your problem.

Related

Python Playsound not playing any sound when compiled into an .exe file

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

exe made by pyinstaller crashes at calling matplotlib

I am trying to use matplotlib with Tkinter to create a stand-alone executable by pyinstall (command: pyinstaller --onefile main.py).
The program works at the stations where the python installed. However, at the computers without any installed python the program crashes at the line, which calls pyplot (fig = plt.Figure()). The crash occurs without any error.
I've tried upgrading/downgrading matplotlib or pyinstaller, changed Figure() to figure(), re-installed numpy, nothing helped, and I do not know what to do else. I ran it from command prompt and I haven't seen any messages.
UPD: I've tried --debug-imports flag, and found the only line that differs between working and not working station is "exec(bytecode, module.dict)" that exists only in the debug-log of the working program. The line occurs after deprecation warning "D:\Prog_files\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py:493: MatplotlibDeprecationWarning:
The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3."
Do you have any ideas on how I can fix it?
Code:
from tkinter import *
from tkinter import filedialog
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
def setplot(x, y):
fig = plt.Figure()
canvas = FigureCanvasTkAgg(fig, root)
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
ax1 = fig.add_subplot(1, 1, 1)
(l, ) = ax1.plot(x, y)
def Quit():
global root
root.destroy()
def LoadFile():
xx = list(range(0,100))
yy = [i*i for i in xx]
setplot(xx, yy)
root = Tk()
root.geometry('700x500')
panelFrame = Frame(root, height = 60, bg = 'gray')
panelFrame.pack(side = 'top', fill = 'x')
loadBtn = Button(panelFrame, text = 'Plot', command = LoadFile)
quitBtn = Button(panelFrame, text = 'Exit', command = Quit)
loadBtn.pack()
loadBtn.place(x = 10, y = 10, width = 70, height = 40)
quitBtn.place(x = 100, y = 10, width = 70, height = 40)
root.mainloop()
I had a similar problem and after several searches I found a recipe that works. These are my configurations with Anaconda:
python version 3.6
pyinstaller version 3.6
matplotlib version 3.0.3
Do not use pyinstaller with "--onefile" option so in the folder "dist" there will be various ".dll" files. And here is the problem, the file "libiomp5md.dll" is missing!
Then just copy to the folder "dist" the file "libiomp5md.dll" which is located in the Anaconda installation folder ...\Anaconda3\Library\bin
I had the same exact issue in a project that used
matplotlib
pandas
pillow
pyinstaller
pysimplegui
After endless hours trying a million different solutions (DLLs, cmd line parameters, --paths="", hooks, reading the docs, trying cx freeze and py2exe instead etc.) i fixed it randomly by simply using a specific combination of package versions... so frustrating...
python 3.7.10
matplotlib 3.4.1
pyinstaller 4.3
pysimplegui 4.38
The rest of the packages that I mentioned don't seem to have made a difference. I really hope it helps, i know how painful it is. I suggest creating a new anaconda environment from scratch and installing the specific versions with conda install -c conda-forge -n {envNameYouWant} pyinstaller=4.3

Python - Tkinter over xrdp Display issue

I am learning about Python3 scripting/programming with this config:
My PC on win10
remote PC under my tv with Debian 9 on it.
I installed xrdp to get a remote graphical UI to play around with Tkinter.
I wrote this very simple script :
#!/usr/bin/env python3
#coding: utf-8
from tkinter import *
fenetre = Tk()
label = Label(fenetre, text="Hello World")
label.pack()
fenetre.mainloop()
But I hit an issue:
_tkinter.TclError: couldn't connect to display ":10.0"
I understand that's linked to xrdp because if I try it physically on the pc it works well.
Any idea or workaround? :/
Thank you !
I used this with Python3.10 on Ubuntu 20.4 with xrdp and hyper-v running on Windows 11 and it works.
import tkinter as tk
window = tk.Tk()
label = tk.Label(window, text="Hello World")
label.pack()
window.mainloop()

Python using Tkinter (Raspberry Pi running Debian).

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.

Python Tk (Tkinter) not work transparency on ubuntu unity

This code in python 2.7.3 work in windows xp, not work in Ubuntu Unity.
How make in ubuntu window transparency.
# -*- coding: UTF-8 -*-
from Tkinter import Tk
window = Tk()
window.wm_attributes('-alpha',0.8)
window.geometry('500x500+200+211')
window.mainloop()
Try this instead:
from Tkinter import Tk
window = Tk()
window.wait_visibility(window)
window.wm_attributes('-alpha',0.8)
window.geometry('500x500+200+211')
window.mainloop()

Categories