Tkinter를 사용하면 개발자가 로컬 시스템 내부의 파일과 상호 작용할 수 있습니다. 이 기사에서는 filedialog와 같은 Tkinter 패키지를 사용하여 파일의 하드카피를 인쇄하는 방법을 볼 것입니다. 및 win32api 모듈.
이러한 패키지를 가져오려면 먼저 환경에 이러한 모듈을 설치해야 합니다. win32api를 설치하려면 pip install pywin32를 사용합니다.
예
#import the required libraries
from tkinter import *
from tkinter import filedialog
import win32api
#Create an instance of tkinter frame or window
win= Tk()
win.title('Print Hard Copy')
win.geometry("700x400")
#Define function
def print_file():
file= filedialog.askopenfilename(initialdir="/", title="Select any file",filetypes=(("Text files", "*.txt"), ("all files", "*.*")))
if file:
#Print Hard copy using Printer
win32api.ShellExecute(0, "Choose a File", file, None, ".", 0)
#Create a button for printing event
button= Button(win, text="Choose a File to Print", command=print_file).pack(pady= 20)
#Keep running the window or frame
win.mainloop() 출력
위의 코드를 실행하면 다음과 같은 출력이 생성됩니다 -

버튼을 클릭하면 인쇄할 파일을 선택할 수 있는 폴더가 열립니다.