1

我正在制作一个程序来获取多个用户名并将它们存储在 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')

绕过它的最佳方法是什么?我不知道输入的数量,我希望退出按钮一直工作。

4

1 回答 1

3

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

第一个解决方案:显示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(在您的情况下与 canvas 非常相似)、gridpack画布通常用于绘制图片或图表。

于 2013-04-28T20:21:27.477 回答