最简单的Tkinter demo

来源:互联网 发布:淘宝如何导出订单信息 编辑:程序博客网 时间:2024/05/16 10:40

 #-*- coding:cp936 -*-
#tkinter_demo.pyw (.pyw不产生console窗口)
#参考 http://www.pythonware.com/library/ 有Tkinter和PIL的详细教程
from Tkinter import *
import tkMessageBox
import tkSimpleDialog
import tkFileDialog
root=Tk()

#text窗口
t=Text(root)
t.pack()
#菜单调用函数
def on_command1():
 #简单提示信息
 tkMessageBox.showinfo("info",u"简单信息提示框")
def on_command2():
 #简单输入
 input=tkSimpleDialog.askstring("input",u"请输入")
 t.insert(END,"you input: %s/n" %input)
def on_command3():
 #简单获得打开文件名(自动判断文件是否存在)
 filename=tkFileDialog.askopenfilename()
 t.insert(END,"you select: %s/n" %filename)
def on_command4():
 #简单获得保存文件名(自动判断是否存在同名文件)
 filename=tkFileDialog.asksaveasfilename()
 t.insert(END,"you choose: %s/n" %filename)
#顶层菜单(横排)
menubar=Menu(root)
#子菜单(纵排)
filemenu1=Menu(menubar,tearoff=0)
filemenu2=Menu(menubar,tearoff=0)
#子菜单项目
filemenu1.add_command(label="menu1_item1",command=on_command1)
filemenu1.add_command(label="menu1_item2",command=on_command2)
filemenu2.add_command(label="menu2_item1",command=on_command3)
filemenu2.add_command(label="menu2_item2",command=on_command4)
#链接顶层菜单和子菜单
menubar.add_cascade(label="menu1",menu=filemenu1)
menubar.add_cascade(label="menu2",menu=filemenu2)
#链接主窗口和顶层菜单
root["menu"]=menubar

root.mainloop()


原创粉丝点击