tkiner输出不在同一个弹出窗口中



好的,所以我想弄清楚的是如何控制输出的位置。目前,当您运行代码时,会弹出一个窗口,允许您输入标签"手册"。选择按回车或点击"提交"按钮。输入的条目每次都会被放到一个新的弹出窗口中,即Input1(test)和input2(testing)将分别出现在不同的窗口中。我如何设置它,使它显示在同一个弹出窗口最好间隔在y轴上,所以它不是杂乱的。由于这是一个手动条目,因此不知道将提交多少条目,并且一旦输入了如此多的条目,它可能需要在X轴上移动并在Y轴的顶部重新开始。

我也想知道如何选择弹出结束在屏幕上我尝试使用win.place(x=200, y=50).Toplevel()没有.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()

要在同一个弹出窗口上显示条目,可以使用下面的条目函数代码

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()
sh = window.winfo_screenheight()
sw = window.winfo_screenwidth()
x_cord = int((sw/2)-550)
y_cord = int((sh/2)-300)
wh = 550
ww = 1100
window.geometry("{}x{}+{}+{}".format(ww, wh, x_cord, y_cord))

#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()

您需要创建一次弹出窗口并在entry_function()中添加标签。

最初你可以弹出隐藏和显示它在entry_function()可以指定使用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
...

最新更新