简体   繁体   English

Python 在搜索框中输入名称并在同名文件夹中查找文件

[英]Python input name in searchbox and find file in folder with same name

Absolute newbie here, I'm just trying to make a simple code where I scan a barcode and then when I hit the button it looks in a specific folder for a file with the same name and opens that file.这里绝对是新手,我只是想制作一个简单的代码来扫描条形码,然后当我点击按钮时,它会在特定文件夹中查找具有相同名称的文件并打开该文件。

I have looked everywhere for an answer but I don't seem to find out how to get the value from the entry box and compare it with the folder files.我到处寻找答案,但似乎不知道如何从输入框中获取值并将其与文件夹文件进行比较。

Also, how do I search for the document folder without having to put the correct username, so that it will work on any account?另外,如何在不输入正确用户名的情况下搜索文档文件夹,以便它可以在任何帐户上使用?

I'm a little stuck if you guys could point me in the right direction it would be greatly appreciated.如果你们能指出我正确的方向,我会有点卡住,我将不胜感激。

Here you can see my code so far, although I'm not sure it is correct.到目前为止,您可以在这里看到我的代码,尽管我不确定它是否正确。

import tkinter as tk
import os

# define print button function to open file
def sendprint():
os.startfile(r"C:\AutoPrint\test.txt")

# create window
window = tk.Tk()
window.geometry('300x200')
tk.Label(window, 
        text="Scan Bracode:").grid(row=0)
window.title('AutoPrint')

# create entrybox
entrybox = tk.Entry(window)
entrybox.grid(row=0, column=1)

# create button
tk.Button(window, 
         text='Print', 
         command=sendprint).grid(row=3, 
                                   column=0, 
                                   sticky=tk.W, 
                                   pady=4)

tk.mainloop()

To get text from Entry从 Entry 中获取文本

    word = entrybox.get()

And later you can use different functions to check in folder稍后您可以使用不同的功能签入文件夹

os.listdir(folder)

def sendprint():
    word = entrybox.get()

    folder = r'C:\AutoPrint'
    
    for filename in sorted(os.listdir(folder)):
        if word.lower() in filename.lower():
            fullpath = os.path.join(folder, filename)
            os.startfile(fullpath)

glob.glob() with * glob.glob()*

def sendprint():
    word = entrybox.get()

    folder = r'C:\AutoPrint'
        
    pattern = os.path.join(folder, f'*{word}*')
    print('pattern:', pattern)

    filenames = glob.glob(pattern)
    print('filenames:', filenames)
    
    #if filenames:
    #   os.startfile(filenames[0])

    for fullpath in filenames:
       os.startfile(fullpath)

@furas Thanks for your help and hints, I've only recently started and am not really sure how to move! @furas 感谢您的帮助和提示,我最近才开始,不太确定如何移动!

I managed to put the code together after following your instructions and works really well only one little thing is bugging me, when no entry is present in the entry box it still opens what I believe it is the first file in the folder.在按照您的说明进行操作后,我设法将代码放在一起,并且运行良好,只有一件小事困扰着我,当输入框中没有条目时,它仍然会打开我认为它是文件夹中的第一个文件的文件。

What could I modify to get it to work as it should?我可以修改什么以使其正常工作?

import tkinter as tk
import tkinter.font as font
import os
import glob
import time
from pynput.keyboard import Key, Controller, Listener
import ctypes
import keyboard

ctypes.windll.shcore.SetProcessDpiAwareness(True)

keyboard = Controller()

# values for widgets coordinates
ref_x=0
ref_y=0

# set values for thickboxes
pro_only=1
pro_x2=2
w_only=3

# create window
##window=ThemedTk(themebg=True)
##window.set_theme('clam')
window = tk.Tk()
window.geometry('650x400')
window.configure(bg='#fdfdfd')
window.title('AutoPrint')
myFont = font.Font(family='Calibri', size=13)

# create entrybox with label
entrybox = tk.Entry(window, width=28)
entrybox.place(x=ref_x + 200, y=ref_y + 10)
entrybox['font'] = myFont
entrybox.focus()
boxlabel = tk.Label(window, text='Scan Barcode:', bg='#fdfdfd')
boxlabel.place(x=ref_x + 15, y=ref_y + 10)
boxlabel['font'] = myFont

# define search funtions
def sendprint():
    word = entrybox.get()
    folder = r'C:\AutoPrint'    
    pattern = os.path.join(folder, f'*{word}*')
    print('pattern:', pattern)
    filenames = glob.glob(pattern)
    print('filenames:', filenames)
    
    if filenames:
       os.startfile(filenames[0])
       time.sleep(3)
       keyboard.press(Key.enter)
       time.sleep(0.5)
       keyboard.release(Key.enter)
       print (filenames)
 
    else:
       print ('Not found')    

# create button
print_button = tk.Button(window, text='Print', command=sendprint, width=15, bg='#00a2ed', fg='white')
print_button.place(x=ref_x + 220, y=ref_y + 280)
print_button['font'] = myFont

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从输入目录文件夹输入并以与输入文件相同的名称将输出文件保存在python中的输出文件夹中 - How to input from input direcory folder and save output file in same name as input file in output folder in python Python:将文件名与同一目录中的文件夹名进行比较 - Python : Comparing file name with folder name in same directory Python:如何将具有特定名称的文件移动到具有相同名称的文件夹中 - Python: how to move a file with a certain name to a folder with the same name 用同一个文件夹python中的第一个文件的名称命名一个文件夹 - Naming a folder with the name of the first file in the same folder python Python需要从文件名中找到输入表名 - Python need to find the input table name from the file name 使用python将文件复制到名称与该文件几乎相同的文件夹 - Using python, copy a file to a folder that has almost the same name as the file 将文件移动到恰好在Python中具有相同文件名的新文件夹中 - Move file to a new folder that happens to have the same file name in Python 如何访问 python 中文件名中具有相同字符串的文件夹中的文件? - How to access files in folder with same string in file name in python? 写一个 function 将两个同名文件(文件扩展名不同)移动到 python 同名文件夹中 - Writing a function to move two files with the same name (with different file extensions) to a folder with the same name in python 如何在python中获取文件夹名称和文件名 - how to get a folder name and file name in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM