Python的GUI编程中,如何使button上的数字改变?
我设计了一个程序让4个button上产生随机数字,还有一个按钮是我定义成'retry',我希望按下这个按钮后可以使产生随机数的button上再次生成随机数。该如何编写程序...
我设计了一个程序让4个button上产生随机数字,还有一个按钮是我定义成'retry',我希望按下这个按钮后可以使产生随机数的button上再次生成随机数。该如何编写程序(即下方程序中的函数“ran(a,b,c,d)”怎么定义)?
其中一段(其中input_char()是我定义的另一个函数):
def main():
root=Tk()
calculator24=Frame(root,width=600,height=600)
calculator24.pack(fill=BOTH,expand=1)
addWidgets(calculator24)
root.title('Calculator of 24-point Game')
root.wm_resizable(width=False,height=False)
calculator24.mainloop()
def addWidgets(frame):
a=randint(1,13)
expression=Text(frame,height=2,width=28)
number_1 = Button(frame, text=str(a), width = 5, command = lambda: input_char(str(a), expression))
btn_add_9 = Button(frame, text = 'Retry', width = 5, command = lambda: ran(a,b,c,d))
def ran(a,b,c,d):
a=randint(1,13)
b=randint(1,13)
c=randint(1,13) 展开
其中一段(其中input_char()是我定义的另一个函数):
def main():
root=Tk()
calculator24=Frame(root,width=600,height=600)
calculator24.pack(fill=BOTH,expand=1)
addWidgets(calculator24)
root.title('Calculator of 24-point Game')
root.wm_resizable(width=False,height=False)
calculator24.mainloop()
def addWidgets(frame):
a=randint(1,13)
expression=Text(frame,height=2,width=28)
number_1 = Button(frame, text=str(a), width = 5, command = lambda: input_char(str(a), expression))
btn_add_9 = Button(frame, text = 'Retry', width = 5, command = lambda: ran(a,b,c,d))
def ran(a,b,c,d):
a=randint(1,13)
b=randint(1,13)
c=randint(1,13) 展开
4个回答
2015-01-07
展开全部
retry绑事件,修改按钮的文字
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import os
from time import sleep
from Tkinter import *
class DirList(object):
def __init__(self, initdir = None):
self.top = Tk()
self.label = Label(self.top,text="Driectory List V1.1")
self.label.pack()
self.cwd = StringVar(self.top)
self.dirlabel = Label(self.top,fg='blue',font=('Helvetica',12,'bold'))
self.dirlabel.pack()
#comment add by kinfinger
self.dirfm =Frame(self.top)
self.dirfm.pack() # 被丢失的代码
self.dirsb=Scrollbar(self.dirfm)
self.dirsb.pack(side=RIGHT,fill=Y)
self.dirs =Listbox(self.dirfm,height =15,width= 50,yscrollcommand=self.dirsb.set)
self.dirs.bind('',self.setDirAndGo)
self.dirsb.config(command = self.dirs.yview)
self.dirs.pack(side = LEFT,fill = BOTH)
self.dirn = Entry(self.top,width = 50,textvariable =self.cwd)
self.dirn.bind('',self.doLS)
self.dirn.pack()
self.bfm= Frame(self.top)
self.clr =Button(self.bfm,text ='Clear',command = self.clrDir,activeforeground ='white',activebackground ='blue',)
self.ls =Button(self.bfm,text ='List Directory',command =self.doLS, activeforeground ='white',activebackground ='green')
self.quit =Button(self.bfm,command=self.top.quit,text= 'quit',activeforeground ='white',activebackground ='red')
self.clr.pack(side = LEFT)
self.ls.pack(side = LEFT)
self.quit.pack(side = LEFT)
self.bfm.pack()
if initdir: #comment none
self.cwd.set(os.curdir)
self.doLS
def clrDir(self,ev=None):
self.cwd.set('')
def setDirAndGo(self,ev=None):
self.last = self.cwd.get()
self.dirs.config(selectbackground ='red')
check =self.dirs.get(self.dirs.curselection())
if not check:
check = os.curdir
self.cwd.set(check)
self.doLS()
def doLS(self,ev=None):
error = ''
tdir = self.cwd.get()
if not tdir: tdir = os.curdir
if not os.path.exists(tdir):
error = tdir + ': no such file'
elif not os.path.isdir(tdir):
error =tdir +':not a directory'
if error:
self.cwd.set(error)
self.top.update()
sleep(2)
if not (hasattr(self,'last')) \
and self.last:
self.last = os.curdir
self.cwd.set(self.last)
self.dirs.config(\
selectbackground ='LightSkyBlue')
self.top.update()
return
self.cwd.set(\
'fetch directory contents....'
)
self.top.update()
dirlist = os.listdir(tdir)
print dirlist
dirlist.sort()
if os.chdir(tdir):
print 'success'
else:
print tdir
print os.getcwd()+'+++++++++++'
self.dirlabel.config(text=os.getcwd())
self.dirs.delete(0,END)
self.dirs.insert(END,os.curdir)
print os.curdir+'not change'
for eachFile in dirlist:
self.dirs.insert(END,eachFile)
#print eachFile
self.cwd.set(os.curdir)
self.dirs.config(\
selectbackground ='LightSkyBlue')
def main():
d=DirList(os.curdir)
mainloop()
if __name__ == '__main__':
main()
from time import sleep
from Tkinter import *
class DirList(object):
def __init__(self, initdir = None):
self.top = Tk()
self.label = Label(self.top,text="Driectory List V1.1")
self.label.pack()
self.cwd = StringVar(self.top)
self.dirlabel = Label(self.top,fg='blue',font=('Helvetica',12,'bold'))
self.dirlabel.pack()
#comment add by kinfinger
self.dirfm =Frame(self.top)
self.dirfm.pack() # 被丢失的代码
self.dirsb=Scrollbar(self.dirfm)
self.dirsb.pack(side=RIGHT,fill=Y)
self.dirs =Listbox(self.dirfm,height =15,width= 50,yscrollcommand=self.dirsb.set)
self.dirs.bind('',self.setDirAndGo)
self.dirsb.config(command = self.dirs.yview)
self.dirs.pack(side = LEFT,fill = BOTH)
self.dirn = Entry(self.top,width = 50,textvariable =self.cwd)
self.dirn.bind('',self.doLS)
self.dirn.pack()
self.bfm= Frame(self.top)
self.clr =Button(self.bfm,text ='Clear',command = self.clrDir,activeforeground ='white',activebackground ='blue',)
self.ls =Button(self.bfm,text ='List Directory',command =self.doLS, activeforeground ='white',activebackground ='green')
self.quit =Button(self.bfm,command=self.top.quit,text= 'quit',activeforeground ='white',activebackground ='red')
self.clr.pack(side = LEFT)
self.ls.pack(side = LEFT)
self.quit.pack(side = LEFT)
self.bfm.pack()
if initdir: #comment none
self.cwd.set(os.curdir)
self.doLS
def clrDir(self,ev=None):
self.cwd.set('')
def setDirAndGo(self,ev=None):
self.last = self.cwd.get()
self.dirs.config(selectbackground ='red')
check =self.dirs.get(self.dirs.curselection())
if not check:
check = os.curdir
self.cwd.set(check)
self.doLS()
def doLS(self,ev=None):
error = ''
tdir = self.cwd.get()
if not tdir: tdir = os.curdir
if not os.path.exists(tdir):
error = tdir + ': no such file'
elif not os.path.isdir(tdir):
error =tdir +':not a directory'
if error:
self.cwd.set(error)
self.top.update()
sleep(2)
if not (hasattr(self,'last')) \
and self.last:
self.last = os.curdir
self.cwd.set(self.last)
self.dirs.config(\
selectbackground ='LightSkyBlue')
self.top.update()
return
self.cwd.set(\
'fetch directory contents....'
)
self.top.update()
dirlist = os.listdir(tdir)
print dirlist
dirlist.sort()
if os.chdir(tdir):
print 'success'
else:
print tdir
print os.getcwd()+'+++++++++++'
self.dirlabel.config(text=os.getcwd())
self.dirs.delete(0,END)
self.dirs.insert(END,os.curdir)
print os.curdir+'not change'
for eachFile in dirlist:
self.dirs.insert(END,eachFile)
#print eachFile
self.cwd.set(os.curdir)
self.dirs.config(\
selectbackground ='LightSkyBlue')
def main():
d=DirList(os.curdir)
mainloop()
if __name__ == '__main__':
main()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-01-04
展开全部
水多少字才可以得到经验啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询