Tkinter로 만드는 스마트카드 통신 프로그램 - 2

2017. 7. 26. 17:19IT관련

반응형

이전글: Tkinter로 만드는 스마트카드 통신 프로그램 - 1




목표

1. APDU 명령어를 직접 보내고 응답 받는 것을 ScrolledText에 출력


특이사항

1. "Send" 버튼에 연결된 callback 함수에 parameter를 넘겨주기 위해서 lambda 형식을 이용


소스코드

# imports
import tkinter as tk
import tkinter.scrolledtext as tkst
from tkinter import Menu
from tkinter import ttk
from tkinter import messagebox

from smartcard.System import readers
from smartcard.util import toHexString, toBytes


# Click a exit menu
def clickExit():
    win.quit()
    win.destroy()
    exit()

# Click a abount menu
def clickAbout():
    messagebox.showinfo('About', 'About...')

# Click a reset button
def clickReset():
    try:
        for r in readers():
            if r.name == reader_name.get():
                connection = r.createConnection()
                connection.connect()
                stLog.insert(tk.INSERT, 'ATR: ' + toHexString(connection.getATR()) + '\n')
                stLog.see(tk.END)
                return connection
    except:
        messagebox.showinfo('Error', 'Please check a card or readers...')
        return

# Click a send button
def clickSend(connection):
    apdu = toBytes(entryCommand.get())
    response, sw1, sw2 = connection.transmit(apdu)
    #print('response: ', response, ' status words: ', "%x %x" % (sw1, sw2))
    capdu = '< ' + toHexString(apdu) + '\n'
    rapdu = '> ' + toHexString(response) + '\n> {:02X}'.format(sw1) + ' {:02X}'.format(sw2) +'\n'   
    stLog.insert(tk.INSERT, capdu + rapdu)
stLog.see(tk.END)


if __name__ == '__main__':
    connection = None
    win = tk.Tk()                   # Create instance
    win.title('Manager')            # Add a title

    labelReader = ttk.Label(win, text='Reader')
    labelReader.grid(column=0, row=0)

    reader_name = tk.StringVar() # String variable
    comboReader = ttk.Combobox(win, width=30, state='readonly', textvariable=reader_name) # Create a combobox
    comboReader.grid(column=1, row=0)
    comboReader['values'] = readers()
    try:
        comboReader.current(0)
    except:
        pass

    buttonReset = ttk.Button(win, text='Reset...', command=clickReset)  # Create a button
    buttonReset.grid(column=2, row=0)

    stLog = tkst.ScrolledText(win, width=50, height=20, wrap=tk.WORD)   # Create a scrolledtext
    stLog.grid(column=0, row=1, columnspan=3)

    entryCommand = tk.Entry(win, width=40)                              # Create a entry
    entryCommand.grid(column=0, row=2, columnspan=2)
    entryCommand.focus_set()

    buttonCommand = ttk.Button(win, text='Send...', command=lambda: clickSend(connection))  # Create a button
    buttonCommand.grid(column=2, row=2)

    menuBar = Menu(win)                                     # Create a menu
    win.config(menu=menuBar)

    fileMenu = Menu(menuBar, tearoff=0)                     # Create the File Menu
    fileMenu.add_command(label="Exit", command=clickExit)   # Add the "Exit" menu and bind a function
    menuBar.add_cascade(label="File", menu=fileMenu)

    helpMenu = Menu(menuBar, tearoff=0)
    helpMenu.add_command(label="About", command=clickAbout) # Add the "About" menu and bind a function
    menuBar.add_cascade(label="Help", menu=helpMenu)

    connection = clickReset()
    
    win.resizable(0, 0)             # Disable resizing the GUI
    win.mainloop()                  # Start GUI


반응형