Code not printing the events in tkinter python - python

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

Related

Tkinter button not clicking until I switch windows

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

Why can't I display an Image in a tkinter Toplevel() window?

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!

Full screen tkinter canvas python

Hello how can I make a full screen tkinter.Canvas? Can you help me? This is my code:
import tkinter
import datetime
import sys
import os
uvodcanvas = tkinter.Canvas(width=400,height=200,bg="white")
uvodcanvas.pack()
tkinter.mainloop()
You need to make your main window full-screen, then configure the canvas to occupy the whole main window:
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True) # make main window full-screen
canvas = tk.Canvas(root, bg='white', highlightthickness=0)
canvas.pack(fill=tk.BOTH, expand=True) # configure canvas to occupy the whole main window
root.mainloop()
You can set root.attributes to fullscreen.
The following example shows how to toggle from fullscreen to a sized window, and vice-versa, upon pressing the Escape key
import tkinter as tk
def toggle_fs(dummy=None):
state = False if root.attributes('-fullscreen') else True
root.attributes('-fullscreen', state)
if not state:
root.geometry('300x300+100+100')
root = tk.Tk()
tk.Canvas(root, bg='cyan').pack(expand=True, fill=tk.BOTH)
root.attributes('-fullscreen', True)
root.bind('<Escape>', toggle_fs)
root.mainloop()

How do I make my tkinter image pop not show the tkinter option window

I have a simple code that opens an exe. This exe makes a pop-up image using Tkinter but there is an option to delete or minimise the photo. How do I get rid of this option?
here is my code
and here is the TK option I was talking about
[![enter image description here]
and here is the TK option I was talking about
You can try:
CODE_1: where it will show minize, maximize and exit but it will not work
from tkinter import *
window = Tk()
window.title("Grand Canyon")
canvas = Canvas(window, width=500, height=500)
canvas.pack()
#window.overrideredirect(1)
window.attributes('-disabled', True)
window.resizable(0,0)
window.mainloop()
CODE_2: where it will NOT show minize, maximize and exit because it will pack into the top of your window
from tkinter import *
window = Tk()
window.title("Grand Canyon")
canvas = Canvas(window, width=500, height=500)
canvas.pack()
window.overrideredirect(1)
#window.attributes('-disabled', True)
window.resizable(0,0)
window.mainloop()

Python Tkinter : Canvas with Image takes forever to execute

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

Categories

Resources