得到上一个条目

时间:2014-03-25 05:10:11

标签: python python-2.7 tkinter

我正在尝试创建一个能够记住上一个条目的tkinter小部件。我遇到的问题是我按下的方法每次按下时都会删除前一个条目。

from Tkinter import *

class step1():
    def __init__(self):
        pass

    def getTextbook(self):
        temp = str(textbook.get())
        textbook.delete(0, END) 
        x = " "
        x += temp
        print x 

    def equal_button(self):
        print getTextbook(self) 


root = Tk()
root.title("step1")
root.geometry("600x600")
s = step1()

app = Frame(root)
app.grid()
label = Label(app, text = "step1")
label.grid()

textbook = Entry(app, justify=RIGHT)
textbook.grid(row=0, column = 0, columnspan = 3, pady = 5)

textbook2 = Entry(app, justify=RIGHT) 
textbook2.grid(row=1, column = 0, columnspan = 3, pady = 5)

button1 = Button(app, text = "Return", command = lambda: s.getTextbook())
button1.grid() 

button2 = Button(app, text="Equal")
button2.grid()

root.mainloop()

1 个答案:

答案 0 :(得分:1)

每次将X设置为getTextbook()时," "中的变量from Tkinter import * root = Tk() textbookList = [] def getTextbook(): textbookList.append(textbook.get()) textbook.delete(0,END) print textbookList textbook = Entry(root) textbook.pack() btn1 = Button(root, text='Return', command=getTextbook) btn1.pack() root.mainloop() 都会被覆盖。请尝试使用列表,并在按下按钮时将每个条目附加到列表中:

{{1}}