简体   繁体   中英

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

    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 *

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM