• Python学习笔记_一个Tkinter示例,使用FileDialog


    为了使用Python进行数据分析,编写一个图形界面,选择一个Excel文件(或CSV),然后进行后续处理。

    一、本示例涵盖如下知识点:

    1、FileDialog的使用

    2、退出程序

    3、消息提示框的示例

    4、文本框赋值

    二、实验环境:

     1、OS:Win 10 64位

     2、Python 3.7

    三、进一步说明

    1、代码如下:

    from tkinter import *
    from tkinter import filedialog
    import tkinter.messagebox
    
    def main():
        def selectExcelfile():
            sfname = filedialog.askopenfilename(title='选择Excel文件', filetypes=[('Excel', '*.xlsx'), ('All Files', '*')])
            print(sfname)
            text1.insert(INSERT,sfname)
    
        def closeThisWindow():
            root.destroy()
    
        def doProcess():
            tkinter.messagebox.showinfo('提示','处理Excel文件的示例程序。')
        
        #初始化
        root=Tk()
    
        #设置窗体标题
        root.title('Python GUI Learning')
    
        #设置窗口大小和位置
        root.geometry('500x300+570+200')
    
    
        label1=Label(root,text='请选择文件:')
        text1=Entry(root,bg='white',width=45)
        button1=Button(root,text='浏览',width=8,command=selectExcelfile)
        button2=Button(root,text='处理',width=8,command=doProcess)
        button3=Button(root,text='退出',width=8,command=closeThisWindow)
     
    
        label1.pack()
        text1.pack()
        button1.pack()
        button2.pack()
        button3.pack() 
    
        label1.place(x=30,y=30)
        text1.place(x=100,y=30)
        button1.place(x=390,y=26)
        button2.place(x=160,y=80)
        button3.place(x=260,y=80)
     
        root.mainloop() 
    
     
    if __name__=="__main__":
        main()

    2、运行后如下图:

    其中,“浏览”,“退出”按钮的功能已编写,“处理”的功能,仅给出一个消息提示,真正使用,需在此处增加相应的功能。

    本示例中代码实测可用。

    幸福都是奋斗出来的,努力奋斗才能梦想成真。坚持自律,约束自我,克制弱点,坚持努力,遇见更好的自己。
  • 相关阅读:
    Navicat连接MySQL数据库的一些问题与解决方案
    从select机制谈到epoll机制
    关于VS2017提示I/O文件操作函数需要加上_s的解决办法
    LeetCode初级算法(树篇)
    LeetCode初级算法(动态规划+设计问题篇)
    LeetCode初级算法(其他篇)
    Leetcode初级算法(排序和搜索+数学篇)
    Leetcode初级算法(链表篇)
    Leetcode初级算法(字符串篇)
    机器学习之k-近邻算法
  • 原文地址:https://www.cnblogs.com/SH170706/p/10443947.html
Copyright © 2020-2023  润新知