鱼C论坛

 找回密码
 立即注册
查看: 1423|回复: 6

[已解决]tkinter 按钮有返回值的,要怎么写???

[复制链接]
发表于 2020-7-27 17:08:43 | 显示全部楼层 |阅读模式

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

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

x
  1. from tkinter import *
  2. from tkinter import filedialog

  3. root = Tk()
  4. root.geometry("300x200")
  5. root.resizable(0, 0)


  6. def show():
  7.     path = filedialog.askopenfilename()
  8.     textvar.set(path)
  9.     file = textvar.get()


  10. button = Button(root, text='浏览', font=('宋体', 12), command=show)
  11. button.place(x=238, y=30)

  12. textvar = StringVar()
  13. textvar.set('请输入文件路径...')
  14. filepath = Entry(root, width=25, textvariable=textvar, font=('宋体', 11))
  15. filepath.place(x=15, y=34)

  16. mainloop()
复制代码





show 函数中; 得到Entry的字符串内容 ;怎么反回来 以便全后面使用?  


比如我想要输出 Entry的字符串内容  
最佳答案
2020-7-27 17:24:39
maxliu06 发表于 2020-7-27 17:20
如果在代码最后, print(file)  ;它会提法  file 没定义的




给你加上线程打印:

  1. from tkinter import *
  2. from tkinter import filedialog
  3. from threading import Thread

  4. root = Tk()
  5. root.geometry("300x200")
  6. root.resizable(0, 0)


  7. def show():
  8.     path = filedialog.askopenfilename()
  9.     textvar.set(path)
  10.     global file
  11.     file = textvar.get()
  12.     t.start()


  13. button = Button(root, text='浏览', font=('宋体', 12), command=show)
  14. button.place(x=238, y=30)
  15. file = None
  16. textvar = StringVar()
  17. textvar.set('请输入文件路径...')
  18. filepath = Entry(root, width=25, textvariable=textvar, font=('宋体', 11))
  19. filepath.place(x=15, y=34)

  20. def d():
  21.     print(file)

  22. t = Thread(target=d)


  23. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-27 17:10:43 | 显示全部楼层


先在全局初始化一个变量,然后进函数 global 声明下重新赋值即可:

  1. from tkinter import *
  2. from tkinter import filedialog

  3. root = Tk()
  4. root.geometry("300x200")
  5. root.resizable(0, 0)


  6. def show():
  7.     path = filedialog.askopenfilename()
  8.     textvar.set(path)
  9.     global file
  10.     file = textvar.get()


  11. button = Button(root, text='浏览', font=('宋体', 12), command=show)
  12. button.place(x=238, y=30)
  13. file = None
  14. textvar = StringVar()
  15. textvar.set('请输入文件路径...')
  16. filepath = Entry(root, width=25, textvariable=textvar, font=('宋体', 11))
  17. filepath.place(x=15, y=34)

  18. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-27 17:20:11 | 显示全部楼层
Twilight6 发表于 2020-7-27 17:10
先在全局初始化一个变量,然后进函数 global 声明下重新赋值即可:

如果在代码最后, print(file)  ;它会提法  file 没定义的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-27 17:21:00 | 显示全部楼层
本帖最后由 Twilight6 于 2020-7-27 17:22 编辑
maxliu06 发表于 2020-7-27 17:20
如果在代码最后, print(file)  ;它会提法  file 没定义的


你直接 print 肯定不行呀,代码都运行完不会回头了,除非你用线程,或者放在 show 里面

话说也不会报错吧,是你放的位置也有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-27 17:24:39 | 显示全部楼层    本楼为最佳答案   
maxliu06 发表于 2020-7-27 17:20
如果在代码最后, print(file)  ;它会提法  file 没定义的




给你加上线程打印:

  1. from tkinter import *
  2. from tkinter import filedialog
  3. from threading import Thread

  4. root = Tk()
  5. root.geometry("300x200")
  6. root.resizable(0, 0)


  7. def show():
  8.     path = filedialog.askopenfilename()
  9.     textvar.set(path)
  10.     global file
  11.     file = textvar.get()
  12.     t.start()


  13. button = Button(root, text='浏览', font=('宋体', 12), command=show)
  14. button.place(x=238, y=30)
  15. file = None
  16. textvar = StringVar()
  17. textvar.set('请输入文件路径...')
  18. filepath = Entry(root, width=25, textvariable=textvar, font=('宋体', 11))
  19. filepath.place(x=15, y=34)

  20. def d():
  21.     print(file)

  22. t = Thread(target=d)


  23. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-27 17:38:37 | 显示全部楼层
Twilight6 发表于 2020-7-27 17:24
给你加上线程打印:

理解理解 , 为何它不能直接return...
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-30 19:17:56 | 显示全部楼层
Twilight6 发表于 2020-7-27 17:21
你直接 print 肯定不行呀,代码都运行完不会回头了,除非你用线程,或者放在 show 里面

话说也不会 ...

用线程会同步是吗,还是怎么回事,还是说写在函数里面会安全一点?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-19 10:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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