亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

你好,請問下為什么while循環只能進行一次?

你好,請問下為什么while循環只能進行一次?

瀟瀟雨雨 2022-05-18 15:11:00
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() = 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()



查看完整回答
反對 回復 2022-05-23
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

我感覺,首先namelist得用global變量,否則每次調用nameget都會清空namelist。在這個思路的基礎上,while循環是沒有必要的,是/否的選擇也不是很重要。我修改的版本是這樣:


from Tkinter import *import tkMessageBoxmaster = Tk()= 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()

 如果題主要更清晰準確地實現添加名單、查看名單,應該進一步修改界面。


查看完整回答
反對 回復 2022-05-23
  • 2 回答
  • 0 關注
  • 370 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號