can't invoke "wm" command: application has been destroyed - python

import tkinter as tk
from tkinter import *
import pyautogui
root = tk.Tk()
#label = tk.Label(root, text='you coomand hacking', font= ('Times New Roman','80'), fg='black', bg='white')
#label.pack()
root.title("Wallpaper")
wall = PhotoImage(file = "20170227180026_3.gif")
wall_label = Label(image = wall)
root.geometry(f"{wall.width()}x{wall.height()}")
root.overrideredirect(True)
currentMouseX,currentMouseY = pyautogui.position()
print(f'{currentMouseX} {currentMouseY}')
root.geometry("+0+0")
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")
wall_label.place(x = -2,y = -2)
root.after(1000 , root.destroy)
#root.geometry("+{}+{}".format(currentMouseX,currentMouseY))
root.geometry("+{}+{}".format(currentMouseX,currentMouseY))
root.mainloop()
root.after(1000 , root.destroy)
root.geometry("+{}+{}".format(currentMouseX,currentMouseY))
Please understand even if the source code is dirty.
What I want is for the image to follow the mouse
I tried to follow the mouse by repeating the image disappearing and appearing, but it is not easy.

Here. I made the image follow the mouse, without pyautogui.
Code:
import tkinter as tk
from tkinter import *
#import pyautogui
root = tk.Tk()
#label = tk.Label(root, text='you coomand hacking', font= ('Times New Roman','80'), fg='black', bg='white')
#label.pack()
root.title("Wallpaper")
wall = PhotoImage(file = "20170227180026_3.gif")
c = Canvas(root, height = 1200, width = 1200)
c.pack()
pic = c.create_image(600, 600, image = wall)
def movepic(event):
x,y = event.x, event.y
c.coords(pic, x, y)
c.bind_all("<Motion>", movepic)
root.mainloop()

Related

How to detect keyboard press in python window?

from tkinter import *
root = Tk()
root.title('FAS')
root.geometry("600x650")
#root.attributes('-alpha', 0.5)
root.wm_attributes("-topmost", True)
root.lift()
root.wm_attributes("-transparentcolor", "grey")
#background image
bg = PhotoImage(file="3MtoCW.png")
#this is to make the background image transparents
red_frame = Frame(root, width=600, height=650, bg='grey')
red_frame.place(x=0, y=0)
my_label = Label(root, image=bg, bg='grey')
my_label.place(x=0, y=0)
root.mainloop()
I want that when I press a key in the tkinter window, the original background image "3MtoCW.png" will swap to a new background image called "3MswCW.png"
How can I do that?
You need to create a function that would change the Label's configuration on keypress and bind this function to the Event
The below should do the trick
from tkinter import *
root = Tk()
root.title('FAS')
root.geometry("600x650")
# root.attributes('-alpha', 0.5)
root.wm_attributes("-topmost", True)
root.lift()
root.wm_attributes("-transparentcolor", "grey")
bg_images = ["3MtoCW.jpg", "3MswCW.jpg"]
idx = 0
# background image
bg = PhotoImage(file="3MtoCW.png")
red_frame = Frame(root, width=600, height=650, bg='red')
red_frame.place(x=0, y=0)
my_label = Label(root, image=bg, bg='grey')
my_label.place(x=0, y=0)
def keypress(e):
print("called")
global idx
idx = (idx + 1) % 2
my_label.config(image=PhotoImage(file=bg_images[idx]))
red_frame.bind("<KeyPress>", keypress)
red_frame.pack()
red_frame.focus_set()
root.mainloop()
You want to bind an event to root. Here's a list of events you can "look" for
https://www.python-course.eu/tkinter_events_binds.php
bg = PhotoImage(file="3MtoCW.png")
root.bind('<KeyPress>', lambda: bg.configure(file="3MswCW.png")
or instead of using lambda, you can do this in a new function. Make sure to omit brackets when you bind the event or it will call the function instead of pointing to it.
def changeimage(event=None):
bg.configure(file="3MswCW.png")
root.bind('<KeyPress>', changeimage)

How to set python window center position

i want set python window center position when the form is loaded how set.what i tried so far i attached below
from tkinter import *
from tkinter import messagebox
from subprocess import call
root = Tk()
root.title("Main")
root.geometry("500x500")
global e1
global e2
def Ok():
call(["python", "Main.py"])
Label(root, text="Welcome").place(x=10, y=10)
Button(root, text="Add Student", command=Ok ,height = 3, width = 13).place(x=10, y=100)
root.mainloop()
Does this center your window?
from tkinter import *
from tkinter import messagebox
from subprocess import call
root = Tk()
root.title("Main")
window_width,window_height = 500,500
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
position_top = int(screen_height/2 - window_height/2)
position_right = int(screen_width / 2 - window_width/2)
root.geometry(f'{window_width}x{window_height}+{position_right}+{position_top}')
def Ok():
pass
Label(root, text="Welcome").place(x=10, y=10)
Button(root, text="Add Student", command=Ok ,height = 3, width = 13).place(x=10, y=100)
root.mainloop()

Adding an image to a button in Tkinter

I am trying to add an image to a button, but I have got some issues when I try to execute the current code. All it shows is an image with no words. I can't even see the button either. Is there some way of fixing my current code?
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
root.mainloop()
You need to pack (or grid) your button in the window, here is how you could do:
import tkinter as tk
from tkinter import PhotoImage
def print_hello():
print('hello')
root = tk.Tk()
root.geometry("960x600")
imagetest = PhotoImage(file="giftest.gif")
button_qwer = tk.Button(root, text="asdfasdf", image=imagetest, command=print_hello)
button_qwer.pack() # <-- don't forget to place the button in the window
root.mainloop()
You can have both text and image displayed on your button, using the compound option, like this:
button_qwer = tk.Button(root, image=imagetest, text="asdfasdf", compound="top", command=print_hello)
compound options are bottom, center, left, none, right, or top
You are making the button successfully but you are not drawing it onto the screen/interface. Use pack , place or grid.
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
Your full code can be like:
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
root.mainloop()

Python canvas colour change by button

I am a python self learner. I was stuck on some practice.
My idea was to create a pop out GUI with buttons that can change the canvas colour.
from Tkinter import *
import ttk
import tkMessageBox
root = Tk()
root.title("Colour!")
canvasColor = "yellow"
def buttonRed() :
canvas = Canvas(root, bg = "red", height=100, width=100)
canvas.grid(row=0,column=2)
button = ttk.Button(root, text="Red", command = buttonRed)
button.grid(row=2,column=1)
button2 = ttk.Button(root, text ="Green", command = buttonGreen)
button2.grid(row=2,column=2)
button3 = ttk.Button(root, text="Blue", command = buttonBlue)
button3.grid(row=2,column=3)
canvas = Canvas(root, bg = canvasColor, height=200, width=200)
canvas.grid(row=0,column=2)
root.configure(background='white')
root.mainloop()
i haven't put in the green and blue button command yet, but instead of creating a new canvas when the colour button clicked, i just wanted to have the default canvas colour change.
Any help will be much appreciated!
Thanks in advance.
I think this is what you need -
from Tkinter import *
import ttk
import tkMessageBox
root = Tk()
root.title("Colour!")
canvasColor = "yellow"
def buttonRed() :
canvas.config(background="red")
def buttonGreen() :
canvas.config(background="green")
def buttonBlue() :
canvas.config(background="blue")
button = ttk.Button(root, text="Red", command = buttonRed)
button.grid(row=2,column=1)
button2 = ttk.Button(root, text ="Green", command = buttonGreen)
button2.grid(row=2,column=2)
button3 = ttk.Button(root, text="Blue", command = buttonBlue)
button3.grid(row=2,column=3)
canvas = Canvas(root, bg = canvasColor, height=200, width=200)
canvas.grid(row=0,column=2)
#canvas.config(background="black")
root.configure(background='white')
root.mainloop()

How to display VideoFileClip in canvas Tkinter

I want to display the video in canvas instead opening in a new window.
And I want to know how to stop and pause the video.
import pygame
from Tkinter import *
def movie():
from moviepy.editor import *
pygame.display.set_caption('Hello World!')
clip = VideoFileClip('1.mp4')
clip.preview()
pygame.quit()
root = Tk()
root.title("AVATAR")
label = Label(root, fg="dark green")
label.pack()
frame = Frame(root,background='red')
frame.pack()
canvas = Canvas(height=200,width=200)
canvas.pack()
conversationbutton = Button(frame, text='play in canvas',width=25,fg="green", command=movie)
conversationbutton.pack(side = RIGHT)
stopb=Button(root, text="stop").pack()
Thanks

Categories

Resources