当前位置: 首页 > 工具软件 > Gooey > 使用案例 >

python搭建GUI第三方库gooey入门

傅博瀚
2023-12-01

参考链接

控件

两个简单的控件:FileChooserDateChooser,分别提供了一个“文件选择器”和
“日期选择器”。现在支持的 chooser 类控件有:

控件名控件类型
FileChooser文件选择器
MultiFileChooser文件多选器
DirChooser目录选择器
MultiDirChooser目录多选器
FileSaver文件保存
DateChooser日期选择
TextField文本输入框
Dropdown下拉列表
Counter计数器
CheckBox复选框
RadioGroup单选框

配置

language 参数配置一样,Gooey 还支持很多其它配置,下面是它支持的参数列表:

参数简介
advanced切换显示全部设置还是仅仅是简化版本
show_configSkips the configuration all together and runs the program immediately
language指定从 gooey/languages 目录读取哪个语言包
program_nameGUI 窗口显示的程序名。默认会显 sys.argv[0]
program_descriptionSettings 窗口顶栏显示的描述性文字。默认值从 ArgumentParser 中获取。
default_size窗口默认大小。
required_cols设置必选参数行数。
optional_cols设置可选参数行数。
dump_build_config将设置以 JSON 格式保存在硬盘中以供编辑/重用。

布局流

布局实例可以在下载这个 exmaple 库体验

代码示例

  • 别的先不说,直接上代码
# coding: utf-8
import sys
import os
import time
import math
import numpy as np
import random
from ctypes import *
from time import sleep

from gooey import Gooey, GooeyParser


cmd_path = os.path.split(os.path.realpath(__file__))[0].replace("\\", "/")

embed_lib = cmd_path + '/img_embed_api.dll'
embed_sdk = CDLL(embed_lib)

img_ext_list = ['.jpg', '.png','.PNG', '.tiff' ,'.tif', '.JPG','.jpeg','.JPEG','.bmp','.BMP','.jfif']


def embedding(uuid, num_marco, source_path, dest_dir_path, log_path):
    embed_sdk.imgWatermarkEmbedApi.argtypes = [c_char_p, c_char_p, c_char_p, c_int]
    embed_sdk.imgWatermarkEmbedApi.restype = c_int

    f1 = open(log_path, "w")
    for item in os.listdir(source_path):
        full_item_path = os.path.join(source_path, item)
        ext_name = os.path.splitext(full_item_path)[1]
        if ext_name in img_ext_list:
            dest_path = os.path.join(dest_dir_path, "wm_"+item)
            
            start_s = time.time()
            arg1 = c_char_p(uuid.encode('utf-8'))
            arg2 = c_char_p(full_item_path.encode('gbk'))
            arg3 = c_char_p(dest_path.encode('gbk'))
            ret_value = embed_sdk.imgWatermarkEmbedApi(arg1, arg2, arg3, num_marco)
            end_s = time.time()
            print("\n return code is %d, cost time is %0.3ss\n"%(ret_value, end_s - start_s))
            sys.stdout.flush()
            sleep(0.1)
            f1.write('%s'%(full_item_path) + '\t'+ str(ret_value) + "\n")
    f1.close()


@Gooey(language='chinese',
       dump_build_config = False,
       program_name = u'嵌入测试工具(内部工具请勿外泄)',
       richtext_controls = True,
       required_cols = 2,
       optional_cols = 2,
       default_size=(760, 840),
       menu=[{
           'name': '菜单',
           'items': [{
               'type': 'AboutDialog',
               'menuTitle': '关于',
               'name': '水印嵌入及提取测试工具',
               'description': 'Copyright © xxxx技术有限公司',
               'version': 'v1.0.0',
               'website': 'https://www.xxxx.cn'
           }]
       }])
       
def main():
    # parser = GooeyParser(description="嵌入提取demo")
    default_dir = os.path.dirname(os.getcwd())

    parser = GooeyParser()
    parser.add_argument('appid', metavar='appid',default = "11110001", help="编码方案参数,用于提取阶段")
    parser.add_argument('Blocks', metavar='xxxx', type = int, default = 15, help="水印块参数")
    parser.add_argument('uuid', metavar='uuid', default = "xxxx", help="用于xxx阶段")

    parser.add_argument('emded_source_path', widget="DirChooser",  default = default_dir, help="待嵌入输入图片目录")
    parser.add_argument('emded_output_path', widget="DirChooser", default = default_dir, help="水印嵌入后输出目录")

    parser.add_argument("-Extract","--Extracting_whole", dest="full_Extract",\
             action="store_true", help="完整水印图片提取") 
    parser.add_argument("-Extract_Crop","--Extract_Croping", dest="crop_extract",\
             action="store_true", help="裁剪水印图片提取") 

    parser.add_argument('-extract_full_dir', widget = "DirChooser", default="../xxx",  help="完整水印图片的目录")
    parser.add_argument('-source_reference_path', widget = "FileChooser", default="./xxx.jpg",  help="原图片路径")

    parser.add_argument('-extract_full_img_path', widget = "FileChooser", default="../xxx.jpg",  help="无裁剪水印的图片路径")

    parser.add_argument('-test_crop_image_path', widget = "FileChooser", default="./xxx.jpg",  help="裁剪后待检测水印图路径")
    parser.add_argument('-test_crop_image_dir', widget = "DirChooser", default="test_crop_dir",  help="裁剪的待检测水印图的目录")
        
    args = parser.parse_args()

    if not os.path.exists(args.emded_output_path):
        os.makedirs(args.emded_output_path)

    if not args.full_Extract and not args.crop_extract:
        print("\n......start embedding ......\n")
        if not os.path.exists(args.emded_source_path):
            print("\n input embed dir %s not found\n"%args.emded_source_path)
            return 0

        log_path = os.path.join(args.emded_output_path, "log_embed.txt")
        embedding(args.uuid, args.Blocks, args.emded_source_path, args.emded_output_path, log_path)

        print("\n......embedding done......\n")
    

if __name__ == "__main__":
    sys.exit(main())

 类似资料: