我有一个简单的基于CLI的程序,我想将GUI添加到。最佳情况下,我也想保留通过CLI运行此脚本的能力。如果可以做到这一点,那么最好的方法是什么?免责声明:我对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 vdomn")
output_file.write("edit %sn" % str(args.vdom))
output_file.write("config firewall addressnn")
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 1n")
output_file.write("set subnet %s %sn" %
(str(ip_addr.network_address), str(ip_addr.netmask)))
output_file.write("nextnn")
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 %sn" % url)
output_file.write("set color 1n")
output_file.write("set type fqdnn")
output_file.write("set fqdn %sn" % url)
output_file.write("nextnn")
if __name__ == '__main__':
Main()
查看https://github.com/chriskiehl/gooey。这将自动将您的ArgParser参数转换为GUI。GUI将取决于代码,因此您程序的根仍取决于CLI。