鱼C论坛

 找回密码
 立即注册
查看: 3153|回复: 3

[技术交流] Python小白进化之路004_'tkinter' has no attribute 'filedialog'

[复制链接]
发表于 2019-8-13 14:33:20 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 大头目 于 2019-8-14 10:18 编辑

最近想做个小交互界面,遇见个没碰到过的问题:
AttributeError: module 'tkinter' has no attribute 'filedialog'
把 tkinter全部插入居然用不了filedialog(小甲鱼的旧版书内没有写到这个问题)
百度查到:
这说明filedialog是tkinter模块下的一个子模块,并不是他的函数和性质。不能直接去调用filedialog模块下的函数;需要引入子模块filedialog,再去使用它的函数。
这个问题提示我们使用python的过程中,需要注意子模块和函数的性质。
同时这也是类中的相关知识,子模块就是子类,函数是父类中的函数。


  1. from tkinter import *
  2. from tkinter import filedialog   #filedialog需要单独插入,类似于子类的存在

  3. root = Tk()
  4. root.title('图片搜索小程序')
  5. Label(root,text = '搜索文件夹:').grid(row = 0)
  6. Label(root,text = '输出文件夹:').grid(row = 1)
  7. e1 = Entry(root,width = 50)
  8. e2 = Entry(root,width = 50)

  9. e1.grid(row = 0, column = 1, padx = 10, pady = 10)
  10. e2.grid(row = 1, column = 1, padx = 10, pady = 10)

  11. def openfile():
  12.     fileName = filedialog.askopenfilename()
  13.     taget = e1.get()
  14.     output = e2.get()
  15.     print('选取样本:',fileName)
  16.     print('搜索文件夹:',taget)
  17.     print('输出文件夹:',output)
  18. #    e1.delete(0,END)
  19. #    e2.delete(0,END)
  20.    

  21. Button(root, text = '选取样本', width = 10, command = openfile)\
  22. .grid(sticky = W, padx = 20, pady = 10)

  23. #Button(root, text = '退出', width = 10, command = root.quit)\
  24. #.grid(row = 3, column = 1, sticky = W, padx = 10, pady = 5)

  25. mainloop()
复制代码

本帖被以下淘专辑推荐:

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

使用道具 举报

 楼主| 发表于 2019-9-3 11:00:05 | 显示全部楼层
  1. #实际测试如下方式也可以
  2. import tkinter as tk
  3. def openfile():
  4.     global input_path
  5.     input_path = tk.filedialog.askopenfilename()
  6.     if input_path:
  7.         tk.messagebox.showinfo('Return','选择的输入路径为%s' % input_path)
  8.     else:
  9.         tk.messagebox.showinfo('Return','您还未选择输入路径')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-3 11:02:37 | 显示全部楼层
另外在设置窗口位置是小甲鱼由于是from tkinter import *,导致sticky = W参数变量设置一下子没看懂W是什么东西。其实就是sticky = tk.W,第一次碰到脑子没转过弯来!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-28 14:11:32 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-28 17:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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