I am trying to build a program which gets me an enlarged photo of the text I want, for this I decided to use tkinter, win32gui and pygetwindow modules after taking some tips from already asked problems on stack overflow am having the following problems:
(1)I don't know how to get the hwnd value of the tkinter window which I created.
(2)I can't get hwnd value even if I know how to get it as the window is created after the complete code has run.
So please suggest me solutions to the problem
This is my code:
from tkinter import *
import win32gui
import pygetwindow as gw
#making the tkinter window
root = Tk()
root.title('DaysLeft')
#getting all the windows with their hwnd values
hwnd=gw.getAllWindows()
print(hwnd)
win32gui.SetForegroundWindow(hwnd)
bbox = win32gui.GetWindowRect(hwnd)
img = ImageGrab.grab(bbox)
img.show()
mainloop()
The above code gives error below as expected:.
line 26, in <module>
win32gui.SetForegroundWindow(hwnd)
TypeError: The object is not a PyHANDLE object
You can use PIL for taking a screenshot and win32gui or pygetwindow to get windows location.
Install PIL by saying
pip install Pillow
then your working code would be:
from tkinter import *
from win32gui import FindWindow, GetWindowRect
import pygetwindow as gw
from PIL import ImageGrab
def ss():
win = gw.getWindowsWithTitle('DaysLeft')[0]
winleft = win.left+9
wintop = win.top+38 #change 38 to 7 to not capture the titlebar
winright = win.right-9
winbottom = win.bottom-9
final_rect = (winleft,wintop,winright,winbottom)
img = ImageGrab.grab(final_rect)
img.save('Required Image.png')
#making the tkinter window
root = Tk()
root.title('DaysLeft')
root.after(3000,ss)
root.mainloop()
Why am i subtracting some amount from the pixels? its because, windows has decorations like drop shadow effect to the windows, which are also part of the windows and will be included in the screenshot, so i used this to get rid of those extra pixels.
Or if your still reluctant on using win32gui then, change the function to:
from win32gui import FindWindow, GetWindowRect
from PIL import ImageGrab
......
def ss():
win = FindWindow(None, 'DaysLeft')
rect = GetWindowRect(win)
list_rect = list(rect)
list_frame = [-9, -38, 9, 9] #change -38 to -7 to not capture the titlebar
final_rect = tuple((map(lambda x,y:x-y,list_rect,list_frame))) #subtracting two lists
img = ImageGrab.grab(bbox=final_rect)
img.save('Image.png')
What is after method? It just calls the function after 3000 ms, i.e, 3 seconds. We are basically giving the system some time to build the GUI and capture screenshot.
Hope it helped, do let me know if any errors or doubts.
Cheers
Related
Im Using tkVideoPlayer To Run A Video In My Tkinter Program and when i set the resolution to 1920x1080 the video starts playing extremely slowly, and if i dont set the specific resolution myself it becomes very small(Very Small), but it runs normally. keep in mind that the resolution of the video im using is already 1920x1080. can someone help me?
Source Code ATM:
import time
import os
from subprocess import call
from playsound import playsound
import pygame
from pygame import mixer
import tkinter as tk
from tkinter import *
from tkVideoPlayer import TkinterVideo
def introBIORUNNERDEF():
#Setup For Previewing
root = Tk()
root.title('SNCC3')
root.geometry("1920x1080")
root.config(bg="black")
def BIOSCYCLEDEF():
bioInt1Frame = tk.Frame(root, bg="black")
bioInt1Frame.place(relwidth=1, relheight=1, rely=0)
#bioInt2Frame = tk.Frame(root, bg="black")
#bioInt2Frame.place(relwidth=1, relheight=1)
bioInt1 = TkinterVideo(master=bioInt1Frame,scaled=True)
bioInt1.load(r"ViD/BIOINTFULL.mp4")
bioInt1.pack(expand=True,fill="both")
bioInt1.play()
#bioInt2Frame.place(relwidth=1, relheight=1, rely=0)
#bioInt2 = TkinterVideo(master=bioInt2Frame,scaled=True)
#bioInt2.load(r"ViD/BIOINTFULL.mp4")
#bioInt2.pack(expand=True,fill="both")
#bioInt2.play()
BIOSCYCLEDEF()
root.mainloop()
introBIORUNNERDEF()
I dont know what to try, i tried following the guide and following a tutorial video but still nothing.
i made a simple Tool with Tkinter, and i want this tool to stay display on desktop even if i click on browser or other program ,so i want the tool always stay on front until i click minimize .
thank you .
import pyautogui as p
import time
import tkinter
from tkinter import ttk
from tkinter import *
top = Tk()
top.title("past")
def brk():
#from dofus-map.com
time.sleep(0.2)
p.moveTo(238,627)
p.tripleClick()
p.hotkey('ctrl','c')
#from website
time.sleep(0.1)
p.moveTo(84,742)
p.tripleClick()
p.hotkey('ctrl','v')
#to game
time.sleep(0.1)
p.click(88,801)
p.tripleClick()
but = Button(text="past",width = 6, height = 3, font = ("blood",15,""), bg = "lightgreen", fg = "black" , command=brk)
but.pack()
top.mainloop()
Try using this
top.attributes("-topmost", True)
This will always keep your window on top of the screen, regardless of what other programs you use.
I'm trying to show and hide a png (With no frame, margin, or title bar) but can't find a good way to do it. I tried using matplotlib but the code has to be in the main thread so I can't figure out how to use functions to show or hide it. I also tried tkinter but ran into the same problem where calling it from a function the code would just stop when trying to call it from a function.
Is there a simple way to accomplish this?
Below is what I am trying to accomplish though the code doesn't display any image. The code doesn't return an error either, it just stops executing at mainloop without displaying anything unless the code is in the main thread.
#Import modules
import os #For working directory
from pynput import keyboard #For hotkeys
from tkinter import *
from PIL import ImageTk,Image
#Set default directory
os.chdir('C:\\Users\\Username\\Desktop\\Python')
#Define global variables
global img
#Functions
def ExitProgram():
print('test')
#sys.exit(0)
quit() #Will not work in production, switch to sys.exit
return
def ShowImage():
root = Tk()
canvas = Canvas(root, width=300, height=300)
canvas.pack()
img = ImageTk.PhotoImage(Image.open("menu.png"))
canvas.create_image(20, 20, anchor=NW, image=img)
root.overrideredirect(True) # removes title bar
root.mainloop()
print('test')
return
#Define hotkeys
with keyboard.GlobalHotKeys({
'<ctrl>+q': ExitProgram,
'<ctrl>+0': ShowImage}) as h:
h.join()
Im using PIL and TKinter to open an image. I do not understand why I'm getting this error
import os
import random
from PIL import Image
import time
from tkinter import *
root = Tk()
def timer(mins):
time.sleep(mins * 60)
def anmuViewer():
random_pic = (random.choice(os.listdir("D:/de_clutter/memez/anmu")))
openPic = Image.open('D:/de_clutter/memez/anmu/' + random_pic)
openPic.show()
timer(3)
start_btn = Button(root, text = "Start", command = anmuViewer)
start_btn.pack()
root.mainloop()
what should happen is a tkinter window should pop up with only a button called "start". When I click that button, a new window with the image should pop up. instead I get this error
line 17, in anmuViewer
openPic = Image.open('D:/de_clutter/memez/anmu/' + random_pic)
AttributeError: type object 'Image' has no attribute 'open'
Like Michael Butscher said,
"tkinter.Image" overwrites "PIL.Image" in your module's namespace. Avoid imports with *
But if you must use a wildcard import, importing tkinter before PIL should solve your problem
from tkinter import *
import os
import random
from PIL import Image
import time
I am new to GUI programming and recently started working with tKinter.
My problem is that the program won't show my image, I'm suspecing that it is my code that is wrong, however, I would like somone to exactly explain to me how i can make it work...
Here's my code:
from tkinter import * # Import the tkinter module (For the Graphical User Interface)
from PIL import ImageTk, Image
width = 1920
height = 1080
RootGeo = str(width) + "x" + str(height) # Make a def for RootGeo so the Root geometry isn't hardcoded
def MakeWindow():
# -----Root_Attributes-----
Root = Tk()
Root.geometry(RootGeo)
Root.state("zoomed")
# -----Root_Attributes, Root_Containers----- ### NOT WORKING ###
__DISPlAY__ = Image.open("Display.png")
__DISPLAY_RENDER__ = ImageTk.PhotoImage(__DISPlAY__)
Display_icon = Label(Root, image=__DISPLAY_RENDER__)
Display_icon.image = __DISPLAY_RENDER__
Display_icon.place(x=0, y=0)
# -----Root_Containers----- ### NOT WORKING ###
Root.mainloop()
MakeWindow()
Any and all help would be very appreciated.
try to change the image and check out if its still not showing up.
if its still not showing up, try to change this line:
__DISPlAY__ = Image.open("Display.png")
to
__DISPlAY__ = Image.open("Display.png").resize((600,800))
see if it would show up now then change the width and height as you like.
Pychamarm does not want to show the images, so to solve this problem i had to run the script from cmd every time...