0

for example if you have a an image, button, and a label. If you don't click the button or anywhere on the screen/window in 10 seconds, then hide the button and the label. If you click the screen/window then it shows the button and label and resets thanks to Gonzalo Odiard's suggest the issue now is only: How to handle a click on the screen with python gtk if buttons are also there? (how to call custom method "eventListener()" if screen is clicked)

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib

def button_pressed():
    global clicked
    clicked = 0

def eventListener():
    global clicked
    clicked = 0
    btn.set_visible(True)
    lbl.set_visible(True)#set visible

def hide():
    global clicked
    
    if(clicked >=10):
        btn.set_visible(False) #set invisible
        lbl.set_visible(False)
    else:
        clicked += 1
    GLib.timeout_add_seconds(1, hide)

def callback(window, event):
    global countToHide
    countToHide = 0
    btn.set_visible(True)
    lbl.set_visible(True)
    

width, height= pyautogui.size()
builder = Gtk.Builder()
builder.add_from_file("main.glade")#this gets glade class for GUI
win = builder.get_object("Main_Page")#this sets screen/window to var for display later
global clicked
clicked = 0
btn= builder.get_object("button")#get object button from glade
btn.connect("clicked",button_pressed)#handle events of glade button

lbl= builder.get_object("label")#get Label from glade

#initialize event listener for if screen/window is clicked
#?how to do this 
#call method if 10 second go by with out anything being clicked
#this method below
#hide()
GLib.timeout_add_seconds(10, hide)
win.connect('button-press-event', callback)
win.show() #show GUI
Gtk.main()
  • You can use `GLib.timeout_add_seconds()` to call a function with a specified delay. https://lazka.github.io/pgi-docs/GLib-2.0/functions.html#GLib.timeout_add_seconds. Your function should return False to be called only one time, if returns True will be called multipe times with the given interval – Gonzalo Odiard Jan 12 '22 at 00:51
  • thank you for thoughts Gonzalo Odiard `GLib.timeout_add_seconds(1, hide)` could potentially make a loop for checking if system has been clicked. Do you have any thoughts on how to handle if anywhere has been clicked on the screen not just if it had a button or label at the location – Adam Rhoades Jan 12 '22 at 17:36
  • I didn't found a example at hand but you need to read about event propagation https://docs.gtk.org/gtk4/input-handling.html#event-propagation – Gonzalo Odiard Jan 12 '22 at 19:16
  • Sorry, that link is for gtk4. Maybe this is useful https://stackoverflow.com/questions/44174950/python-gtk3-window-listen-to-input-events – Gonzalo Odiard Jan 12 '22 at 19:18
  • thanks for that last link this one I just found right before https://stackoverflow.com/questions/8945681/click-event-on-any-part-of-the-window-with-pygtk-window – Adam Rhoades Jan 12 '22 at 19:35
  • I just adapted it to suit my imports – Adam Rhoades Jan 12 '22 at 19:35
  • Thanks for the help it works pretty good now. I was able figure it out after you put me on the path to GLib.timeout_add_seconds() imported method for the loop. then https://stackoverflow.com/questions/8945681/click-event-on-any-part-of-the-window-with-pygtk-window to handle screen clicked – Adam Rhoades Jan 12 '22 at 19:42

0 Answers0