Print all pygame events in a tkinter mainloop - python-3.x

I want to print all events that happen in my application.
I am using python 3 with pygame and I use tkinter.
import pygame
from tkinter import *
from tkinter import ttk
def main():
root = Tk()
app = soundscene(root)
while True:
for event in pygame.event.get():
print (event)
root.mainloop()
if __name__ == "__main__":
main()
This code does not run as expected because the loop blocks, which makes sense
My question is how do I make the content of the "while true" loop part of the eventloop, so it prints out all pygame events?

To receive pygame events, a pygame window / display must be initialized.
"The input queue is heavily dependent on the pygame display module. If the display has not been initialized and a video mode not set, the event queue will not really work." Pygame event documentation.
However, if you do want to use tkinter with pygame, you can embed a pygame window into a tkinter window. This however will only give pygame events when using the embedded window. Here is an example from another question:
import pygame
import tkinter as tk
from tkinter import *
import os
root = tk.Tk()
embed = tk.Frame(root, width = 500, height = 500) #creates embed frame for pygame window
embed.grid(columnspan = (600), rowspan = 500) # Adds grid
embed.pack(side = LEFT) #packs window to the left
buttonwin = tk.Frame(root, width = 75, height = 500)
buttonwin.pack(side = LEFT)
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
screen = pygame.display.set_mode((500,500))
screen.fill(pygame.Color(255,255,255))
pygame.display.init()
pygame.display.update()
while True:
for event in pygame.event.get():
print(event)
pygame.display.update()
root.update()
If you are looking for a way to receive events when using tkinter widgets, you can setup bindings in tkinter. More information at this link

Related

How to take screenshot by ignoring the main window in tkinter? [duplicate]

I am trying to create a translucent window in Tkinter like the one in windows 11
How to do this? If we cannot do this can we capture a part of a screen and blur it using cv2 and use it as a continuously updating background?
No, this is not directly possible with tkinter. But:
If you use PIL, you can get the location of the window, and then take a screenshot, then blur it and then make it your app background. But this wont work if user tries to move/resize the application. But here is a rough code:
from tkinter import *
from PIL import ImageTk, ImageGrab, ImageFilter # pip install Pillow
root = Tk()
root.overrideredirect(1) # Hide the titlebar etc..
bg = Canvas(root)
bg.pack(fill='both',expand=1)
root.update()
# Get required size and then add pixels to remove title bar and window shadow
left = root.winfo_rootx()
top = root.winfo_rooty()
right = left + root.winfo_width()
bottom = top + root.winfo_height()
root.withdraw() # Hide the window
img = ImageGrab.grab((left,top,right,bottom)) # Get the bg image
root.deiconify() # Show the window
img = img.filter(ImageFilter.GaussianBlur(radius=5)) # Blur it
img = ImageTk.PhotoImage(img)
bg.create_image(0,0, image=img, anchor='nw') # Show in canvas
label = Label(root,text='This is a translucent looking app')
bg.create_window(bg.winfo_width()/2,bg.winfo_height()/2,window=label) # Position in the center
root.mainloop()
Output with tkinter:
tkinter is not the best choice if you are trying to go for a modern look, use PyQt and check qtacrylic
Output with PyQt:
For live blur (native Windows blur) use "BlurWindow":
python -m pip install BlurWindow
from tkinter import *
from ctypes import windll
from BlurWindow.blurWindow import blur
root = Tk()
root.config(bg='green')
root.wm_attributes("-transparent", 'green')
root.geometry('500x400')
root.update()
hWnd = windll.user32.GetForegroundWindow()
blur(hWnd)
def color(hex):
hWnd = windll.user32.GetForegroundWindow()
blur(hWnd,hexColor=hex)
e = Entry(width=9)
e.insert(0,'#12121240')
e.pack()
b = Button(text='Apply',command=lambda:[color(e.get())])
b.pack()
root.mainloop()

how to display a timer in python

hey i am making a program which records desktop screen.
So what i want to do is when ever i click on my start button(tkinter Gui) in my gui window.
It should start a timer like 3.... ,2.... ,1.... in big font directly on my desktop screen and not on my tkinter window. and then my function should start.
How can i do that ..
import tkinter as tk
from tkinter import *
root = Tk()
root.title("our program")
start_cap =tk.button(text='start recording' command=start_capute)
start_cap.pack()
root.mainloop()
Not mentioning the functions and the entire code here as not necessary the code is working fine and i just want to add a new feature of the timer in it.
An minimal example:
import tkinter as tk
# from tkinter import *
def Start():
def Count(Number):
if Number == -1:
win.withdraw()
print("Start") # what you want to do
return False
NumberLabel["text"] = Number
win.after(1000,Count,Number-1)
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
win = tk.Toplevel()
win.geometry("+%d+%d"%((screen_width-win.winfo_width())/2,(screen_height-win.winfo_height())/2)) # make it in the center.
win.overrideredirect(1)
win.wm_attributes('-topmost',1) # top window
win.wm_attributes('-transparentcolor',win['bg']) # background transparent.
NumberLabel = tk.Label(win,font=("",40,"bold"),fg='white')
NumberLabel.pack()
win.after(0,lambda :Count(3))
win.mainloop()
root = tk.Tk()
root.title("our program")
start_cap = tk.Button(text='start recording',command=Start)
start_cap.pack()
root.mainloop()

Why can't run both tkinter mainloop and cefpython3 messageloop?

I'm working at a project in Python3 in which i have both tkinter and a frame in tkinter with cef browser.
This is the code.
from tkinter import messagebox
#import threading
from cefpython3 import cefpython as cef
import platform
import sys
from tkinter import *
import time
def on_closing ():
print('closing')
r.destroy()
cef.Shutdown()
r = Tk()
r.geometry('800x600')
r.protocol('WM_DELETE_WINDOW', on_closing)
f = Frame(r, bg = 'blue', height = 200)
f.pack(side = TOP, fill = 'x')
g=Frame(r,bg = 'white',height = 200)
g.pack(side = TOP, fill = 'x')
b1 = Button (g,text='Exit',command = on_closing)
b1.pack (side = LEFT)
b2 = Button (g,text='Show something',command = lambda:messagebox.showinfo('TITLE', 'Shown something'))
b2.pack (side = RIGHT)
sys.excepthook = cef.ExceptHook
rect = [0, 0, 800, 200]
print('browser: ', rect[2],'x',rect[3])
window_info=cef.WindowInfo(f.winfo_id())
window_info.SetAsChild(f.winfo_id(),rect)
cef.Initialize()
browser = cef.CreateBrowserSync(window_info, url='http://www.google.com')
r.update()
cef.MessageLoop()
##_thread = threading.Thread(target=cef.MessageLoop)
##
##_thread.start()
##
##_thread.join()
r.mainloop()
print('end')
The problem is:
I leave cef.MessageLoop() and the browser works but buttons don't.
I comment out cef.MessageLoop() and the browser doesn't work but
tkinter window does.
I was thinking that maybe threading module wuold help but i tried (you can see the commented lines) and doesn't work (i get no exceptions but browser don't work).
How can i sort this out?
Tkinter runs in a single thread so when you write what is basically an infinite loop inside of it then you will block Tkinter from working. The only reason you screen is coming up at all is because you used update() but that will not fix the issue here.
The solution will be to use threading to manage the MessageLoop in a separate thread while also passing the frame to the function to allow for some interaction between Tkinter and cef.
Note: I also cleaned up your code a bit to better follow PEP8 standards.
import tkinter as tk
from tkinter import messagebox
from cefpython3 import cefpython as cef
import threading
import sys
def test_thread(frame):
sys.excepthook = cef.ExceptHook
window_info = cef.WindowInfo(frame.winfo_id())
window_info.SetAsChild(frame.winfo_id(), rect)
cef.Initialize()
browser = cef.CreateBrowserSync(window_info, url='http://www.google.com')
cef.MessageLoop()
def on_closing():
print('closing')
root.destroy()
root = tk.Tk()
root.geometry('800x600')
root.protocol('WM_DELETE_WINDOW', on_closing)
frame = tk.Frame(root, bg='blue', height=200)
frame2 = tk.Frame(root, bg='white', height=200)
frame.pack(side='top', fill='x')
frame2.pack(side='top', fill='x')
tk.Button(frame2, text='Exit', command=on_closing).pack(side='left')
tk.Button(frame2, text='Show something',
command=lambda: messagebox.showinfo('TITLE', 'Shown something')).pack(side='right')
rect = [0, 0, 800, 200]
print('browser: ', rect[2], 'x', rect[3])
thread = threading.Thread(target=test_thread, args=(frame,))
thread.start()
root.mainloop()
Results:

Python code activated with checkbox/button

I want to call function doit when Checkbutton is ON and stop it when it's OFF.
I tried to do it with button and it kinda works, but when I have my CheckButton in ON and I click my button my window freeze and I can't turn it off again.
from tkinter import *
import PIL.ImageGrab
from PIL import ImageGrab
import time
import cv2
import numpy as np
import pyautogui
import random
def doit():
time.clock()
while label_text.get()=="ON":
rgb = PIL.ImageGrab.grab().load()[1857,307]
print(rgb)
print(time.clock())
else:
print('module is turned OFF')
window = Tk()
label_text = StringVar()
label = Label(window, textvariable=label_text)
label_text.set("OFF")
check=Checkbutton(window, text=label_text.get(), variable=label_text,
onvalue="ON", offvalue="OFF")
label.pack()
check.pack(side="left")
b = Button(window, text="OK", command=doit)
b.pack()
window.mainloop()
When you run long-runnign process - your while loop - then mainloop can't work and it can't get mouse/keyboard events from system, sends events to widgets, updates widgets, redraws window.
You can run doit once - without while - and then use after(time, doit) to run it after some time. This way mainloop will have time to do its job.
def doit():
time.clock()
if label_text.get() == "ON":
rgb = PIL.ImageGrab.grab().load()[1857,307]
print(rgb)
print(time.clock())
after(50, doit)
else:
print('module is turned OFF')
Or use window.update() in while to give mainloop time to update elements.
def doit():
time.clock()
while label_text.get() == "ON":
rgb = PIL.ImageGrab.grab().load()[1857,307]
print(rgb)
print(time.clock())
window.update()
else:
print('module is turned OFF')
If PIL.ImageGrab.grab() runs longer then you may have to run it in separated thread.

How to run a python tkinter app without appearing in unity panel or alt-tab

I am working on a countdown timer for Ubuntu using Python and tkinter.
I have created most of the parts and now I want my app to be able run without appearing in Unity panel or Alt-Tab switching sequence. Is there any way to do this?
And also I'd like to whether it is possible to create a moveable window without the title bar. I tried root.overrideredirect(1).
But with it I am unable to move the window.
Here's the code for my program.
#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from tkinter import TOP,LEFT
import time
import datetime
import sys
class Countdown(ttk.Frame):
def __init__(self,parent=None, endDate=None):
ttk.Frame.__init__(self,parent)
self.style = ttk.Style()
self.style.theme_use("clam")
self.pack()
endDate = endDate.split("/")
self.endTime = datetime.datetime(int(endDate[2]),int(endDate[1]),int(endDate[0]))
self.setWidgets()
self.initWidgets()
def setWidgets(self):
self.dLbl = ttk.Label(self,text="0",font="Ubuntu 14 bold")
self.dLbl.pack(padx=10,pady=10,side=LEFT)
self.hLbl = ttk.Label(self,text="0",font="Ubuntu 14 bold")
self.hLbl.pack(padx=10,pady=10,side=LEFT)
self.mLbl = ttk.Label(self,text="0",font="Ubuntu 14 bold")
self.mLbl.pack(padx=10,pady=10,side=LEFT)
def getTimeDelta(self):
self.curDate = datetime.datetime.now()
self.diff = self.endTime - self.curDate
self.tSec = self.diff.total_seconds()
self.days = self.diff.days
h = int(((self.tSec) - self.days*24*60*60)/3600)
self.hours = h if h>0 else 0
m = int(((self.tSec) - (self.hours*60*60+self.days*24*60*60))/60)
self.minutes = m if m>0 else 0
self.sec = int(self.tSec - self.minutes*60)
return [self.days,self.hours,self.minutes+1]
def initWidgets(self):
def set():
dhm = self.getTimeDelta()
self.dLbl["text"]=str(dhm[0])+" Days"
self.hLbl["text"]=str(dhm[1])+" Hours"
self.mLbl["text"]=str(dhm[2])+" Mins"
self.after(1000,set)
set()
root = tk.Tk()
root.title(sys.argv[1])
app = Countdown(root, sys.argv[2])
app.mainloop()
To move a window without borders you can take a look at this question for a simple example of how to implement what you want and just keep building on top of it.
For the hiding, you could use .iconify(), theoretically minimizing the app to the tray thus hiding it, and .deiconify(), for example:
root = tk.Tk()
root.iconify()
ps. If it don't work on Ubuntu/Unity you may have to use other GUI framework with support for this behavior on Ubuntu, like GTK.
import tkinter
root = tkinter.Tk()
root.withdraw()
root.mainloop()
Will hide the window.
Make your own titlebar, like so:
import sys
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk() #create the window
titlebar = tk.Label(root,height=2, bg='cyan', fg='navyblue', text=sys.argv[0]) #create the titlebar
resizable = ttk.Sizegrip(root) #make the window resizable
titlebar.pack(fill='both') # display the titlebar
root.overrideredirect(1) #make the window run without appearing in alt tab
#root.withdraw()
#root.deiconify()
root.geometry('200x200') #set the window size to 200x200
resizable.pack(fill='y',side='right')
def ontitlebarclicked(event):
global lastposition
lastposition=[event.x,event.y]
def ontitlebardragged(event):
global lastposition
windowposition=[int(i) for i in root.geometry().split('+')[1:]] # get the window position
diffposition=[event.x-lastposition[0],event.y-lastposition[1]]
widthheight=root.geometry().split('+')[0]
root.geometry(widthheight+'+'+str(windowposition[0]+diffposition[0])+'+'+str(windowposition[1]+diffposition[1]))
titlebar.bind('<Button-1>',ontitlebarclicked)
titlebar.bind('<Button1-Motion>',ontitlebardragged)
titlebar.focus_force()

Resources