-1

I have a python script which creates a GUI Window using Tkinter. After creation of the window and reading input I expect the execution to proceed to the while loop, but the execution never resumes as the window isn't destroyed. I want it to resume after the input is read. But here after displaying "You entered xxxx" the screen freezes and execution never gets back to the former while loop. How do I fix this? I don't want to create the window everytime

from PIL import ImageGrab, Image
from glob import glob
from io import BytesIO
import time
import tkinter as tk

window = None
entry = None
window_created = False
x = None
name_of_file = None
input_wait = False

def handle_click(event):
    global name_of_file
    name_of_file = entry.get()
    print("You Entered " + name_of_file)
    entry.delete(0, tk.END)
    hide_window(window)


def show_window(window):
    window.update()
    window.deiconify()


def hide_window(window):
    window.update()
    window.withdraw()


def create_window():
    global window
    global entry
    window = tk.Tk()
    window.geometry("600x600")
    window.title("Name for your image")
    label = tk.Label(text="What do you want to name the image?")
    entry = tk.Entry()
    button = tk.Button(text="Create")
    button.bind("<Button-1>", handle_click)
    window.bind("<Return>", handle_click)
    label.pack()
    entry.pack()
    button.pack()
    window.mainloop()


while True:
    print("Looping")
    image_list = map(Image.open, glob('*.png'))
    presentItem = ImageGrab.grabclipboard()
    if presentItem is not None:
        with BytesIO() as f:
            presentItem.save(f, format='PNG')
            x = Image.open(f)
            if x not in image_list:
                print("New item found")
                if not window_created:
                    create_window()
                    window_created = True
                    print("Resumes")
                else:
                    show_window(window)
                    print("Resumes")
                presentItem.save(name_of_file + '.png', 'PNG')
    else:
        print("No item in clipboard")
    time.sleep(0.1)
Mithil Mohan
  • 223
  • 2
  • 11
  • Replace your `while ...` with `.after(...`. Read [tkinter: how to use after method](https://stackoverflow.com/a/25753719/7414759) – stovfl May 25 '20 at 17:22
  • after thing wouldn't really work because here I want to resume execution only once the input is entered – Mithil Mohan May 25 '20 at 17:23
  • What if I don't want to create the window each time? I would want to create only once – Mithil Mohan May 25 '20 at 17:24
  • 1
    Two ways:1. Use OOP to do that,reconstruct your code.This will create only once(Refer how to use after method)2. Still use `while` loop,but you need to create it each time. – jizhihaoSAMA May 25 '20 at 17:27
  • could you help me with the restructuring using OOP – Mithil Mohan May 25 '20 at 17:29
  • 1
    ***after thing wouldn't really work ... resume ... only ... input is entered***: You can break/resume a ***after_loop_function*** as often you wish. **BREAK**: Don't call `.after(...` again. **RESUME**: Call the ***after_loop_function*** again. – stovfl May 25 '20 at 19:36
  • Your `window = tk.Tk()` is root window hence `create_window()` will end only if you close that window (when mainloop ends). And last but not least: try to do not use global variables (like `window_created ` , `window` and other). – martin-voj May 28 '20 at 22:41

1 Answers1

0

You can hide a tkinter window with window.wm_attributes("-alpha", 0) and show with window.wm_attributes("-alpha", 0.99). This code adds an alpha value. There is a code which shows that better:

import time
from tkinter import *

tk = Tk()

def more_alpha():
    global alpha
    alpha += 0.05
    if alpha >= 1:
        alpha = 0.99
    update()

def less_alpha():
    global alpha
    alpha -= 0.05
    if alpha <= 0:
        alpha = 0.01
    update()

def update():
    global alpha
    tk.wm_attributes("-alpha", alpha)

alpha = 0.5
update()

Button(tk, text="More visibility", command=more_alpha).grid(padx=25, pady=5)
Button(tk, text="Less visibility", command=less_alpha).grid(padx=25, pady=5)

tk.mainloop()

The window is always active, but not when the alpha value is set to 0.

D_00
  • 1,440
  • 2
  • 13
  • 32