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()

  • 相关阅读:
    俞敏洪谈大学爱情:终于在清华说实话了
    人的有两面性
    冯亚丽,打造1500亿帝国的奇女子
    做企业就要有精气神
    当心!“饭桌教育”祸害你的孩子
    为何企业宁可高薪招人,也不给老员工加薪?
    住的离公司有多远,成长就有多难
    物理像素,逻辑像素,解决1px的问题
    移动端布局 + iscroll + 滚动事件
    移动端布局 + iscroll.js
  • 原文地址:https://www.cnblogs.com/AlwaysWIN/p/6225828.html
Copyright © 2011-2022 走看看