I've been experimenting a little with the Canvas function of Tkinter and tried to add some imagefile to the window, to test if i can create proper background images.The code so far seems to be working fine (atleast i get no errors when executing). But as soon as i try to execute the Python script it just takes forever to load and doesn't display a window or anything. As soon as i remove the Canvas block from my script it works fine.
from Tkinter import *
root = Tk()
root.title("ImageTest")
root.geometry("350x150")
root.minsize(350,150)
root.maxsize(350,150)
#***** Canvas *****
photo = PhotoImage(file="derp.gif")
w = Canvas(root, width=350, height=150)
w.pack()
w.create_image(0,0, anchor=NW, image=photo)
w.image = photo
root.mainloop()
Related
Code Part 1
from tkinter import *
from PIL import Image, ImageTk
import threading
c1 = "#262626"
root = Tk()
root.overrideredirect(True)
root.configure(bg=c1)
def destroy():
global root
root.destroy()
def pr():
print("asd")
destroy()
def animation():
threading.Timer(0.01, pr).start()
animation()
root.mainloop()
after the destroy () command has worked, the window is closed, but the program continues to work, you can disable it only with the help of the task manager or CMD, but there's no any errors.
Code Part 2
login = Tk()
img = Image.open(selfDir + "\\ok.png").resize((50, 50), Image.ANTIALIAS)
test = ImageTk.PhotoImage(img)
label1 = Label(login, image=test, bg=c1)
label1.image = test
label1.place(x=330, y=145, width=50, height=50)
login.mainloop()
if I run this part after the first one, I see an error:
tkinter.TclError: can't invoke "image" command: application has been destroyed
but if I run only this part, without the first one, then it works and I can see the photo in the window
So the problem is.
I need to close window after threading.Timer()
Then i need to open window, with an image
i just need to write
root.after(10, pr)
instead of
threading.Timer(0.01, pr).start()
special thanks to Matiiss
I am following a Tkinter tutorial and I have a very basic GUI built. When I run it, the GUI opens and everything is fine, except the button is not immediately clickable. The only way I can get it to click is if I switch to a full screen window and back to the GUI. Below is the code I am using, along with a screenshot. Does anyone know why this is happening? If it does not make enough sense I can upload a video. Thank you.
import tkinter as tk
from tkinter import Frame
HEIGHT = 700
WIDTH = 800
root = tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
frame = tk.Frame(root, bg='#ff9a7f')
frame.place(relwidth=2, relheight=1)
button = tk.Button(root, text='Test Button')
button.pack()
root.mainloop()
I'm trying to create a python program tkinter that, upon the pressing of a button, opens a new full screen tkinter window containing an image and plays an audio file - here's my code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.png"))
label = Label(window, image=img).pack()
playsound("song.mp3")
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
(my image and audio file are both on the desktop with my python file)
However, when I run my code, when I press the button, the audio plays but no second tkinter window opens.
I've tried to destroy() the buttonWindow and have tried many different ways of including an image on a tkinter window - if I remove the line of code using PhotoImage(), the window appears (obviously I then get a syntax error stating that 'img' is not defined).
How could I solve this?
Thanks,
Louis
I had similar problem.
But i resolve this trial and error method.
First of all i don't use pillow library and put image into Label
You should try define Photoimage object NOT IN FUNCTION
Example:
It will work:
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
def popUp():
popff = tk.Toplevel(root)
popff.geometry("400x200")
labelImgff = tk.Label(popff, image=imgff, bg="white")
labelImgff.grid(row=0, column=0)
imgff = tk.PhotoImage(file="/home/user/image.png")
btnff = tk.Button(text="startPOP", command=popUp)
btnff.grid(column=0,row=0)
root.mainloop()
effect of action
Your playsound() command is blocking execution. The playsound() command has an optional field 'block', which is True by default. Changing this to False will continue execution and allow mainloop() to continue.
Second, just call label.draw() to draw your image to the TopLevel window.
Here's the code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.jpeg"))
label = Label(window, image=img).pack()
playsound("song.mp3",block=False)
label.draw()
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
buttonWindow.mainloop()
Cheers!
So I'm trying to make a code that prints the events/actions that happens on the tkinter area. When I run the script I don't get an error but when I'm clicking on the graphics area nothing prints out.
import tkinter
canvas = tkinter.Canvas(width=640, height=480)
canvas.pack()
def function1(event):
print(repr(event))
canvas.bind("ButtonPress-1", function1)
canvas.mainloop()
You need to define the instance of tkinter.Tk() and use it as root. The following implementation works for me as expected:
import tkinter
root = tkinter.Tk()
def function1(event):
print(repr(event))
canvas = tkinter.Canvas(root, width=640, height=480)
canvas.bind("<ButtonPress-1>", function1)
canvas.pack()
root.mainloop()
I'm currently developing a Windows application that requires the use of an .exe file I've created (called kill_game.exe in the below example). This .exe file accepts serialized objects as arguments. I call this .exe file using subprocess.Popen. When I do so, an exact copy of my current window opens and my .exe file won't run until I close the 2nd window myself. I'm completely stumped as to why it happens. I'd like the .exe file to run without the 2nd GUI opening itself. I tried to switch to multiprocessing as it exists in this example on SO, but that didn't work either.
Below, I'll post a simplified version (my original version is 300+ lines) of my code that illustrates the GUI and the function that is used to open the .exe file. I'll also post pictures of what happens when I press the button.
from tkinter import *
from tkinter import Tk
import subprocess
root = Tk()
sizex = sizey = 500
posx = posy = 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
myframe = Frame(root, relief=GROOVE, width=500, height=500, bd=1)
myframe.place(x=0, y=0)
canvas = Canvas(myframe)
frame = Frame(canvas)
canvas.pack(side="left")
canvas.create_window((0, 0), window=frame, anchor='nw')
button = Button(frame, text="Hello World")
button.grid(row=0, column=2)
games_to_stop = []
button.bind("<Button>",
lambda event, game_list=games_to_stop: some_function(game_list))
def some_function(game_list):
#This function is the function of interest, which is bound to my "Hello
#World" Button
child_script = r"C:\Users\Willy\AddictKiller\dist\kill_game.exe"
subprocess.Popen([child_script, game_list])
root.mainloop()
This is the GUI before the button is pressed:
This is the GUI and the 2nd GUI (which for some reason looks the the GUI of my non-simplified code) after the button is pressed:
I had a similar problem where when I run my program duplicates of my main Tkinter window would open and the function wouldn't run until I close all the duplicates.
you fix this by putting the part where the tkiner window is being created behind an if statement to make sure you only create a window in the main window as such
if __name__ == "__main__":
root = Tk()