zoukankan      html  css  js  c++  java
  • 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、运行后如下图:

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

    本示例中代码实测可用。

    幸福都是奋斗出来的,努力奋斗才能梦想成真。坚持自律,约束自我,克制弱点,坚持努力,遇见更好的自己。
  • 相关阅读:
    跨域解决方法
    css之line-height
    untiy项目中使用MD5加密
    unity给子物体添加Shader
    unity中UI坐标转3d世界坐标
    unity项目字符串转为Vector3和Quaternion
    unity中使用Highlighting System v4.0插件给物体添加高亮
    加载AssetBundle方法
    Lua面向对象----类、继承、多继承、单例的实现
    Lua学习笔记(一)-----C#和lua的交互
  • 原文地址:https://www.cnblogs.com/SH170706/p/10443947.html
Copyright © 2011-2022 走看看