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()
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
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.
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).
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.
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()