鱼C论坛

 找回密码
 立即注册
查看: 1943|回复: 1

[已解决]Tkinter 如 何 实 现 倒 序 输 出 自 己 的 生 日 ?

[复制链接]
发表于 2023-9-18 18:13:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from tkinter import *
  2. root=Tk()
  3. root.title("倒序")
  4. def a(n):
  5.       if n!=0:
  6.             print("%d"%(n%10),end="")
  7.             a(n//10)
  8.             l_4.config(text=a)
  9. l_1=Label(root,text="请输入自己的生日:")
  10. l_1.grid(row=0,column=0)
  11. e_1=Entry(root)
  12. e_1.grid(row=0,column=1,columnspan=2)
  13. l_2=Label(root,text="例如:20061108")
  14. l_2.grid(row=1,column=0)
  15. l_3=Label(root,text="将会倒序输出生日:")
  16. l_3.grid(row=2,column=0)
  17. l_4=Label(root)
  18. l_4.grid(row=2,column=2,columnspan=1)
  19. b_1=Button(root,text="确定",command=a(123456789))
  20. b_1.grid(row=3,column=0)
  21. b_2=Button(root,text="取消",command=root.destroy)
  22. b_2.grid(row=3,column=2)
  23. mainloop()
复制代码
最佳答案
2023-9-18 18:13:52
这段代码是一个使用Tkinter实现的倒序输出生日的小程序。但是代码中存在一些问题,例如Button的command参数不能直接传入函数调用,需要使用lambda表达式或者partial函数来包装函数调用。另外,在Label中使用a函数作为text参数是不正确的,应该先调用a函数获取结果再赋值给Label的text属性。

下面是修正后的代码:

  1. from tkinter import *

  2. root = Tk()
  3. root.title("倒序")

  4. def reverse_birthday():
  5.     n = int(e_1.get())
  6.     res = ""
  7.     while n != 0:
  8.         res += str(n % 10)
  9.         n //= 10
  10.     l_4.config(text=res)

  11. l_1 = Label(root, text="请输入自己的生日:")
  12. l_1.grid(row=0, column=0)

  13. e_1 = Entry(root)
  14. e_1.grid(row=0, column=1, columnspan=2)

  15. l_2 = Label(root, text="例如:20061108")
  16. l_2.grid(row=1, column=0)

  17. l_3 = Label(root, text="将会倒序输出生日:")
  18. l_3.grid(row=2, column=0)

  19. l_4 = Label(root)
  20. l_4.grid(row=2, column=2, columnspan=1)

  21. b_1 = Button(root, text="确定", command=lambda: reverse_birthday())
  22. b_1.grid(row=3, column=0)

  23. b_2 = Button(root, text="取消", command=root.destroy)
  24. b_2.grid(row=3, column=2)

  25. mainloop()
复制代码


在这个修正后的代码中,我们定义了一个名为 reverse_birthday 的函数来实现倒序输出生日的功能。在函数中,我们首先获取Entry中的输入,然后使用while循环和取模运算来实现倒序输出。最后将结果赋值给Label的text属性。在Button的command参数中,我们使用了lambda表达式来包装函数调用,确保点击按钮时才会调用函数。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-18 18:13:52 | 显示全部楼层    本楼为最佳答案   
这段代码是一个使用Tkinter实现的倒序输出生日的小程序。但是代码中存在一些问题,例如Button的command参数不能直接传入函数调用,需要使用lambda表达式或者partial函数来包装函数调用。另外,在Label中使用a函数作为text参数是不正确的,应该先调用a函数获取结果再赋值给Label的text属性。

下面是修正后的代码:

  1. from tkinter import *

  2. root = Tk()
  3. root.title("倒序")

  4. def reverse_birthday():
  5.     n = int(e_1.get())
  6.     res = ""
  7.     while n != 0:
  8.         res += str(n % 10)
  9.         n //= 10
  10.     l_4.config(text=res)

  11. l_1 = Label(root, text="请输入自己的生日:")
  12. l_1.grid(row=0, column=0)

  13. e_1 = Entry(root)
  14. e_1.grid(row=0, column=1, columnspan=2)

  15. l_2 = Label(root, text="例如:20061108")
  16. l_2.grid(row=1, column=0)

  17. l_3 = Label(root, text="将会倒序输出生日:")
  18. l_3.grid(row=2, column=0)

  19. l_4 = Label(root)
  20. l_4.grid(row=2, column=2, columnspan=1)

  21. b_1 = Button(root, text="确定", command=lambda: reverse_birthday())
  22. b_1.grid(row=3, column=0)

  23. b_2 = Button(root, text="取消", command=root.destroy)
  24. b_2.grid(row=3, column=2)

  25. mainloop()
复制代码


在这个修正后的代码中,我们定义了一个名为 reverse_birthday 的函数来实现倒序输出生日的功能。在函数中,我们首先获取Entry中的输入,然后使用while循环和取模运算来实现倒序输出。最后将结果赋值给Label的text属性。在Button的command参数中,我们使用了lambda表达式来包装函数调用,确保点击按钮时才会调用函数。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 17:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表