PySimpleGui制作程序

最近父母老是忘记谁送过东西,在什么时候,我就想了一下,做了个项目

from hmac import trans_36
from os import read
from re import A
from turtle import tracer
from xml.etree.ElementPath import find
import xlrd
from xlutils.copy import copy
import PySimpleGUI as sg
#这是我写的函数所用的库,大致一样
from ast import excepthandler
from email.policy import default
from faulthandler import disable
from turtle import width
from typing import List
import PySimpleGUI as sg
from xlutils.copy import copy
import init         #导入附加函数维持程序运行
import datetime
#主程序的

导入库后就继续干

这个程序用的PysimpleGui的库,好看方便实用!

主要程序的代码,还有附加的

sg.theme('Tan')#这里的话主题随你
con = []

goin_layout = [
    [sg.Text('欢迎来到清单查询系统!')],
    [sg.Button('请点我后开始使用!!!',size=30,button_color='red',disabled=False,key='-Updater-')],
    [sg.Radio('物品名', "RADIO1", default=True,key='-Thing-'),sg.Radio('赠送人', "RADIO1",key='-People-'),sg.Radio('日期', "RADIO1",key='-Time-')],
    [sg.Input(key='-Input-',size=40)],
    [sg.Button('查询',key='-Findout-',size=(10,1)),sg.Button('新建',key='-New-',size=(10,1)),sg.Button('保存为excel',key='-Save-',size=(10,1))],
    [sg.Button('看不全?选中该条后点我!',size=35,key='-OkLook-')],
    [sg.Listbox(values=['物品名             赠与人              日期'],size=(38,32),key='-List-')]
]   #自己看着需要来该

window = sg.Window('清单查询系统',layout=goin_layout)
win2_active = False#这里时4个窗口的
win3_active = False
win4_active = False

while True:

    event, values = window.read()
    if event in (None, 'Exit'):
        break
    if event == '-Updater-':
        a = init.updater('D:\\2.txt')#目标可以改
        window['-List-'].update(a)
        sg.popup('同步成功,可以使用了')

    if event == '-OkLook-':
        try:
            a = format(values['-List-'][0]).split()
            sg.popup('物品:   '+a[0]+'\n'+'赠与人:   '+a[1]+'\n'+'日期:   '+a[2])
        except:
            sg.popup('查看失败!!')
    
    if event == '-Save-':
        win4_active = True
        init.save()
    if event == '-Findout-':
        findoutvalue = values['-Input-']
        findthing = []
        if values['-Thing-']:
            findthing = init.find_thing('D:\\2.txt',findoutvalue)#这里也是可以改,有地址的都可以改成正确的
            init.look(findthing)
        if values['-People-']:
            findthing = init.find_people('D:\\2.txt',findoutvalue)
            init.look(findthing)
        if values['-Time-']:
            findthing = init.find_time('D:\\2.txt',findoutvalue)
            init.look(findthing)
    if event == '-New-':
        win2_active = True

        layout2 = [                                         #定义新建窗口
            [sg.Text('新建')],
            [sg.Input('物品名',size=25,key='-Thing-')],
            [sg.Input('赠与人',size=25,key='-People-')],
            [sg.Input('日期',size=25,key='-Time-'),sg.Button('当前日期',key='-TimeGet-')],
            [sg.Submit('确认新建',key='-Ok-',size=25)]
                   ]
        win2 = sg.Window('新建', layout2)
        while True:
            event2, value2 = win2.read()
            if event2 in(None, 'Exit'):
                win2.close()
                win2_active = False
                break
            if event2 == '-TimeGet-':
                today = datetime.date.today() #获取当前日期
                win2['-Time-'].update(today)
            if event2 == '-Ok-':
                span1 = 10
                span2 = 10
                thing_name = value2['-Thing-'] 
                people_name = value2['-People-']
                times_get = value2['-Time-']
                a = thing_name + ' ' +people_name + ' ' +times_get
                init.writefile('D:\\2.txt',a)
                sg.popup('新建成功!')
                things = thing_name + ' '*span1
                peoples = people_name + ' '*span2
                a = things+peoples+times_get
                c = init.readfile('D:\\2.txt')
                c.append(a)
                window['-List-'].update(c)
                a = init.updater('D:\\2.txt')
                window['-List-'].update(a)
                win2.close()
                win2_active=False
                break

附加的

'''
          地址自己看着改,别改错了
            这个保存在另一个文档中,要和主程序同级保存在一个文件夹
            文件命名为“init”,不然会出错!!
            
'''
def excelwrite(filename,value):         
    workbook = xlrd.open_workbook(filename) 
    sheet = workbook.sheet_by_index(0)  
    rowNum = sheet.nrows    
    colNum = sheet.ncols    
    newbook = copy(workbook)    
    newsheet = newbook.get_sheet(0) # 在末尾增加新行
    for i in range(0,len(value)):
        newsheet.write(rowNum, i, value[i]) # 覆盖保存  
    newbook.save(filename)

def readfile(filename):
    l = ''
    x = []
    with open(filename,'r',encoding='utf-8') as f:
        for i in f:
            for j in i.rsplit():
                l += j
                l += '          '
            l = l.rstrip()
            x.append(l)
            l = ''
        print(x)
        return x


def writefile(filename,value):
    with open(filename,'a',encoding='utf-8') as fl:
        fl.write(value + '\n')

def updater(filename):
    r = readfile(filename)
    return r

def find_thing(filename,value):
    find_out = []
    cons = 0
    with open(filename,'r',encoding='utf-8') as f:
        for i in f:
            if value in i.rsplit()[0]:
                cons += 1
                find_out.append(i.rsplit())
    sg.popup('已查询到'+ str(cons) + '条记录')
    return find_out

def find_people(filename,value):
    find_out = []
    cons = 0
    with open(filename,'r',encoding='utf-8') as f:
        for i in f:
            if value in i.rsplit()[1]:
                cons += 1
                find_out.append(i.rsplit())
    sg.popup('已查询到'+ str(cons) + '条记录')
    return find_out
def find_time(filename,value):
    find_out = []
    cons = 0
    with open(filename,'r',encoding='utf-8') as f:
        for i in f:
            if value in i.rsplit()[2]:
                cons += 1
                find_out.append(i.rsplit())
    sg.popup('已查询到'+ str(cons) + '条记录')
    return find_out

def look(findthing):
    win3_active = True
    layout3 = [                                         #定义新建窗口
        [sg.Button('查看',key='-Look-')],
        [sg.Listbox(values=[''],size=(35,10),key='-List1-')]
               ]
    win3 = sg.Window('查看', layout3)
    while True:
        event3, value3 = win3.read()
        if event3 in(None, 'Exit'):
            win3.close()
            win3_active = False
            break
        if event3 == '-Look-':
            print(findthing)
            win3['-List1-'].update(findthing)  
            findthing = []      

def save():
    win4_active = True
    layout4 = [                                         #定义新建窗口
        [sg.Text('保存为excel表格')],
        [sg.Input(size=15,key='-Input2-'),sg.FileBrowse(key='-FileNeed-')],
        [sg.Button('确认保存',size=10,key='-A-')]

               ]
    win4 = sg.Window('保存', layout4)
    while True:
        event4, value4 = win4.read()
        if event4 in(None, 'Exit'):
            win4.close()
            win4_active = False
            break
        if event4 =='-A-':
            try:
                l = ''
                x = []
                with open('D:\\2.txt','r',encoding='utf-8') as f:
                    for i in f:
                        for j in i.rsplit():
                            l += j
                            l += ' '
                        l = l.rstrip()
                        x.append(l)
                        l = ''
                print(x)
                for i in x:
                    a = i.split()

                    excelwrite(value4['-Input2-'],a)
            except:
                sg.popup('失败!')
            else:
                sg.popup('保存成功','请在'+value4['-Input2-']+'查看')
                win4.close()
                win4_active = False
                break

最后,如果都没错,那就该是这样:

PySimpleGui制作程序

 

PySimpleGui制作程序

 

PySimpleGui制作程序

好了,就这样,如果还是不行,直接去看程序下载吧,在下个博客中 

上一篇:C#添加程序执行时长监控日志


下一篇:C#查找指定路径下的所有指定文件,并读取