0

Hello again to the Community! How can I open a LibreOffice or Word file (on my Mac, but eventually Windows as well) using Tkinter on Python 3. My UI has several buttons that I want to associate with a specific file, and when that button is pressed, it should send a command to a Brother printer and print that file out. While I am here, I'd also like to know whether there is a script to detect what OS the user is using and do separate commands (as Mac and Windows have different requirements) also using Tkinter. My code goes as follows.

Note: If you look at the function 'SignOut', it screws everything up. That is why it is commented.

`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()
`

1 Answers1

0

You can use the platform module to detect the operating system. Like so:

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

You could use the command argument to create a callback for a tkinter.Button:

 new_button = Button(master, text = "Some button", command = function)

For the Word-documents, you can use python-docx, you can find the documentation here. Printing the file indeed needs to be done differently on different platforms. You can find that answer here.

Community
  • 1
  • 1
RedFantom
  • 332
  • 1
  • 10