0

I have a form which is having multiple rows. By pressing enter it opens up a option window. But every time i hit enter it opens a new option window. How can i control and check if an option window is open then don't open an option window.

import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import messagebox

# Directory/File processing libraries
import os
import configparser
import csv

def callback():
    #messagebox.showinfo("Netezza", Folder_Name_var.get())
    #messagebox.showinfo("Netezza", Table_Name_var.get() )
    config = configparser.ConfigParser()
    config.read('C:\\aa\\config.ini')
    #for value in config['Folder']: print(value)
    for key in config.items('Folder'):
        print (key[1].replace('{Folder_Name}',Folder_Name_var.get()))
        os.makedirs(key[1].replace('{Folder_Name}',Folder_Name_var.get()),exist_ok=True)

def click_tv(event):
    #messagebox.showinfo("Inside")
    selected=trv.focus()
    print(trv.item(selected))

def press_enter(event):
    #messagebox.showinfo("Inside")
    selected=trv.focus()
    print(trv.item(selected))
    print(str((event.keysym)))
    if str((event.keysym))=='Return':
        option_wnd=Toplevel(root)
        option_wnd.geometry('200x200')
        option_wnd.title('Option Window')
        option_wnd.grab_set()
        #option_wnd.pack()
def selection_change(event):
    selected = trv.selection()[0]
    print('You clicked on', trv.item(selected))
    #option_wnd.mainloop()
root = Tk()

Folder_Name_var = tk.StringVar()
Table_Name_var = tk.StringVar()

# This is the section of code which creates the main window
root.geometry('873x498')
root.configure(background='#63B8FF')
root.title('Automation Software - Blue Shield of California')

pic= Canvas(root, height=100, width=100)
#pic= Canvas(root, height=225, width=580)

#picture_file = PhotoImage(file = 'c:\\aa\\bsc.png')
#pic.create_image(0, 0, anchor=NW, image=picture_file)
#pic.place(x=5, y=5)


lbl_Folder_Name = Label(root, text="Folder Name",background='#63B8FF').place(x=600, y=50)
lbl_Table_Name = Label(root, text="Table Name",background='#63B8FF').place(x=600, y=90)

txt_Folder_Name = Entry(root,textvariable = Folder_Name_var).place(x=700, y=50)
txt_Table_Name = Entry(root,textvariable = Table_Name_var).place(x=700, y=90)

Button(root, text="Show", width=10, command=callback).place(x=700,y=120)

tree_frame=Frame(root)
tree_frame.place(x=10,y=260)

tree_scroll=Scrollbar(tree_frame)
tree_scroll.pack(side=RIGHT,fill=Y)

style=ttk.Style()
style.theme_use("default")
style.map("Treeview",background=[('selected','bisque2')],foreground=[('selected','black')])

trv= ttk.Treeview(tree_frame,yscrollcommand=tree_scroll.set, columns=(1,2,3), show="headings", height="10")
trv.heading(1,text="Parameter", anchor=W)
trv.heading(2,text=" Parameter  Description", anchor=W)
trv.heading(3,text=" Specify your value", anchor=W)
trv.tag_configure('even',background="white")
trv.tag_configure('odd',background="steelblue")
trv.bind("<Double-1>",click_tv)
trv.bind("<Return>",press_enter)
trv.bind("<<TreeviewSelect>>",selection_change)
trv.pack()
tree_scroll.config(command=trv.yview)


i=1

with open("c:\\aa\control.txt") as options:
    options_line = csv.reader(options, delimiter='\t')
    for option in options_line:
        #a=1
        if i%2==0:
            trv.insert(parent='', index='end', values=(option), tags=('even',))
            #print("even")
        else:
            trv.insert(parent='', index='end', values=(option), tags=('odd',))
            #print("odd")
        i=i+1
#trv.place(x=100,y=260)


root.mainloop()

Everytime I hit enter on the root window it opens a pop up window. I need to control the it. if the pop up window is open then we should not allow another pop up to open. enter image description here

1
  • You have called option_wnd.grab_set() inside press_enter(), so you cannot trigger another Enter key press event on the treeview. So I wonder how can you open another option_wnd without closing the existing one? Commented Oct 29, 2020 at 9:48

4 Answers 4

2

The direct way is to use winfo_exists():

root.option_wnd = None # Init value

def press_enter(event):
    #messagebox.showinfo("Inside")
    selected=trv.focus()
    print(trv.item(selected))
    print(str((event.keysym)))
    if str((event.keysym))=='Return': 
        if root.option_wnd and root.option_wnd.winfo_exists():
            root.option_wnd.lift() # make this window on the top.
        else: # create this window
            root.option_wnd  = tk.Toplevel(root)
            .....

But I don't think you need to create this window each time when user type Enter.Create it at the beginning, just show it when user type Enter

For example:

root = tk.Tk()
option_wnd = tk.Toplevel()
option_wnd.wm_protocol("WM_DELETE_WINDOW", option_wnd.withdraw) # when user try to close this window, hide it instead of destroy it
.....


option_wnd.withdraw() # hide this window

def press_enter(event):
    #messagebox.showinfo("Inside")
    selected=trv.focus()
    print(trv.item(selected))
    print(str((event.keysym)))
    if str((event.keysym))=='Return': 
        option_wnd.deiconify() # show it.
Sign up to request clarification or add additional context in comments.

Comments

0

one option I can think of is binding the option window like this:

option_wnd.bind('<Return>', lambda e: option_wnd.close()) # or withdraw() or quit() ?

binding the option window to when enter is pressed it gets closed (althought I dont know about differences of above functions(there are so you should look up that)) but this can get in the way if you want to enter values using Enter(Return) key because it will close the window. The other option is to bind option window to this event:

option_wnd.bind('<FocusOut>', lambda e: option_wnd.close())

which is when window is not focused on anymore so if you pressed enter it would open a new one still bet the old one should be closed. Also you can try doing some logic programming with sth like when enter is presses 'turn on' a mode when pressing it again wont open window and when you close the existing window it will again allow that.

3 Comments

def press_enter(event): global option_wnd selected=trv.focus() print(trv.item(selected)) print(str((event.keysym))) option_wnd=Toplevel(root) if str((event.keysym))=='Return' and option_wnd is None: option_wnd.geometry('500x500') option_wnd.title('Option Window') option_wnd.grab_set() print('state - ',option_wnd.state()) else: option_wnd.title('in else') option_wnd.bind('<FocusOut>', lambda e: option_wnd.close())
its going to else but still opens new windows on hitting enter
are you telling this to me?
0

If you do not uses classes you could do the following:

options_displayed = False #global

def option_closed(w):
    global options_displayed
    w.destroy() #close the actual window
    options_displayed = False # log the fact it is now close

def press_enter(event):
    global options_displayed # reference global variable
    #messagebox.showinfo("Inside")
    selected=trv.focus()
    print(trv.item(selected))
    print(str((event.keysym)))
    if str((event.keysym))=='Return' and not options_displayed:
        option_wnd=Toplevel(root)
        option_wnd.geometry('200x200')
        option_wnd.title('Option Window')
        option_wnd.grab_set()
        option_wnd.grab_set()
        option_wnd.protocol("WM_DELETE_WINDOW", lambda w= option_wnd :option_closed(w))
        

9 Comments

Wont you have to set option_wnd to not None inside the if ?
option_wnd is actually set to Toplevel(root)
I tried the above one. the if condition is executing but the option window is getting generated with root property.
After close this toplevel window, the option_hwd is not None. So user could open this window only once.
After changes the option window is not opening but still there is a window with root property is opening.
|
0
def press_enter(event):
    #messagebox.showinfo("Inside")
    root.deiconify ()
    selected=trv.focus()
    print(trv.item(selected))
    print(str((event.keysym)))
    if str((event.keysym))=='Return':
        option_wnd=Toplevel(root)
        option_wnd.geometry('200x200')
        option_wnd.title('Option Window')
        option_wnd.grab_set()
        #option_wnd.pack()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.