Python中CLI脚本中的选项不起作用

时间:2015-12-06 09:45:51

标签: python python-3.x tkinter command-line-interface

我编写了以下命令行程序,以便提供一种自动方式来测试我的主脚本 - 一个gui应用程序,在一个模块(gui.py)中实现,我在CLI中导入"测试套件&#34 ;.

import argparse
from data import Document
from gui import View

parser = argparse.ArgumentParser()
parser.add_argument("-f", type=str, help="Path of the JSON file to be used.")
parser.add_argument("-u", type=str, help="User UUID.")
parser.add_argument("-d", type=str, help="Document UUID.")
parser.add_argument("-t", type=int, help="Task ID. ID's range from 2 to 5, as per coursework instructions.")

args = parser.parse_args()

doc = Document(args.f)
view = View()

if args.t == 1:
    view.display_views_by_country(doc, args.d)
elif args.t == 2:
    view.display_views_by_cnt(doc, args.d)
elif args.t == 3:
    view.display_browser_hist(doc)
elif args.t == 4:
    view.top10_window(doc)
elif args.t == 5:
    view.also_likes_most_viewers_window(doc, args.u, args.d)
elif args.t == 6:
    view.also_likes_timeread_window(doc, args.u, args.d)

对于-t参数,1到3将产生所需的输出,这是每个任务id的不同图形,通过pandas和matplot库实现。但是当我提供必要的参数(例如-t = 4的文件路径)以及其余可用选项(4到6)的-t选项时,没有任何效果。应该发生的是在新窗口上显示列表。 下面是我要导入的View类的代码以及应该实现所需任务的方法。

class View(tk.Frame):

    def __init__(self, *args, **kwargs):
        """Constructor of the Frame widget, representing the initial window.
        Main objective is to add the widgets through which the user will initialize
        the application by entering the path of the json file to be processed.

        :param args: Parent widget
        :param kwargs: Config options
        :return: None
        """
        tk.Frame.__init__(self)

        self.master.title("Document Analytics")

        self.master.rowconfigure(0, weight=1)
        self.master.columnconfigure(0, weight=1)
        self.grid(sticky=W+E+N+S)

        self.text_filepath = tk.Text(self, width=45, height=1)
        self.text_filepath.grid(sticky=W+E+N+S)
        self.text_filepath.insert(tk.INSERT, "Insert the path of the file to be processed")
        self.text_filepath.config(state=tk.DISABLED)

        self.entry_filepath = tk.Entry(self, width=20)
        self.entry_filepath.grid(sticky=W+E+N+S, row=0, column=1, columnspan=3)

        self.button_filepath = tk.Button(self, text="Initialize", command=self.new_window, width=25)
        self.button_filepath.grid(row=1, column=1, sticky=W+E+N+S)

        self.rowconfigure(1, weight=1)
        self.columnconfigure(1, weight=1)

    def top10_window(self, data):
        """Method which displays a list of the top 10 readers
        based on reading time.

        :param data: Reference to a Document object.
        :return: None
        """
        window = tk.Toplevel(self)

        list_box = tk.Listbox(window)
        list_box.pack()

        for item in data.top10_readers():
            list_box.insert(tk.END, item)

    def also_likes_most_viewers_window(self, data, visitor_id, doc_id):
        """Method which implements the 'also likes' functionality.
        Creates a new window which displays a list of top 10 relevant documents.
        Sorting is based on number of viewers of each document document.

        :param data: Reference to a Document object.
        :param visitor_id: The visitor UUID.
        :param doc_id: The document UUID.
        :return: None
        """
        window = tk.Toplevel(self)

        list_box = tk.Listbox(window)
        list_box.pack()

        for item in data.also_likes_most_viewers(visitor_id, doc_id):
            list_box.insert(tk.END, item)

    def also_likes_timeread_window(self, data, visitor_id, doc_id):
        """Method which implements the 'also likes' functionality.
        Creates a new window which displays a list of top 10 relevant documents.
        Sorting is based on reading time spent on each document.

        :param data: Reference to a Document object.
        :param visitor_id: The visitor UUID.
        :param doc_id: The document UUID.
        :return: None
        """
        window = tk.Toplevel(self)

        list_box = tk.Listbox(window)
        list_box.pack()

        for item in data.also_likes_reader_profile(visitor_id, doc_id):
            list_box.insert(tk.END, item)

我还应该提到这些方法通过GUI按预期工作。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

你需要调用tkinter mainloop才能让windows显示操作。