在TKinter中列表为空时列表时调用列表

时间:2016-05-03 21:58:56

标签: python tkinter

我希望从用户输入输入的列表中打印,然后从列表中打印该输入。但是,如果列表中没有空白字符串,则此操作无效,因为它表示列表太短,无法在标签中显示数据。我想要在按下继续按钮之后立即打印输入,而不是在按下下一个按钮之后。

from tkinter import *

class Traveller:
    def __init__(self, parent):
        self.names = [""]
        self.E_phone = "f"
        self.E_name = "q"
        self.count = 0

        self.go = Frame(parent, width=500, height=450, bg="snow", pady=30, padx=10)
        self.go.grid(row=1, column=0)
        self.go.grid_propagate(0)  # to reserve space required for frame

        self.dataView = Frame(parent, width=500, height=500, bg="snow", pady=30, padx=10)

        name = Label(self.go, text="Name:", bg="snow")
        name.grid(row=1, column=0, sticky=E)
        self.E_name = Entry(self.go, width=40)
        self.E_name.grid(row=1, column=1, sticky=W, pady=4)

        menuButton = Button(self.go, text="Continue", command=self.dataSave)
        menuButton.grid(row=2, column=1, pady=4)

        dataTitle = Label(self.dataView, text="Here is all of the inputted data:", bg="snow")
        dataTitle.grid(row=2, column=0)

        dataExit = Button(self.dataView, text="Return", command=self.returnT)
        dataExit.grid(row=1, column=0, pady=5)

        nextData = Button(self.dataView, text="Next", command=self.NData)
        nextData.grid(row=4, column=0, pady=5)

        prevData = Button(self.dataView, text="Previous", command=self.PData)
        prevData.grid(row=4, column=1, pady=5)

        self.everything = Label(self.dataView, text="The person" + self.names[self.count], bg = "snow")
        self.everything.grid(row=3, column = 0)

    def dataSave(self):

        self.names.append(self.E_name.get())
        self.go.grid_remove()
        self.dataView.grid(row=1, column=0)
        self.dataView.grid_propagate(0)
        # clearing the entry boxes
        self.E_name.delete(0, END)

    def returnT(self):
        self.dataView.grid_remove()
        self.go.grid(row=1, column=0)
        self.go.grid_propagate(0)

   def NData(self):
        self.count = self.count + 1
        self.everything.configure(text = "The person " + self.names[self.count])


def PData(self):
    if self.count >= 1:
        self.count = self.count - 1
        self.everything.configure(text = "The person " + self.names[self.count])



# main routine
if __name__ == "__main__":
    root = Tk()
    root.title("Traveller Details")
    play = Traveller(root)
    root.geometry("500x450+0+0")
    root.mainloop()

0 个答案:

没有答案