使用 Tkinter 将 Python argparse CLI 程序转换为 GUI? [英] Converting a Python argparse CLI program into a GUI with Tkinter?

查看:36
本文介绍了使用 Tkinter 将 Python argparse CLI 程序转换为 GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的基于 CLI 的程序,我想向它添加一个 GUI.最佳情况下,我希望保留通过 CLI 运行此脚本的能力.如果可以做到这一点,解决这个问题的最佳方法是什么?免责声明:我对 Tkinter 比较陌生!

I have a simple CLI based program that I would like to add a GUI to. Optimally I would like to retain the ability to have this script run via the CLI as well. If this can be done, what is the best way to approach this? Disclaimer: I am relatively new to Tkinter!

from argparse import ArgumentParser
from ipaddress import IPv4Network

def Main():
    """ Main Program """
    parser = ArgumentParser(
        description='Provided a list of IP addresses, format and output the correct fortigate commands to create them')
    parser.add_argument('VDOM', help='Specify a VDOM', type=str)
    parser.add_argument(
        'File', help='Specify a file.  Each entry should be on its own line, and have no extra characters', typ=str)
    args = parser.parse_args()

    with open(args.File, 'r') as input_file:
        array = input_file.read().splitlines()

    with open(args.vdom + '.txt', 'w') as output_file:
        output_file.write("config vdom\n")
        output_file.write("edit %s\n" % str(args.vdom))
        output_file.write("config firewall address\n\n")

        for i in range(0, len(array)):
            try:
                ip_addr = IPv4Network(array[i])
                generateip(ip_addr, output_file)
            except ValueError:
                url = array[i]
                generateurl(url, output_file)


def generateip(ip_addr, output_file):
    """
    Generate a single IP address object.

    ip_addr -- IP address network object
    output_file -- an output text file
    """
    output_file.write("edit \"%s\"\n" % str(ip_addr.with_prefixlen))
    output_file.write("set color 1\n")
    output_file.write("set subnet %s %s\n" %
                  (str(ip_addr.network_address), str(ip_addr.netmask)))
    output_file.write("next\n\n")


def generateurl(url, output_file):
    """
    Generate a single URL address object.

    url -- A valid URL string
    output_file -- an output text file
    """

    output_file.write("edit %s\n" % url)
    output_file.write("set color 1\n")
    output_file.write("set type fqdn\n")
    output_file.write("set fqdn %s\n" % url)
    output_file.write("next\n\n")


if __name__ == '__main__':
    Main()

推荐答案

查看 https://github.com/chriskiehl/粘糊糊的.这将自动将您的 ArgParser 参数转换为 GUI.GUI 将依赖于代码,因此您的程序的根目录仍然依赖于 CLI.

Check out https://github.com/chriskiehl/Gooey . This will automatically convert your ArgParser arguments to a GUI. The GUI will be dependent on the code, so the root of your program still depends on the CLI.

这篇关于使用 Tkinter 将 Python argparse CLI 程序转换为 GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆