python GUI tkinter实战

在这里插入图片描述
筛选出列长度不为指定长度的列

from os import path
from tkinter import (BOTH, BROWSE, EXTENDED, INSERT, Button, Frame, Label,
                     Text, Tk, filedialog, mainloop, messagebox)
import matplotlib.pyplot as plt
import pandas as pd
from PIL import Image, ImageTk
from tkinter import Entry, StringVar, messagebox, Tk, mainloop
import dataframe_image as dfi
class CleanCode():
    def __init__(self) -> None:

        self.root = Tk()
        self.root.title("显示需要清洗代码列")

        self.root.geometry(f"{500}x{300}")

        self.frame = Frame(self.root).grid(row=0, column=0, rowspan=4)

        self.old_pic_frame = Frame(self.root).grid(row=3, column=0)
        self.new_pic_frame = Frame(self.root).grid(row=3, column=1)
        self.width = 10
        self.col = StringVar()
        self.col_length = StringVar()
        btn_open = Button(self.frame, text="打开excel文件", width=self.width, height=1, command=self.open_xlsx_file).grid(row=0, column=0)  #

        Label(self.frame, text="输入清洗的列", height=1, width=self.width).grid(row=1, column=0)
        Entry(self.frame,  textvariable=self.col).grid(row=1, column=1)

        Label(self.frame, text="列的正确长度", height=1, width=self.width).grid(row=1, column=2)
        Entry(self.frame, textvariable=self.col_length).grid(row=1, column=3)

        btn_process = Button(self.frame, text="筛选", width=self.width, height=1, command=self.process_all,).grid(row=2, column= 3, padx = 15)
        btn_reload = Button(self.frame, text="重载该文件", width=self.width, height=1, command=self.re_load).grid(row=0, column= 3, padx = 15)

    def re_load(self):
        global df
        df = pd.read_excel(self.filepath, keep_default_na=False)
        new_img = self.df2image(df)
        Label(self.old_pic_frame, image=new_img).grid(row=3, column=0)
        messagebox.showinfo('重载', '重载文件成功')

    def df2image(self,df):
        df_style = df.iloc[:50,].style.background_gradient()
        table_filename = "./test.png"
        dfi.export(obj=df_style, filename=table_filename, fontsize=30)
        # 加载图像到Tkinter PhotoImage对象
        new_img = Image.open(table_filename).convert("RGB")
        # new_img = Image.open(fp=self.filepath).convert("RGB")
        self.img_width, self.img_height = new_img.size
        print(self.img_width, self.img_height)
        self.rate = self.img_width / self.img_height
        # 如果图片高度过高则进行缩小
        if self.img_height > self.screenheight * 0.5:
            width = int(0.5 * self.screenwidth)
            height = int(width / self.rate)
            new_img = ImageTk.PhotoImage(
                image=new_img.resize(size=(width, height)))
        else:
            new_img = ImageTk.PhotoImage(new_img)
        return new_img
    
    def open_xlsx_file(self):
        global df
        global new_img
        self.screenwidth = self.root.winfo_screenwidth()
        self.screenheight = self.root.winfo_screenheight()
        self.root.geometry(f"{self.screenwidth}x{self.screenheight}+0+0")
        self.filepath = filedialog.askopenfilename(title='选择文件', filetypes=[('文件', ['*.xlsx', '*.xls', '*.csv'])])
        # messagebox.showinfo('已经选中文件', self.filepath)
        df = pd.read_excel(self.filepath, keep_default_na=False)
        new_img = self.df2image(df)
        Label(self.old_pic_frame, image=new_img).grid(row=3, column=0)

    def process_all(self):
        global df
        number_mode = r'(\d+)'
        normal_mode = r'([0-9a-zA-Z]+)'
        col_length_str =  self.col_length.get()
        col_length_list =  col_length_str.split(',')
        col = self.col.get()
        df['code_length'] = df.loc[:, col].astype(str).str.extract(normal_mode)
        df['code_length'].fillna( '',inplace=True)
        df['code_length'] = df['code_length'] .str.len()
        df['code_length'] = df['code_length'].astype(str)
        doubt_df =df[~df['code_length'].isin(col_length_list)]
        new_img = self.df2image(doubt_df)
        Label(self.new_pic_frame, image=new_img).grid(row=3, column=1)


if __name__ == "__main__":
    main = CleanCode()
    df = None
    new_img = None
    mainloop()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python GUI项目实战通常涉及创建图形用户界面(GUI),以便用户可以直观地与程序进行交互。在一个示例项目中,我们可以使用PythonGUI库,如Tkinter或PyQt,来创建一个简单的登录和主窗体。在这个项目中,我们可以使用上述引用中提到的代码片段来加载主窗体和传递参数。 首先,我们需要导入所需的库,例如Tkinter或PyQt。然后,我们可以创建一个登录窗体,其中包含用户名和密码的文本框以及登录按钮。当用户点击登录按钮时,我们可以对用户名和密码进行验证,并在验证通过后加载主窗体。 在登录验证成功后,我们可以使用上述引用中提到的代码片段来加载主窗体,并将用户信息和当前时间作为参数传递进去。这样,主窗体就可以根据这些参数显示相应的内容。 主窗体的设计和功能实现可以根据具体需求进行定制。例如,可以添加各种按钮、标签、文本框等组件,并为它们设置相应的事件处理程序。这样,用户可以通过与这些组件的交互来执行不同的操作。 总之,Python GUI项目实战涉及创建图形用户界面,并通过用户输入和交互实现各种功能。可以使用Tkinter或PyQt等库来创建窗体和组件,并使用相应的代码来加载主窗体并传递参数。具体的设计和功能实现取决于项目需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Python GUI项目实战(二)主窗体的界面设计与实现](https://blog.csdn.net/weixin_32501329/article/details/113498872)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Python GUI项目实战:主窗体的界面设计与实现](https://blog.csdn.net/m0_48405781/article/details/107886396)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值