zoukankan      html  css  js  c++  java
  • Python2.X和Python3.X中Tkinter模块的文件对话框、下拉列表的不同

    Python2.X和Python3.X文件对话框、下拉列表的不同  

    今天初次使用Python Tkinter来做了个简单的记事本程序。发现Python2.x和Python3.x的Tkinter模块的好多内置函数都有所改变,这里简单整理一下以备日后查验。

    一.导入方式:

      Python2.x:

        from Tkinter import *

      Python3.x:

        from tkinter import *

    二.打开文件框:

      Python2.X:

        import tkFileDialog

        filename = tkFileDialog.askopenfilename(filetypes=[("bmp格式".decode('gbk'),"bmp")])

        #注意:Python2.X会有中文乱码问题,需要在"中文"后加.decode('gbk') 。Python3.X则不需要

        这里可以加入属性: initialdir 设置默认初始路径。即:

        FileName = tkFileDialog.askopenfilename(filetypes=[("bmp格式".decode('gbk'),"bmp")], initialdir = 'E:')

      Python3.X:

        import tkinter.filedialog

        filename=tkinter.filedialog.askopenfilename(filetypes=[("bmp格式","bmp")])

    三.对话框:

      Python2.X:

        import tkFileDialog

        showinfo(title='中文标题'.decode('gbk'), message='XXX') #注意:中文要加.decode('gbk')

      Python3.X:

        import tkinter.messagebox

        tkinter.messagebox.showinfo(title='XXX',message='XXX')

    四.下拉列表:

      Python2.X:

        import ttk

        #注意:如果写from ttk import * 会影响Label的属性,这里可能Label会自动调用ttk里的Label?猜测而已

      Python3.X:

        from tkinter import ttk

        用法一样:

          myComboList = ['AAA','BBB',]

          myCombox = ttk.Combobox(root, values=myComboList )

          myCombox .pack()

  • 相关阅读:
    【原】文本图片自适应高度小bug以及解决办法
    【原】iOS学习39网络之数据请求
    【原】iOS学习38网络之数据解析
    iOS数据持久化文件读写之偏好设置
    SQLite错误码
    iOS开发代码规范(通用)
    iOS学习37数据处理之CoreData
    iOS中的单例
    iOS学习36数据处理之SQLite数据库
    UIImage两种初始化的区别
  • 原文地址:https://www.cnblogs.com/AlwaysWIN/p/6225828.html
Copyright © 2011-2022 走看看