当前位置:网站首页>Practical cases | getting started and mastering tkinter+pyinstaller

Practical cases | getting started and mastering tkinter+pyinstaller

2022-06-26 05:07:00 JERRY__ JIN

Practical cases | Getting started Tkinter+Pyinstaller

Made a Tkinter+Pyinstaller A small example of linkage , Record the experimental problems here

 Insert picture description here

One 、Tkinter

visualization GUI

1. Install the parts :

python It comes with its own

2. Basic course :

Common modules

https://www.runoob.com/python/python-gui-tkinter.html

Emphasis on the :filedialog yes tkinter File dialog box in

 Insert picture description here

  • Use :
    • The import module :import tkinter.filedialog
    • Select the format of the file dialog box :
      • tkinter.filedialog.asksaveasfilename(): Choose what file name to save , Return the filename
      • tkinter.filedialog.asksaveasfile(): Choose what file to save , Create a file and return the file stream object
      • tkinter.filedialog.askopenfilename(): Choose what file to open , Return the filename
      • tkinter.filedialog.askopenfile(): Choose what file to open , return IO Stream object
      • tkinter.filedialog.askdirectory(): Choose a directory , Return directory name
      • tkinter.filedialog.askopenfilenames(): Select to open multiple files , Returns multiple file names as tuples
      • tkinter.filedialog.askopenfiles(): Select to open multiple files , Returns multiple items as a list IO Stream object

import tkinter.filedialog as filedialog
#  Return the filename 
filenames = filedialog.askopenfilenames()

In this case , There are some deficiencies , That is, the front end and the back end are not separated ( Because it won't ...) Just write it together

3. Complete code :

from tkinter import *
import tkinter.filedialog as filedialog
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
# Show all columns 
pd.set_option('display.max_columns', None)

# --- Logic implementation ---
def data_range(require_data):
    if '±' in require_data:
        print(" Scope of requirements :", require_data)
        base = require_data.split('±')[0]
        volatility = require_data.split('±')[1]
        base_up = float(base) + float(volatility)
        base_down = float(base) - float(volatility)
        return base_up, base_down

def read_excel(path):
    data_xls = pd.ExcelFile(path)
    # sheet The name of the table 
    print(data_xls.sheet_names)
    data = {
    }
    for name in data_xls.sheet_names:
        df = data_xls.parse(sheetname=name)
        data[name] = df
        # print(df)
        # print(name)
    return data

def get_select_dataset(sheet_path, require_path,save_path):
    dataset = read_excel(sheet_path)
    select_dfs = []
    with open(require_path, 'r', encoding='utf-8') as f:
        res_ls = f.readlines()
        for i, res in enumerate(res_ls):
            sheet_name = res.split(' ')[0]
            require_data = res.split(' ')[1].split('\n')[0]
            df = dataset[sheet_name]
            #  Table to be filtered 
            print(f' Serial number :{
      i+1} \t Table to be filtered :{
      sheet_name}')
            #  Scope of requirements 
            base_up, base_down = data_range(require_data)
            print(df)
            select_df = df[(df['Retention time (min)'] >= base_down) & (df['Retention time (min)'] <= base_up)]
            #  Filtered data 
            print(' Filtered data :')
            print(select_df)
            select_df['sheet name'] = sheet_name
            select_dfs.append(select_df)
    final_df = pd.concat(select_dfs)
    final_df.to_csv(save_path,index=False)

    s = f' Filtering complete , Please check the results :{
      save_path}'
    txt.insert(END, s)  #  Additional display of operation results 

# ------ Window implementation ----------------

def xz1():
    filenames = filedialog.askopenfilenames()

    if len(filenames) != 0:
        string_filename =filenames[0]
        txt1.insert(END,string_filename)
    else:
        txt1.insert(END," You have not selected any files ")

def xz2():
    filenames = filedialog.askopenfilenames()
    if len(filenames) != 0:
        string_filename = filenames[0]
        txt2.insert(END,string_filename)
    else:
        txt2.insert(END," You have not selected any files ")

root = Tk()
root.geometry('700x540')
root.title(' Table condition filter _v1 ( Special for sister min )')

lb1 = Label(root, text=' Select the table file ')
lb1.place(relx=0.1, rely=0.1, relwidth=0.2, relheight=0.1)
lb2 = Label(root, text=' Select requirements document ')
lb2.place(relx=0.4, rely=0.1, relwidth=0.2, relheight=0.1)
lb3 = Label(root, text=' Fill in the save file name (.csv  ending )')
lb3.place(relx=0.7, rely=0.1, relwidth=0.2, relheight=0.1)
txt1 = Text(root)
txt1.place(relx=0.1, rely=0.2, relwidth=0.2, relheight=0.1)
txt2 = Text(root)
txt2.place(relx=0.4, rely=0.2, relwidth=0.2, relheight=0.1)

input1 = Entry(root)
input1.place(relx=0.7, rely=0.2, relwidth=0.2, relheight=0.1)
btn1 = Button(root,text=" Select the table to filter ",command=xz1)
btn1.place(relx=0.1, rely=0.4,relwidth=0.2,relheight=0.05)
btn2 = Button(root,text=" Select requirements document ",command=xz2)
btn2.place(relx=0.4, rely=0.4,relwidth=0.2,relheight=0.05)

#  Screening rules 
btn3 = Button(root, text=' Start the calculation ', command=lambda: get_select_dataset(txt1.get('0.0','end').split('\n')[0],txt2.get('0.0','end').split('\n')[0],input1.get()))
btn3.place(relx=0.7, rely=0.4, relwidth=0.2,relheight=0.05)
#  Prompt box 
lb = Label(root, text=' Prompt box ')
lb.place(relx=0.1, rely=0.5, relwidth=0.2, relheight=0.1)
#  In the vertical top-down position of the form 60% From the beginning , Relative height of the form 40% High text box 
txt = Text(root)
txt.place(relx=0.1,rely=0.6, relwidth=0.8,relheight=0.3)

root.mainloop()

Two 、Pyinstaller

Pyinstaller pack Python Program , There are many answers to questions on the Internet , I'm so angry !

1. Install the parts :

pip install pyinstaller

Possible problems :

Installed pyinstall Tips ‘pyinstall’ Not an internal or external command , It's not a runnable program Or batch file solutions

Never mind , Give it a try pyinstaller

Now generate exe My command is pyinstaller, instead of pyinstall !!! Be careful !!!

2. Generate ico Format

Generate ico

3. Command line

Basic commands :

#  Be careful !!!  yes  pyinstaller
pyinstaller -F -i  picture .ico  file .py

Hide the console

#  Be careful !!!  yes  pyinstaller
pyinstaller -F -w -i  picture .ico  file .py

Generated exe stay dist In the folder

原网站

版权声明
本文为[JERRY__ JIN]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180507506563.html