Python GUI with Tkinter (from youtube) 在youtube上能找到很多编程视频...
Python GUI with Tkinter - 1 - Introduction
以上链接是一个python tkinter视频系列的第一讲的链接.虽然英语不好,但是,程序还是看得懂的(照着做就可以了),所以找不到中文视频时看下这些英语视频也是可以的.
以下是我在看视频过程中的练习, 可以在python2.7下运行.
001: hello,world:
from Tkinter import Label, Tk root = Tk()
thelabel = Label(root, text="This is too easy")
thelabel.pack()
root.mainloop()
002: Button pack布局
from Tkinter import * root = Tk() topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM) button1 = Button(topFrame, text="Button 1", fg="red")
button2 = Button(topFrame, text="Button 2", fg="blue")
button3 = Button(topFrame, text="Button 3", fg="green")
button4 = Button(bottomFrame, text="Button 4", fg="purple") button1.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
button4.pack(side=BOTTOM) root.mainloop()
003: Label和pack布局
from Tkinter import * root = Tk() one = Label(root, text="one", bg="red", fg="white")
two = Label(root, text="two", bg="green", fg="black")
three = Label(root, text="three", bg="blue", fg="white") one.pack()
two.pack(fill=X)
three.pack(side=LEFT, fill=Y) root.mainloop()
004: grid布局.
from Tkinter import * root = Tk() label_1 = Label(root, text="Name")
label_2 = Label(root, text="Password")
entry_1 = Entry(root)
entry_2 = Entry(root) label_1.grid(row=0)
label_2.grid(row=1)
entry_1.grid(row=0, column=1)
entry_2.grid(row=1, column=1) root.mainloop()
005: grid布局
from Tkinter import * root = Tk() label_1 = Label(root, text="Name")
label_2 = Label(root, text="Password")
entry_1 = Entry(root)
entry_2 = Entry(root) label_1.grid(row=0, sticky=E)
label_2.grid(row=1, sticky=E)
entry_1.grid(row=0, column=1)
entry_2.grid(row=1, column=1) c = Checkbutton(root, text="Keep me logged in")
c.grid(columnspan=2) root.mainloop()
006: Button和事件
from Tkinter import * root = Tk() def printName():
print("Chello my name is Bucky!") button_1 = Button(root, text="Print my name", command=printName)
button_1.pack() root.mainloop()
007: 绑定事件: 左键,中键,右键
#coding:utf8
from Tkinter import * root = Tk() def printName(event):
print("Chello my name is Bucky!") button_1 = Button(root, text="Print my name")
'''
<Button-1> 鼠标左键
<Button-2> 鼠标中键
<Button-3> 鼠标右键
'''
button_1.bind("<Button-1>", printName)
button_1.pack() root.mainloop()
008: 绑定事件: 左键,中键,右键
from Tkinter import * root = Tk() def leftClick(event):
print "left" def middleClick(event):
print "middle" def rightClick(event):
print "right" frame = Frame(root, width=300, height=250)
frame.bind("<Button-1>", leftClick)
frame.bind("<Button-2>", middleClick)
frame.bind("<Button-3>", rightClick)
frame.pack() root.mainloop()
009: Python GUI with Tkinter-8-Using Classes
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-8-Using Classes
'''
from Tkinter import * class BuckysButtons:
def __init__(self, master):
frame = Frame(master)
frame.pack() self.printButton = Button(frame, text="Print Message", command=self.printMessage)
self.printButton.pack(side=LEFT) self.quitButton = Button(frame, text="Quit", command=frame.quit)
self.quitButton.pack(side=LEFT) def printMessage(self):
print "Wow, this actually worked!" root = Tk()
b = BuckysButtons(root)
root.mainloop()
010: Python GUI with Tkinter-9-Creating Drop Down Menus
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-9-Creating Drop Down Menus
'''
from Tkinter import * def doNothing():
print("ok ok I won't...") root = Tk() menu = Menu(root)
root.config(menu=menu) subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project...", command=doNothing)
subMenu.add_command(label="New...", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing) editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=doNothing) root.mainloop()
011: Python GUI with Tkinter-10-Creating a Toolbar
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-10-Creating a Toolbar
'''
from Tkinter import * def doNothing():
print("ok ok I won't...") root = Tk() # ***** Main Menu *****
menu = Menu(root)
root.config(menu=menu) subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project...", command=doNothing)
subMenu.add_command(label="New...", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing) editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=doNothing) # ***** Toolbar ***** toolbar = Frame(root, bg="blue") insertBtn = Button(toolbar, text="Insert Image", command=doNothing)
insertBtn.pack(side=LEFT, padx=2, pady=2)
printBtn = Button(toolbar, text="Print", command=doNothing)
printBtn.pack(side=LEFT, padx=2, pady=2) toolbar.pack(side=TOP, fill=X) root.mainloop()
012: Python GUI with Tkinter-11-Adding the Status Bar
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-11-Adding the Status Bar
'''
from Tkinter import * def doNothing():
print("ok ok I won't...") root = Tk() # ***** Main Menu *****
menu = Menu(root)
root.config(menu=menu) subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project...", command=doNothing)
subMenu.add_command(label="New...", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing) editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=doNothing) # ***** Toolbar ***** toolbar = Frame(root, bg="blue") insertBtn = Button(toolbar, text="Insert Image", command=doNothing)
insertBtn.pack(side=LEFT, padx=2, pady=2)
printBtn = Button(toolbar, text="Print", command=doNothing)
printBtn.pack(side=LEFT, padx=2, pady=2) toolbar.pack(side=TOP, fill=X) # ***** Status Bar ***** status = Label(root, text="Preparing to do nothing...", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X) root.mainloop()
013: Python GUI with Tkinter-12-Messagebox
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-12-Messagebox
How to create a message box with tkinter?
http://stackoverflow.com/questions/1052420/how-to-create-a-message-box-with-tkinter
'''
from Tkinter import *
import tkMessageBox root = Tk()
tkMessageBox.showinfo("Window Title", "Monkeys can live up to 300 years.")
msg = tkMessageBox answer = msg.askquestion("Question1", "Do you like silly faces?") if answer == 'yes':
print(' 8===D~') root.mainloop()
014: Python GUI with Tkinter-13-Shapes and Graphics
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-13-Shapes and Graphics
'''
from Tkinter import * root = Tk() canvas = Canvas(root, width=200, height=100)
canvas.pack() blackLine = canvas.create_line(0, 0, 200, 50)
redLine = canvas.create_line(0, 100, 200, 50, fill="red")
greenBox = canvas.create_rectangle(25, 25, 130, 60, fill="green") # canvas.delete(redLine)
canvas.delete(ALL) root.mainloop()
015: Python GUI with Tkinter-14-Images and Icons
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-14-Images and Icons
(1.1)在这个例子中遇到问题"_tkinter.TclError: couldn't recognize data in image file"
(1.2)http://stackoverflow.com/questions/27599311/tkinter-photoimage-doesnt-not-support-png-image
(1.3)http://effbot.org/tkinterbook/photoimage.htm
(1.4)解决办法参考上面的链接. 引入 "from PIL import Image, ImageTk"
---------------
(2)中文目录要加 u"..."
'''
from Tkinter import *
from PIL import Image, ImageTk root = Tk() imgList = ["image001.png", "kaola.jpg", u"考拉.jpg"]
print(imgList[-1])
image = Image.open(imgList[-1])
# photo = PhotoImage(file="image001.png")
photo = ImageTk.PhotoImage(image) label = Label(root, image=photo)
label.pack() root.mainloop()
Python GUI with Tkinter (from youtube) 在youtube上能找到很多编程视频...的更多相关文章
- Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) JAVA日志的前世今生 .NET MVC采用SignalR更新在线用户数 C#多线程编程系列(五)- 使用任务并行库 C#多线程编程系列(三)- 线程同步 C#多线程编程系列(二)- 线程基础 C#多线程编程系列(一)- 简介
Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) 一.前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅. 一.前言 ...
- python GUI学习——Tkinter
支持python的常见GUI工具包: Tkinter 使用Tk平台 很容易得到 半标准 wxpython 基于wxWindows.跨平台越来越流行 Python Win 只能在Windows上使用 使 ...
- Python GUI之tkinter窗口视窗教程大集合(看这篇就够了)
一.前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅. 一.前言 二.Tkinter 是什么 三.Tkinter 控件详细介绍 1. T ...
- Python GUI工具Tkinter以及拖拉工具Page安装
如果使用Tkinter作为Python GUI工具,我们需要安装Tkinter,这个使用conda或者pip即可: conda install -c anaconda tk 为了提高界面编写效率,可以 ...
- Python:GUI之tkinter学习笔记1控件的介绍及使用
相关内容: tkinter的使用 1.模块的导入 2.使用 3.控件介绍 Tk Button Label Frame Toplevel Menu Menubutton Canvas Entry Mes ...
- python gui之tkinter语法杂记
随手写的,后续整理. 1. Entry如何调用父类的__init__? 以下错,原因后续分析 super(Your_Entry_Class, self).__init__(self,**kw) 报错: ...
- python gui 之 tkinter库
http://blog.csdn.net/jcodeer?viewmode=contents http://tieba.baidu.com/p/3082739560 http://blog.sina. ...
- python gui之tkinter事件处理
事件一览表 事件 代码 备注 鼠标左键单击按下 1/Button-1/ButtonPress-1 鼠标左键单击松开 ButtonRelease-1 鼠标右键单击 3 鼠标左键双击 Doub ...
- python GUI 之 tkinter
写一个 登陆窗口来学习 tkinter ,还剩下一些问题 代码暂时如下 import tkinter as tk import webbrowser import pickle from tkinte ...
随机推荐
- Extjs中grid行的上移和下移
一.将up和down按钮放到tbar中,然后选中grid行即可实现上移和下移 var up = new Ext.Action({ text : 'Up', icon : 'up.png',//或者添加 ...
- HW4.36
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- guslterFS
Installing GlusterFS - a Quick Start Guide Purpose of this document This document is intended to giv ...
- 一切皆WEB
所有应用都应该成为Web应用吗?当然不是.总有一些重要的例外,有些种类的软件跟网络也毫无关系.但是,这些是少数情况,是一些特殊应用.它们固然是重要的小生态环境,但不管怎么说,就只是“小生态”. 如果你 ...
- jdbc内容
Jdbc:java数据库连接技术 主要执行操作:连接数据库,执行sql语句,处理结果 Class.forName("com.mysql.jdbc.Driver"); 执行jdbc驱 ...
- UIImage的使用
UIImage是IOS中层级比较高的一个用来加载和绘制图像的一个类,更底层的类还有CGImage,以及IOS5.0以后新增加的CIImage.今天我们主要聊一聊UIImage的三个属性: imageO ...
- 如何调试什么时候SaveFileDialog会被Dispose
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何调试什么时候SaveFileDialog会被Dispose.
- 在CentOS6上安装Redis
检查安装依赖程序 yum install -y gcc-c++ tcl wget 获取安装文件 wget http://download.redis.io/releases/redis-2.8.17. ...
- iOS设计模式之生成器
iOS设计模式之生成器 1.生成器模式的定义 (1): 将一个复杂的对象的构件与它的表示分离,使得相同的构建过程能够创建不同的表示 (2): 生成器模式除了客户之外还包括一个Director(指导者) ...
- 转换流--OutputStreamWriter类与InputStreamReader类
12.4 转换流--OutputStreamWriter类与InputStreamReader类 整个IO包实际上分为字节流和字符流,可是除了这两个流之外,还存在一组字节流-字符流的转换类. Out ...