python34gui编程

来源:互联网 发布:windows rt看视频 编辑:程序博客网 时间:2024/05/20 10:55

1.用python自带的tkinter写了个gui界面,很简单,直接贴源码吧,希望对入门的人有所帮助,环境是vs2010,ptvs,python3.4

2.源码

from tkinter import *;import tkinter.messagebox;import sys;#MessageBox控件def sayHello():    tkinter.messagebox.showinfo('title','sayHello');def openDialog():    tkinter.messagebox.showinfo('title','openDialog');def createDialog():    tkinter.messagebox.showinfo('title','createDialog');def exitWindow():    sys.exit();def rbButton():    tkinter.messagebox.showinfo('title',str(var.get()));def cbButton1():    tkinter.messagebox.showinfo('title',str(v1.get()));def cbButton2():    tkinter.messagebox.showinfo('title',str(v2.get()));#顶层窗口mainFrame = Tk();mainFrame.title('tkinter 测试');mainFrame.geometry('800x600+100+100');#MenuBar控件menuBar = Menu(mainFrame);#文件菜单fileMenu = Menu(menuBar,tearoff=0);fileMenu.add_command(label='新建',command=createDialog);fileMenu.add_command(label='打开',command=openDialog);fileMenu.add_separator();fileMenu.add_command(label='退出',command=exitWindow);menuBar.add_cascade(label='文件',menu=fileMenu);#编辑菜单editMenu = Menu(menuBar,tearoff=0);editMenu.add_command(label='撤销',command=sayHello);editMenu.add_command(label='重做',command=sayHello);menuBar.add_cascade(label='编辑',menu=editMenu);#帮助菜单helpMenu = Menu(menuBar,tearoff=0);helpMenu.add_command(label='关于我们',command=sayHello);menuBar.add_cascade(label='帮助',menu=helpMenu);#Frame控件contentFrame = Frame(mainFrame,width=600);contentFrame.bg = '#ff2233';contentFrame.pack(fill=BOTH);#Listbox控件scrollFrame = Frame(contentFrame);scrollFrame.pack(side=LEFT);listBox = Listbox(scrollFrame);listBox.insert(0,'0');listBox.insert(1,'1');listBox.insert(2,'2');listBox.insert(3,'3');listBox.insert(4,'4');listBox.insert(5,'5');listBox.pack(side=LEFT,fill=BOTH);#Scrollbar控件scroll = Scrollbar(scrollFrame);scroll.pack(side=RIGHT,fill=Y);scroll.config( command = listBox.yview );#Checkbutton控件v1 = IntVar();v2 = IntVar();cb1 = Checkbutton(contentFrame, text = "cb1", variable = v1, onvalue = 1, offvalue = 0, height=2, width = 20,command=cbButton1)cb2 = Checkbutton(contentFrame, text = "cb2", variable = v2, onvalue = 1, offvalue = 0, height=2, width = 20,command=cbButton2)cb1.pack(side=LEFT)cb2.pack(side=LEFT)#Radiobutton控件var = IntVar();rb1 = Radiobutton(contentFrame,variable = var,text='rb1',value=1,command=rbButton);rb1.pack(side=LEFT,anchor=W);rb2 = Radiobutton(contentFrame,variable = var,text='rb2',value=2,command=rbButton);rb2.pack(side=LEFT,anchor=W);#Text控件text1 = Text(contentFrame,height=2,);text1.pack(fill=X);#Button控件button1 = Button(contentFrame,height=2,text='添加你输入到列表中',command=sayHello);button1.pack(fill=X);#Label控件statusFrame = Frame(mainFrame);statusFrame.pack(side=BOTTOM,fill=X);label1 = Label(statusFrame,text='这里是状态栏',bg='#ff4400',height=2);label1.pack(fill=X);mainFrame.config(menu=menuBar);        mainFrame.mainloop();#开启窗口#3.4版本把2版本中的有些控件都移动到tkinter模块下,输入tkinter后ctrl+j,即可查看#更多参考下面地址 http://www.yiibai.com/python/python_gui_programming.html
3.运行结果

0 0
原创粉丝点击