YeYe?? 2022-04-21 10:59 采纳率: 75%
浏览 104
已结题

如何实现点击文本tkinter文本框文件路径后自动打开该文件。

img

如上图,如何实现点击文件路径后,自动打开该文件?
代码如下:


import tkinter
import tkinter as tk
from tkinter import ttk 
import os
import tkinter.messagebox
from tkinter.filedialog import askdirectory
# from tkinter import messagebox
# from tkinter import filedialog
# import fnmatch
# from tkinter import scrolledtext

file_dir1='C:/Users/111/Desktop/两票模板'
dir_list1 = os.listdir(file_dir1)
#print(dir_list1)



yexu = tkinter.Tk()
yexu.title("两票模板1.0   Made By:11")
yexu.geometry("900x600+260+70")


# 创建下拉菜单1
cmb1 = ttk.Combobox(yexu,width=30,state='normal')
label1 = tk.Label(yexu,text = "一级菜单")
label1.place(x=10,y=10)
cmb1.pack()
cmb1.pack()
cmb1.place(x=30,y=60)
# 设置下拉菜单中的值
cmb1['value'] = dir_list1 #['上海','北京','天津','广州']
# 设置默认值,即默认下拉框中的内容
cmb1.current(0)

# 执行函数1
def func1(event):
    #text.insert('insert',cmb1.get()+'/')
    #print(cmb1.get())
    file_dir2 = 'C:/Users/YeXuLoveHuiYi/Desktop/两票模板' + '/' + cmb1.get()
    cmb2['value'] = os.listdir(file_dir2)
    #print(dir_list2)
cmb1.bind("<<ComboboxSelected>>",func1)


# 创建下拉菜单2
cmb2 = ttk.Combobox(yexu,width=30)
label2 = tk.Label(yexu,text = "二级菜单")
label2.place(x=120,y=10)
cmb2.pack()
cmb2.place(x=270,y=60)
# 设置下拉菜单中的值
#cmb2['value'] = func()
# 设置默认值,即默认下拉框中的内容
cmb2['value'] = ('待选择')
cmb2.current(0)


# 执行函数2
def func2(event):
    #text.insert('insert',cmb2.get()+'/')
    #print(cmb2.get())
    file_dir3 = 'C:/Users/YeXuLoveHuiYi/Desktop/两票模板' + '/' + cmb1.get()+ '/' + cmb2.get()
    cmb3['value'] = os.listdir(file_dir3)
cmb2.bind("<<ComboboxSelected>>",func2)



# 创建下拉菜单3
cmb3 = ttk.Combobox(yexu,width=30)
label3 = tk.Label(yexu,text = "三级菜单")
label3.place(x=60,y=10)
cmb3.pack()
cmb3.place(x=510,y=60)
# 设置下拉菜单中的值
#cmb2['value'] = func()
# 设置默认值,即默认下拉框中的内容
cmb3['value'] = ('待选择')
cmb3.current(0)


# 执行函数3
def func3(event):
    #text.delete()
    #text.insert('insert','C:/Users/YeXuLoveHuiYi/Desktop/两票模板' + '/' + cmb1.get() + '/' + cmb2.get() + '/' + cmb3.get())
    file_dir3='C:/Users/YeXuLoveHuiYi/Desktop/两票模板' + '/' + cmb1.get() + '/' + cmb2.get() + '/' + cmb3.get()
    cmb4['value'] = os.listdir(file_dir3)
    #print(cmb3.get())
cmb3.bind("<<ComboboxSelected>>",func3)



# 创建下拉菜单3
cmb4 = ttk.Combobox(yexu,width=30)
label4 = tk.Label(yexu,text = "三级菜单")
label4.place(x=60,y=10)
cmb4.pack()
cmb4.place(x=30,y=100)
# 设置下拉菜单中的值
#cmb2['value'] = func()#['上海','北京','天津','广州']
# 设置默认值,即默认下拉框中的内容
cmb4['value'] = ('待选择')
cmb4.current(0)







def func_openfile():
    file_dir='C:/Users/YeXuLoveHuiYi/Desktop/两票模板' + '/' + cmb1.get() + '/' + cmb2.get() + '/' + cmb3.get()
    dir_list = os.path.isfile(file_dir)
    #print(dir_list)
    if dir_list == True:
        os.startfile(file_dir)
    else:
        tkinter.messagebox.showwarning(title='两票模板1.0   Made By:11', message='亲,该路径无此文件哦~')

from tkinter import *
Button(yexu,height=2,text = "     打开文件      ",command = func_openfile, bg='LightSkyBlue',font =('黑体',11,'bold')).place(x=390, y=500)



# 检索
path1 = StringVar()
allfiles=[]
def search():

    for filepath, dirnames, filenames in os.walk(r'C:/Users/YeXuLoveHuiYi/Desktop/两票模板'):
        for filename in filenames:
            allfiles.append(os.path.join(filepath, filename))
    text.delete('1.0', 'end')
    for i in range(len(allfiles)):
        if str(path1.get()) in allfiles[i]:
            print(allfiles[i])
            print(str(path1.get()))
            #print(str(path1.get()) in allfiles[i])
            text.insert('insert',allfiles[i]+'\n')
            #text.bind('<Button-1>', os.startfile(allfiles[i]))

    allfiles.clear()

# def click(event):
#     os.startfile(allfiles[i])


# def clear_entry_value():
#     text.delete('1.0', 'end')


Entry(yexu, textvariable = path1,width=43).place(x=30, y=170)#.grid(row = 0, column = 1)
Button(yexu,height=2, text = "  检索  ", command = search, bg='tomato',font =('黑体',11)).place(x=502, y=160)
#Button(yexu,height=2, text = "  重置  ", command = clear_entry_value, bg='tomato',font =('黑体',11)).place(x=532, y=160)
#Button.bind("<<Botton-1>>",search)

text = tkinter.Text(yexu,font =('黑体',12),width=100, height=10,autoseparators=True)
text.pack()
text.place(x=30, y=230)
# text.tag_add("link", '1.4', '1.9')
# text.tag_config('link', foreground='blue', underline=True)


yexu.mainloop()
  • 写回答

1条回答 默认 最新

  • A Python 萌新花花 2022-04-21 11:38
    关注

    先把这个路径贴到一个button上面,然后用pywin32打开文件就好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月6日
  • 已采纳回答 4月28日
  • 创建了问题 4月21日

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多