如何使用 Tkinter 打印硬拷贝?
Tkinter 允许开发人员与本地系统中的文件互动。在本文中,我们将介绍如何使用 filedialog 和 win32api 等 Tkinter 包打印文件的硬拷贝。
若要导入这些包,我们必须先安装这些模块在我们的环境中。若要安装 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()输出
运行上述代码将产生如下输出 −

如果您单击按钮,它将打开一个文件夹,您可以从其中选择一个文件进行打印。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP