Tkinter、wxPython 和 PyQT 是 Python 开发图形界面的三大利器。但他们使用起来都不是太方便。

    直到我遇到了 PySimpleGUI,我才开始觉得搞一个图形界面也是已经容易的事。

    比如说开发一个工具,检查两个文件是否相同,当然了要有图形界面。

    使用 PySimpleGUI 代码如下:

    1. import PySimpleGUI as sg
    2. layout = [
    3. [sg.Text('File 1'), sg.InputText(), sg.FileBrowse(),
    4. sg.Checkbox('MD5'), sg.Checkbox('SHA1')
    5. ],
    6. [sg.Text('File 2'), sg.InputText(), sg.FileBrowse(),
    7. sg.Checkbox('SHA256')
    8. ],
    9. [sg.Output(size=(88, 20))],
    10. [sg.Submit(), sg.Cancel()]
    11. ]
    12. window = sg.Window('File Compare', layout)
    13. while True: # The Event Loop
    14. event, values = window.read()
    15. # print(event, values) #debug
    16. if event in (None, 'Exit', 'Cancel'):
    17. break

    上述代码就会产生如下的用户界面:
    简单的方法开发一个图形界面 - 图1
    有了 UI,就可以很容易地了解如何插入其余的代码。我们只需要监控用户输入的内容,然后相应地采取行动。我们可以很容易地做到这一点,使用以下代码:

    1. import PySimpleGUI as sg
    2. import re
    3. import hashlib
    4. def hash(fname, algo):
    5. if algo == 'MD5':
    6. hash = hashlib.md5()
    7. elif algo == 'SHA1':
    8. hash = hashlib.sha1()
    9. elif algo == 'SHA256':
    10. hash = hashlib.sha256()
    11. with open(fname) as handle: #opening the file one line at a time for memory considerations
    12. for line in handle:
    13. hash.update(line.encode(encoding = 'utf-8'))
    14. return(hash.hexdigest())
    15. layout = [
    16. [sg.Text('File 1'), sg.InputText(), sg.FileBrowse(),
    17. sg.Checkbox('MD5'), sg.Checkbox('SHA1')
    18. ],
    19. [sg.Text('File 2'), sg.InputText(), sg.FileBrowse(),
    20. sg.Checkbox('SHA256')
    21. ],
    22. [sg.Output(size=(88, 20))],
    23. [sg.Submit(), sg.Cancel()]
    24. ]
    25. window = sg.Window('File Compare', layout)
    26. while True: # The Event Loop
    27. event, values = window.read()
    28. # print(event, values) #debug
    29. if event in (None, 'Exit', 'Cancel'):
    30. break
    31. if event == 'Submit':
    32. file1 = file2 = isitago = None
    33. # print(values[0],values[3])
    34. if values[0] and values[3]:
    35. file1 = re.findall('.+:\/.+\.+.', values[0])
    36. file2 = re.findall('.+:\/.+\.+.', values[3])
    37. isitago = 1
    38. if not file1 and file1 is not None:
    39. print('Error: File 1 path not valid.')
    40. isitago = 0
    41. elif not file2 and file2 is not None:
    42. print('Error: File 2 path not valid.')
    43. isitago = 0
    44. elif values[1] is not True and values[2] is not True and values[4] is not True:
    45. print('Error: Choose at least one type of Encryption Algorithm')
    46. elif isitago == 1:
    47. print('Info: Filepaths correctly defined.')
    48. algos = [] #algos to compare
    49. if values[1] == True: algos.append('MD5')
    50. if values[2] == True: algos.append('SHA1')
    51. if values[4] == True: algos.append('SHA256')
    52. filepaths = [] #files
    53. filepaths.append(values[0])
    54. filepaths.append(values[3])
    55. print('Info: File Comparison using:', algos)
    56. for algo in algos:
    57. print(algo, ':')
    58. print(filepaths[0], ':', hash(filepaths[0], algo))
    59. print(filepaths[1], ':', hash(filepaths[1], algo))
    60. if hash(filepaths[0],algo) == hash(filepaths[1],algo):
    61. print('Files match for ', algo)
    62. else:
    63. print('Files do NOT match for ', algo)
    64. else:
    65. print('Please choose 2 files.')
    66. window.close()

    运行结果如下:
    简单的方法开发一个图形界面 - 图2
    image.png
    最后的话:

    虽然不是最漂亮的 UI,但该库允许您快速启动简单的 Python UI,并与您可能需要的任何人共享。更重要的是,您需要执行此操作的代码简单且可读性强。您仍然会遇到必须运行代码才能获取 UI 的问题,这可能会使共享有点困难,但是您可以考虑使用 PyInstaller 之类的东西,它将您的 python 脚本转换为人们只需双击的 exe。

    作者:somenzz
    链接:https://www.jianshu.com/p/a37180c53a07
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。