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

2017. 7. 26. 15:01IT관련

반응형

Python으로 스마트카드 통신 프로그램을 작성해 본다.

이를 위해서는 pyscard가 설치되어 있어야 한다.

pyscard 설치 방법은 "Windows에 pyscard 1.9.5 설치" 포스팅을 참고하기 바란다.


초기 화면


ATR



목표

1. Combobox에 PC/SC 리더기 목록을 표시

2. "Reset" 버튼을 누르면 리셋 후 ATR을 표시



소스코드

# imports
import tkinter as tk
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()
                messagebox.showinfo('ATR', toHexString(connection.getATR()))
                break
    except:
        messagebox.showinfo('Error', 'Please check a card or readers...')



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

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

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

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

    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 "Exit" menu and bind a function
    menuBar.add_cascade(label="Help", menu=helpMenu)

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


반응형