鱼C论坛

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

[已解决]文本编辑器:NameError: name 'Tk' is not defined

[复制链接]
发表于 2023-3-10 07:07:50 | 显示全部楼层 |阅读模式
10鱼币
  1. import tkinter
  2. #from Tkinter import *

  3. def popup(event):
  4.     m2.post(event.x_rott,event.y_root)
  5. def hello3():
  6.     print('我是剪切命令')
  7. def hello4():
  8.     print('我是复制命令')
  9. def hello5():
  10.     print('我是粘贴命令')

  11. root=Tk()
  12. root.geometry('400x400+280+280')

  13. v=StringVar(root)
  14. m1=Menu(root)
  15. filemenu=Menu(m1)
  16. editmenu=Menu(m1)
  17. viewmenu=Menu(m1)
  18. for item in ['打开','关闭','退出']:
  19.     filemenu.add_command(label=item,command=hello1)
  20. for item in ['复制','剪切','粘贴']:
  21.     editmenu.add_command(label=item,command=hello1)
  22. for item in ['代码','拆分','设计']:
  23.     viewmenu.add_command(label=item,command=hello1)
  24. m1.add_cascade(label='文件',menu=filemenu)
  25. m1.add_cascade(label='编辑',menu=editmenu)
  26. m1.add_cascade(label='视图',menu=viewmenu)

  27. filemenu.add_separator()

  28. filemenu.add_checkbutton(label='自动保存',command=hello2,variable=v)

  29. m2=Menu(root)
  30. m2.add_command(label='剪切',command=hello3)
  31. m2.add_command(label='复制',command=hello4)
  32. m2.add_command(label='粘贴',command=hello5)
  33. root.bind('<Button-3>',popup)

  34. root['menu']=m1
  35. root.mainloop()
复制代码
最佳答案
2023-3-10 07:07:51
  1. # import tkinter                                  这行注释掉
  2. from tkinter import *                                       # 这行取消注释,且把 Tkinter 改成 tkinter

  3. def popup(event):
  4.     m2.post(event.x_rott,event.y_root)

  5. ##################                你没有写 hello1 和 hello2
  6. def hello1():
  7.     pass
  8. def hello2():
  9.     pass
  10. ##################
  11. def hello3():
  12.     print('我是剪切命令')
  13. def hello4():
  14.     print('我是复制命令')
  15. def hello5():
  16.     print('我是粘贴命令')

  17. root=Tk()
  18. root.geometry('400x400+280+280')

  19. v=StringVar(root)
  20. m1=Menu(root)
  21. filemenu=Menu(m1)
  22. editmenu=Menu(m1)
  23. viewmenu=Menu(m1)
  24. for item in ['打开','关闭','退出']:
  25.     filemenu.add_command(label=item,command=hello1)
  26. for item in ['复制','剪切','粘贴']:
  27.     editmenu.add_command(label=item,command=hello1)
  28. for item in ['代码','拆分','设计']:
  29.     viewmenu.add_command(label=item,command=hello1)
  30. m1.add_cascade(label='文件',menu=filemenu)
  31. m1.add_cascade(label='编辑',menu=editmenu)
  32. m1.add_cascade(label='视图',menu=viewmenu)

  33. filemenu.add_separator()

  34. filemenu.add_checkbutton(label='自动保存',command=hello2,variable=v)

  35. m2=Menu(root)
  36. m2.add_command(label='剪切',command=hello3)
  37. m2.add_command(label='复制',command=hello4)
  38. m2.add_command(label='粘贴',command=hello5)
  39. root.bind('<Button-3>',popup)

  40. root['menu']=m1
  41. root.mainloop()
复制代码

最佳答案

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-3-10 07:07:51 | 显示全部楼层    本楼为最佳答案   
  1. # import tkinter                                  这行注释掉
  2. from tkinter import *                                       # 这行取消注释,且把 Tkinter 改成 tkinter

  3. def popup(event):
  4.     m2.post(event.x_rott,event.y_root)

  5. ##################                你没有写 hello1 和 hello2
  6. def hello1():
  7.     pass
  8. def hello2():
  9.     pass
  10. ##################
  11. def hello3():
  12.     print('我是剪切命令')
  13. def hello4():
  14.     print('我是复制命令')
  15. def hello5():
  16.     print('我是粘贴命令')

  17. root=Tk()
  18. root.geometry('400x400+280+280')

  19. v=StringVar(root)
  20. m1=Menu(root)
  21. filemenu=Menu(m1)
  22. editmenu=Menu(m1)
  23. viewmenu=Menu(m1)
  24. for item in ['打开','关闭','退出']:
  25.     filemenu.add_command(label=item,command=hello1)
  26. for item in ['复制','剪切','粘贴']:
  27.     editmenu.add_command(label=item,command=hello1)
  28. for item in ['代码','拆分','设计']:
  29.     viewmenu.add_command(label=item,command=hello1)
  30. m1.add_cascade(label='文件',menu=filemenu)
  31. m1.add_cascade(label='编辑',menu=editmenu)
  32. m1.add_cascade(label='视图',menu=viewmenu)

  33. filemenu.add_separator()

  34. filemenu.add_checkbutton(label='自动保存',command=hello2,variable=v)

  35. m2=Menu(root)
  36. m2.add_command(label='剪切',command=hello3)
  37. m2.add_command(label='复制',command=hello4)
  38. m2.add_command(label='粘贴',command=hello5)
  39. root.bind('<Button-3>',popup)

  40. root['menu']=m1
  41. root.mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-30 23:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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