在python tkinter中多次获得相同的输入?

时间:2013-04-28 14:40:51

标签: python input tkinter tkinter-entry

我正在创建一个程序来获取多个用户名并将它们存储在XML文件中。 XML部分工作正常,但我遇到了GUI问题。我希望能够询问用户他想输入多少用户,然后重复Entry次。在保持quit功能的同时,我该怎么做?我试过了:

    def quitter():
        exit()
        quit()

    quitterButton = Button(master, text="Quit", command=quitter)
    mainCanvas.create_window(50, 330, window=quitterButton, tag="quitter")
    num = int(raw_input("How many users are you going to put in?"))
    for x in range(0,num):

        #Create User entry Variable
        userEnter = StringVar()
        usrVar = ""
        #Create enter box:
        userEntryBox = Entry(master, textvariable = userEnter)
        mainCanvas.create_window(250, 300, window=userEntryBox, tag="UserEnterA")

        def gotInput(self):
            usrVar = userEnter.get();
            mainCanvas.create_text(250, 330, text="User Inputted: " + usrVar, tags="0")
            mainCanvas.delete("UserEnterA")

        #Bind entry box
        userEntryBox.bind('<Key-Return>', gotInput)
        userEntryBox.wait_window(userEntryBox)

        #Create a new user element 
        newUsr= ET.Element('Member')

        #Add element to the Root
        root.append(newUsr)

        #Make a sub element Name, set name
        usrName = ET.SubElement(newUsr, 'Name')
        usrName.text = usrVar;
        ...
    tree.write('./output.xml')

最好的解决方法是什么?我不知道输入的数量,我希望退出按钮始终有效。

1 个答案:

答案 0 :(得分:2)

你的程序行为对我来说有点不清楚,但我尽力帮助。

第一个解决方案:显示tkinter askstring对话框num次。如果用户按for按钮,您可以中断Cancel循环。这不是你想要的,但它很容易实现:

from tkinter import *
import tkinter.simpledialog as simpledialog

def add_users():
    n = simpledialog.askinteger('', 'How many users are you going to put in?', initialvalue=1, minvalue=1, maxvalue=10)
    if not n: # 'Cancel'
        return
    for i in range(n):
        user = simpledialog.askstring('', 'User #%s from #%s' % (i+1, n))
        if user is None: # 'Cancel'
            return
        # Do something useful
        print(user)

root = Tk()
Button(root, text='Add users', command=add_users).pack(padx=50, pady=50)
Button(root, text='Quit', command=root.destroy).pack(pady=30)
root.mainloop()

其次(如果您想使用quit按钮将条目和所有新名称放入窗口):

from tkinter import *
import tkinter.simpledialog as simpledialog

class YourApp():
    def __init__(self):
        self.root = Tk()
        Button(self.root, text='Quit', command=self.root.destroy).pack(pady=20)
        self.ask_button = Button(self.root, text='Add users', command=self.add_users)
        self.ask_button.pack(padx=50, pady=50)
        self.root.mainloop()
    def add_users(self):
        self.users_count = 0
        self.user_name = StringVar()
        self.frame = Frame(self.root)
        self.frame.pack()

        self.users_count = simpledialog.askinteger('', 'How many users are you going to put in?', initialvalue=1, minvalue=1, maxvalue=10)
        self.user_entry = Entry(self.frame, textvariable=self.user_name)
        self.user_entry.pack(pady=10)
        self.user_entry.bind('<Key-Return>', self.on_new_user)
        self.user_entry.focus_set()
    def on_new_user(self, event):
        # your code
        print(self.user_name.get())
        Label(self.frame, text='User Inputted: %s' % self.user_name.get()).pack()
        self.users_count -= 1
        if not self.users_count:
            self.user_entry.destroy()
        self.user_name.set('')

YourApp()

Tkinter中有三个几何管理器:place(在您的情况下它与画布非常相似),gridpackCanvas通常用于绘制图片或图表。