[英]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.