如何使用 Python 3 的 Tkinter 打开和打印文件(在打印机中)?

How to Open and Print a File (In a Printer) using Python 3's Tkinter?

再次向社区问好!如何在 Python 上使用 Tkinter 打开 LibreOffice 或 Word 文件(在我的 Mac 上,但最终也是 Windows) 3. 我的 UI 有几个我想要的按钮与特定文件相关联,当按下该按钮时,它应该向 Brother 打印机发送命令并打印该文件。当我在这里时,我还想知道是否有一个脚本来检测 OS 用户正在使用什么并执行单独的命令(因为 Mac 和 Windows 有不同的要求)也使用 Tkinter。我的代码如下。

注意:如果您查看函数 'SignOut',它会搞砸一切。这就是它被评论的原因。

`from tkinter import*
from tkinter import filedialog
import time


def UserPassCheck():
    if e1.get()=='masteryw' and e2.get()=='10897':
        root.destroy()
        OpenWindow()
    elif e1.get()=='Santosh' and e2.get()=='89882':
        root.destroy()
        OpenWindow()
    elif e1.get()=='Ankur' and e2.get()=='41712':
        OpenWindow()
        root.destroy()
    elif e1.get()=='Suchin' and e2.get()=='09655':
        OpenWindow()
        root.destroy()
    elif e1.get()=='Jing' and e2.get()=='34241':
        OpenWindow()
        root.destroy()
    elif e1.get()=='Vishal' and e2.get()=='98017':
        OpenWindow()
        root.destroy()
    elif e1.get()=='Richard' and e2.get()=='99716':
        OpenWindow()
        root.destroy()
    elif e1.get()=='Veni' and e2.get()=='77601':
        OpenWindow()
        root.destroy()
    else:
        root.destroy()
        root2=Tk()
        root2.title('Error')
        root2.geometry('600x600')
        label5=Label(root2, text='Access Denied. Press Button & Reopen command')
        label5.grid(row=0, column=1)
        buttonagain=Button(root2,text='Try Again?', command=root.destroy)
        buttonagain.grid(row=4, column=1)

root=Tk()
root.title("Secure Login")
root.geometry('600x600')
yw=Label(root, text='YoungWonks Secure Login')
yw.grid(row=1, column=1)
label=Label(root, text='Username')
label.grid(row=2, column=1)
label1=Label(root, text='Password')
label1.grid(row=4, column=1)
e1=Entry(root)
e1.grid(row=2, column=2)
e2=Entry(root, show='*')
e2.grid(row=4, column=2)
buttonlog=Button(root,text= 'Sign In',command=UserPassCheck)
buttonlog.grid(row=6, column=1)

def OpenWindow():
    UI=Tk()
    UI.title('Printer UI')
    UI.geometry('1000x600')
    x=500
    void=Label(UI, text='                                                                                                         ') #To Centre the Button List. DO NOT EDIT EVER IF U DO KYS & CENTRE AGAIN
    void.grid(row=0,column=1)
    button1=Button(UI,text='Scratch Set 1')
    button1.grid(row=0, column=x)
    button2=Button(UI,text='Scratch Set 2')
    button2.grid(row=2, column=x)
    button3=Button(UI,text='SPER Set 1')
    button3.grid(row=4, column=x)
    button4=Button(UI,text='SPER Set 2')
    button4.grid(row=6, column=x)
    button5=Button(UI,text='SPER Set 3')
    button5.grid(row=8, column=x)
    button6=Button(UI,text='SPER Set 4')
    button6.grid(row=10, column=x)
    button7=Button(UI,text='SPER Set 5')
    button7.grid(row=12, column=x)
    button8=Button(UI,text='SPER Set 6')
    button8.grid(row=14, column=x)
    button9=Button(UI,text='GPIO Set 1')
    button9.grid(row=16, column=x)
    button10=Button(UI,text='GPIO Set 2')
    button10.grid(row=18, column=x)
    button11=Button(UI,text='GPIO Set 3')
    button11.grid(row=20, column=x)
    button12=Button(UI,text='GPIO Set 4')
    button12.grid(row=22, column=x)
    button13=Button(UI,text='GPIO Set 5')
    button13.grid(row=24, column=x)
    button14=Button(UI,text='GPIO Set 6')
    button14.grid(row=26, column=x)
    button15=Button(UI,text='Pygame Set 1')
    button15.grid(row=28, column=x)
    button16=Button(UI,text='Pygame Set 2')
    button16.grid(row=30, column=x)
    button17=Button(UI,text='Pygame Set 3')
    button17.grid(row=32, column=x)
    button18=Button(UI,text='Pygame Set 4')
    button18.grid(row=34, column=x)
    button19=Button(UI,text='Pygame Set 5')
    button19.grid(row=36, column=x)
    button20=Button(UI,text='Pygame Set 6')
    button20.grid(row=38, column=x)
    KYS=Button(UI,text='KYS, Logout', command=UI.destroy)
    KYS.grid(row=40, column=x)

'''def SignOut():
    UI.destroy()
    root=Tk()
    root.title("Secure Login")
    root.geometry('600x600')
    yw=Label(root, text='YoungWonks Secure Login')
    yw.grid(row=1, column=1)
    label=Label(root, text='Username')
    label.grid(row=2, column=1)
    label1=Label(root, text='Password')
    label1.grid(row=4, column=1)
    e1=Entry(root)
    e1.grid(row=2, column=2)
    e2=Entry(root, show='*')
    e2.grid(row=4, column=2)
    buttonlog=Button(root,text= 'Sign In',command=UserPassCheck)
    buttonlog.grid(row=6, column=1)'''



root.mainloop()
`

您可以使用platform模块检测操作系统。像这样:

import platform
print platform.system()
# Windows / Linux
if platform.system() == "Windows":
    # Do this
if platform.system() == "Linux":
    # Do this differently

您可以使用 command 参数为 tkinter.Button:

创建回调
 new_button = Button(master, text = "Some button", command = function)

对于Word-documents,你可以使用python-docx,你可以找到文档here. Printing the file indeed needs to be done differently on different platforms. You can find that answer here