from Tkinter import *import tkMessageBoxmaster = Tk()e = Entry(master)e.pack()e.focus_set()def clr_text():e.delete(0, END)def nameget():namelist = []morename = "yes"n = 0while morename == "yes":name = e.get()namelist = namelist + [name]n = n + 1morename = tkMessageBox.askyesno("Question","yes:input another name;no:view name list")print n,namelistb2 = Button(master, text="clear", width=10, command=clr_text)b2.pack()b1 = Button(master, text="comfirm", width=10, command=nameget)b1.pack()mainloop()
2 回答
当年话下
TA贡献1890条经验 获得超9个赞
因为你点一次都执行一次nameget函数,用意应该跟刚才的差不多,while并不能循环,可以反过来实现,通过while控制b1这个button,没时间弄,我就跟刚才的那样差不多修改了下
from Tkinter import * import tkMessageBox master = Tk() e = Entry(master) e.pack() e.focus_set() global namelist namelist = [] def clr_text(): e.delete( 0 , END) def nameget(): morename = "yes" n = len (namelist) + 1 while morename = = "yes" : name = e.get() namelist.append(name) morename = tkMessageBox.askyesno( "Question" , "yes:input another name;no:view name list" ) print n,namelist b2 = Button(master, text = "clear" , width = 10 , command = clr_text) b2.pack() b1 = Button(master, text = "comfirm" , width = 10 , command = nameget) b1.pack() mainloop() |
胡说叔叔
TA贡献1804条经验 获得超8个赞
我感觉,首先namelist得用global变量,否则每次调用nameget都会清空namelist。在这个思路的基础上,while循环是没有必要的,是/否的选择也不是很重要。我修改的版本是这样:
from Tkinter import * import tkMessageBox master = Tk() e = Entry(master) e.pack() e.focus_set() namelist = [] def clr_text(): e.delete( 0 , END) def nameget(): global namelist, n_namelist #morename = "yes" #while morename == "yes": name = e.get() namelist = namelist + [name] morename = tkMessageBox.askyesno( "Question" , "yes:input another name; no:view name list" ) print len (namelist), namelist b2 = Button(master, text = "clear" , width = 10 , command = clr_text) b2.pack() b1 = Button(master, text = "comfirm" , width = 10 , command = nameget) b1.pack() mainloop() |
如果题主要更清晰准确地实现添加名单、查看名单,应该进一步修改界面。
添加回答
举报
0/150
提交
取消