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

查看:138
本文介绍了使用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/Gooey .这将自动将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天全站免登陆