Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách in ấn (Print Hard Copy) bằng Tkinter?

Tkinter tương tác file hệ thống. Bài viết in file cứng dùng filedialog chọn file và win32api in ấn.

Cài win32api: pip install pywin32

Ví dụ

# Import thư viện
from tkinter import *
from tkinter import filedialog
import win32api

win = Tk()
win.title('Print Hard Copy')
win.geometry("700x400")

def print_file():
    file = filedialog.askopenfilename(initialdir="/", title="Select any file",
        filetypes=(("Text files", "*.txt"), ("all files", "*.*")))
    if file:
        # In ấn qua máy in
        win32api.ShellExecute(0, "Choose a File", file, None, ".", 0)

Button(win, text="Choose a File to Print", command=print_file).pack(pady=20)

win.mainloop()

Kết quả

Chạy code hiển thị nút chọn file in.

Cách in ấn (Print Hard Copy) bằng Tkinter?

Click nút mở thư mục chọn file in.