|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- from tkinter import *
- root=Tk()
- root.title("倒序")
- def a(n):
- if n!=0:
- print("%d"%(n%10),end="")
- a(n//10)
- l_4.config(text=a)
- l_1=Label(root,text="请输入自己的生日:")
- l_1.grid(row=0,column=0)
- e_1=Entry(root)
- e_1.grid(row=0,column=1,columnspan=2)
- l_2=Label(root,text="例如:20061108")
- l_2.grid(row=1,column=0)
- l_3=Label(root,text="将会倒序输出生日:")
- l_3.grid(row=2,column=0)
- l_4=Label(root)
- l_4.grid(row=2,column=2,columnspan=1)
- b_1=Button(root,text="确定",command=a(123456789))
- b_1.grid(row=3,column=0)
- b_2=Button(root,text="取消",command=root.destroy)
- b_2.grid(row=3,column=2)
- mainloop()
复制代码
这段代码是一个使用Tkinter实现的倒序输出生日的小程序。但是代码中存在一些问题,例如Button的command参数不能直接传入函数调用,需要使用lambda表达式或者partial函数来包装函数调用。另外,在Label中使用a函数作为text参数是不正确的,应该先调用a函数获取结果再赋值给Label的text属性。
下面是修正后的代码:
- from tkinter import *
- root = Tk()
- root.title("倒序")
- def reverse_birthday():
- n = int(e_1.get())
- res = ""
- while n != 0:
- res += str(n % 10)
- n //= 10
- l_4.config(text=res)
- l_1 = Label(root, text="请输入自己的生日:")
- l_1.grid(row=0, column=0)
- e_1 = Entry(root)
- e_1.grid(row=0, column=1, columnspan=2)
- l_2 = Label(root, text="例如:20061108")
- l_2.grid(row=1, column=0)
- l_3 = Label(root, text="将会倒序输出生日:")
- l_3.grid(row=2, column=0)
- l_4 = Label(root)
- l_4.grid(row=2, column=2, columnspan=1)
- b_1 = Button(root, text="确定", command=lambda: reverse_birthday())
- b_1.grid(row=3, column=0)
- b_2 = Button(root, text="取消", command=root.destroy)
- b_2.grid(row=3, column=2)
- mainloop()
复制代码
在这个修正后的代码中,我们定义了一个名为 reverse_birthday 的函数来实现倒序输出生日的功能。在函数中,我们首先获取Entry中的输入,然后使用while循环和取模运算来实现倒序输出。最后将结果赋值给Label的text属性。在Button的command参数中,我们使用了lambda表达式来包装函数调用,确保点击按钮时才会调用函数。
|
|