Tkinter Toplevel 小部件多次获取用户输入

Tkinter Toplevel widget get user input multiple times

我正在尝试使用 Toplevel 小部件获取用户输入以创建订单,但提交按钮无法按预期工作,请协助。

def spawn_window(self):
    top = Toplevel()
    top.title("Available Electronics")
    self.entrytext = StringVar()
    Entry(top, textvariable=self.entrytext).pack()
    button = Button(top, text="Dismiss", command=top.destroy)
    button.pack(side='right')
    submit = Button(top, text ="Submit", command = self.datainput)
    submit.pack(side='left')



def datainput(self):
    input_var = self.entrytext.get()
    self.devices.append(input_var)

"wanted the text box to turn blank as soon as the submit button was clicked in order to create room for another entry"现在清楚你真正想要的是什么了!

在您的 datainput 方法中,只需在末尾清除您的 stringvar,如下所示:

def datainput(self):
    input_var = self.entrytext.get()
    self.devices.append(input_var)
    self.entrytext.set("")