Home > database >  tkiner output isnt on the same pop-up window
tkiner output isnt on the same pop-up window

Time:10-07

Okay so what I'm trying to figure out is how to control the location of output. Currently when you run the code a window comes up and allows you to type into a label "manual". with the option to press enter or click the 'submit' button. The typed entry is then put in a new pop-up window every time I.e. Input1(test) and input2(testing) will show up in separate windows. How do I set it so it shows up in the same pop-up window preferably spaced on the y axis so it isn't cluttered. As this is a manual entry there is no telling how many entries will be submitted and it may be possible once so many entries have been entered it will need to move over on the X axis and start again at the top of the Y axis.

I'd also like to know how to pick where the pop-up ends up on the screen I tried using win.place(x=200, y=50) but the .Toplevel() doesn't have an attribute of .place()


import tkinter as tk

#function that prints the entry
def entry_function(e=None):
    name = entry.get()
    win = tk.Toplevel()
    tk.Label(win, text=name).pack()
    win.geometry('100x100')

 #Setup for the window
window = tk.Tk()
window.title("Title_Name")
window.geometry("500x500")
window.bind('<Return>', entry_function)


#widgets
label = tk.Label(window, text = "Manual:", bg = "dark grey", fg = "white")
entry = tk.Entry()
button = tk.Button(text = "submit",
                   command = entry_function)

#widget placement
label.place(x=0,y=20)
entry.place(x=52,y=21)
button.place(x=177, y=18)

window.mainloop()

CodePudding user response:

For displaying the entry on same popup, you can use the below code for entry function

import tkinter as tk

flag = False
win=""

def setflag(event):
    global flag
    flag = False
    
#function that prints the entry
def entry_function(e=None):
    global flag,win
    name = entry.get()
    if not flag:
        win = tk.Toplevel()
        win.geometry('100x100')
        win.bind('<Destroy>', setflag)
        tk.Label(win, text=name).pack()
        flag = True
        win.geometry(" %d %d" % (x   200, y   50))
    else:
        tk.Label(win, text=name).pack()
     

 #Setup for the window
window = tk.Tk()
window.title("Title_Name")
window.geometry("500x500")
window.bind('<Return>', entry_function)
x = window.winfo_x()
y = window.winfo_y()

#widgets
label = tk.Label(window, text = "Manual:", bg = "dark grey", fg = "white")
entry = tk.Entry(window)
button = tk.Button(window,text = "submit",
                   command = entry_function)

#widget placement
label.place(x=0,y=20)
entry.place(x=52,y=21)
button.place(x=177, y=18)

window.mainloop()

CodePudding user response:

You need to create the popup once and add label to it inside entry_function().

You can make the popup hidden initially and show it inside entry_function() and you can specify the position of the popup using geometry().

def entry_function(e=None):
    MAX_ROWS = 20  # adjust this value to suit your case
    name = entry.get().strip()
    if name:
        n = len(popup.winfo_children())
        tk.Label(popup, text=name).grid(row=n%MAX_ROWS, column=n//MAX_ROWS, sticky='nsew', ipadx=2, ipady=2)
        popup.deiconify()  # show the popup
        window.focus_force()  # give focus back to main window

...
# create the popup
popup = tk.Toplevel()
popup.withdraw() # hidden initially
popup.geometry(' 100 100') # place the popup to where ever you want
popup.title("Log Window")
popup.protocol('WM_DELETE_WINDOW', popup.withdraw) # hide popup instead of destroying it
...
  • Related