Okay so the script runs where you type a entry press submit and a pop-up window shows what was typed. I have two issues I'd like help figuring out. Blank submissions and clearing the text after hitting submit.
Blank submission: input1: hello input2: world input3: "nothing typed" input4:hello world
I get a blank line on input 3 but I don't want anything to happen(no error message needed)
I tried to add .strip() but that didn't work / I didn't put it in correctly I tried it on the end of line 13 name=entry.get().strip
and I tried adding a line entry.strip()
Clearing text: input1: welcome input2: to input3:python I have to manually erase the text inputs for the entry widget before I can type the next line
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 600, y 300))
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:
Made some changes to the code (explanation in code comments):
import tkinter as tk
# no need to use flag because just change win to None
# and check if it is None rather than using an
# additional variable
win = None
# this would set win variable to None
def set_win_none(event=None):
global win
win = None
def entry_function(event=None):
global win
name = entry.get()
# check if the string is empty, if so
# stop the function
if not name:
return
# clear the entry
entry.delete('0', 'end')
# as mentioned win can be used, it just shortens the code
if win is None:
win = tk.Toplevel()
# set geometry as needed, this
# will place the window
# on the right of the main window
win.geometry(f'100x100 {window.winfo_x() window.winfo_width()} {window.winfo_y()}')
win.bind('<Destroy>', set_win_none)
tk.Label(win, text=name).pack()
else:
tk.Label(win, text=name).pack()
window = tk.Tk()
label = tk.Label(window, text="Manual:", bg="dark grey", fg="white")
entry = tk.Entry(window)
button = tk.Button(window, text="submit", command=entry_function)
# better bind the return key to the entry since it is
# directly involved
entry.bind('<Return>', entry_function)
# very rarely do you need to use .place
# better use .grid or .pack
label.grid(row=0, column=0, padx=5)
entry.grid(row=0, column=1, padx=5)
button.grid(row=0, column=2, padx=5)
window.mainloop()
There is rarely a need to use .place
, better use .grid
or .pack
method for placing widgets, they are far more dynamic and adjust to how the window changes.
Also:
I strongly suggest following PEP 8 - Style Guide for Python Code. Function and variable names should be in snake_case
, class names in CapitalCase
. Don't have space around =
if it is used as a part of keyword argument (func(arg='value')
) but have space around =
if it is used for assigning a value (variable = 'some value'
). Have space around operators ( -/
etc.: value = x y
(except here value = x y
)). Have two blank lines around function and class declarations.