再次来到社区!如何在Python 3上使用Tkinter打开LibreOffice或Word文件(在我的Mac上,但最终也是Windows)。我的UI有几个按钮,我想与特定文件关联,按下该按钮时,它应该将命令发送到Brother打印机并打印出该文件。当我在这里时,我还想知道是否有一个脚本来检测用户正在使用的操作系统,并使用Tkinter执行单独的命令(因为Mac和Windows有不同的要求)。我的代码如下。
注意:如果您查看“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()
`
答案 0 :(得分:0)
您可以使用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文档,您可以使用python-docx
,您可以找到文档here。确实需要在不同平台上以不同方式打印文件。您可以找到答案here。