使用python gooey如何在点击多个按钮

时间:2018-02-13 12:37:21

标签: python user-interface

您好我有以下代码,它基于wxpython-gooey正常运行python GUI。但是我的任务是创建一个带有多个按钮的主GUI,在单击任何按钮后,相应的GUI应该打开并处理相应的任务并关闭。我的代码不适用于主GUI,但在主GUI上点击按钮后,下面代码的GUI必须打开,子GUI代码如下

from __future__ import print_function
import pandas as pd
import numpy as np
import glob
import os
import json
from argparse import ArgumentParser
from gooey import Gooey, GooeyParser


@Gooey(program_name="Jio Project")

def parse_args():
    """ Use GooeyParser to build up the arguments we will use in our script
    Save the arguments in a default json file so that we can retrieve them
    every time we run the script.
    """
    stored_args = {}
    # get the script name without the extension & use it to build up
    # the json filename
    script_name = os.path.splitext(os.path.basename(__file__))[0]
    args_file = "{}-args.json".format(script_name)
    # Read in the prior arguments as a dictionary
    if os.path.isfile(args_file):
        with open(args_file) as data_file:
            stored_args = json.load(data_file)
    parser = GooeyParser(description='Qualcomm-Samsung a Jio project')
    parser.add_argument('data_directory',
                        action='store',
                        default=stored_args.get('data_directory'),
                        widget='DirChooser',
                        help="Source directory that contains Excel files")
    parser.add_argument('output_directory',
                        action='store',
                        widget='DirChooser',
                        default=stored_args.get('output_directory'),
                        help="Output directory to save summary report")
    parser.add_argument('cust_file',
                        action='store',
                        default=stored_args.get('cust_file'),
                        widget='FileChooser',
                        help='Customer Account Status File')
    parser.add_argument('-d', help='Start date to include',
                        default=stored_args.get('d'),
                        widget='DateChooser')
    args = parser.parse_args()
    # Store the values of the arguments so we have them next time we run
    with open(args_file, 'w') as data_file:
        # Using vars(args) returns the data as a dictionary
        json.dump(vars(args), data_file)
    return args


def combine_files(src_directory):
    """ Read in all of the sales xlsx files and combine into 1
    combined DataFrame
    """
    all_data = pd.DataFrame()
    for f in glob.glob(os.path.join(src_directory, "sales-*.xlsx")):
        df = pd.read_excel(f)
        all_data = all_data.append(df, ignore_index=True)
    all_data['date'] = pd.to_datetime(all_data['date'])
    return all_data


def add_customer_status(sales_data, customer_file):
    """ Read in the customer file and combine with the sales data
    Return the customer with their status as an ordered category
    """
    df = pd.read_excel(customer_file)
    all_data = pd.merge(sales_data, df, how='left')
    # Default everyone to bronze if no data included
    all_data['status'].fillna('bronze', inplace=True)
    # Convert the status to a category and order it
    all_data["status"] = all_data["status"].astype("category")
    all_data["status"].cat.set_categories(["gold", "silver", "bronze"], inplace=True)
    return all_data


def save_results(sales_data, output):
    """ Perform a summary of the data and save the data as an excel file
    """
    summarized_sales = sales_data.groupby(["status"])["unit price"].agg([np.mean])
    output_file = os.path.join(output, "sales-report.xlsx")
    writer = pd.ExcelWriter(output_file, engine='xlsxwriter')
    summarized_sales = summarized_sales.reset_index()
    summarized_sales.to_excel(writer)



if __name__ == '__main__':
   # maingui=main_args()
    conf = parse_args()
    print("Reading sales files")
    sales_df = combine_files(conf.data_directory)
    print("Reading customer data and combining with sales")
    customer_status_sales = add_customer_status(sales_df, conf.cust_file)
    print("Saving sales and customer summary data")
    save_results(customer_status_sales, conf.output_directory)
    print("Done")

1 个答案:

答案 0 :(得分:1)

为了创建一个真正的自定义交互式gui,我建议你通过这里提到的框架变得更加熟悉:https://wiki.python.org/moin/GuiProgramming

但是为了回答具体问题,Gooey有一些小缺点,这使得很难在原始窗口内跨越一个新的Gooey窗口(我不会在这里详细介绍),但你可以做的是,产生一个新窗口作为子进程。下面的最小例子:

import sys
import os
from argparse import ArgumentParser

from subprocess import Popen, PIPE

from gooey import Gooey
from gooey import GooeyParser

@Gooey()
def main():

    parser = GooeyParser(
        description='''Test''')

    required = parser.add_argument_group('Optional arguments')

    parser.add_argument(
                        '-i', '--input',
                        required=False,
                        dest = 'input',
                        help='Test_file',
                        widget='FileChooser',
                        )

    args = parser.parse_args()

    ########################################
    # call the next gooey as subprocess from here 
    # should work on any system
    ########################################

    PYTHON_PATH = sys.executable
    process = Popen([PYTHON_PATH, 'spawn_next.py'], stdout=PIPE, stderr=PIPE)
    output, error = process.communicate()
    print(output)
    print(error)


if __name__ == "__main__":
    main()

然后你可以调用spawn_next.py文件,你可以在其中打开另一个Goopy窗口,如:

from gooey import Gooey
from gooey import GooeyParser

@Gooey()
def main():

    parser = GooeyParser(
        description='''Test''')

    required = parser.add_argument_group('Optional arguments')

    parser.add_argument(
                        '-i', '--input',
                        required=False,
                        dest = 'input',
                        help='Test_file',
                        widget='FileChooser',
                        )

    args = parser.parse_args()

main()