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

本文对比了Python2.x与Python3.x中Tkinter模块在文件操作方面的不同,包括导入方式、打开文件对话框、显示对话框及创建下拉列表等,并提供了相应的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天初次使用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()

 

 

 

 

参考链接:https://www.cnblogs.com/AlwaysWIN/p/6225828.html

转载于:https://www.cnblogs.com/cap-rq/p/11207251.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值