简单图形编程
笑脸代码
from graphics import*
win = GraphWin()
face = Circle(Point(100,95),50)
leftEye = Circle(Point(80,80),5)
leftEye.setFill("yellow")
leftEye.setOutline("red")
rightEye = Circle(Point(120,80),5)
rightEye.setFill("yellow")
rightEye.setOutline("red")
mouth = Line(Point(80,110),Point(120,110))
face.draw(win)
mouth.draw(win)
leftEye.draw(win)
rightEye.draw(win)
input("Press <enter>")
图形界面编程
小栗子1:点击10次图形界面框,控制打印点击的坐标
from graphics import*
def main():
win = GraphWin("Click Me!")
for i in range(10):
p = win.getMouse()
print("You clicked at:",p.getX(),p.getY())
main()
运行结果
小栗子2:交互式绘制五边形
#coding=gbk
from graphics import*
def main():
win = GraphWin("Draw a polygon",300,300)
win.setCoords(0.0, 0.0, 300.0, 300.0)
message = Text(Point(150,20),"Click on five points")
message.draw(win)
#获得多边形的6个点
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
p3 = win.getMouse()
p3.draw(win)
p4 = win.getMouse()
p4.draw(win)
p5 = win.getMouse()
p5.draw(win)
p6 = win.getMouse()
p6.draw(win)
#使用Polygon对象绘制多边形
polygon = Polygon(p1,p2,p3,p4,p5,p6)
polygon.setFill("peachpuff")
polygon.setOutline("black")
polygon.draw(win)
#等待响应鼠标事件,退出程序
message.setText("Click anywehere to quit.")
win.getMouse()
main()
小栗子3:温度转换
#coding=gbk
from graphics import *
win = GraphWin("Celsius Converter", 400, 300)
win.setCoords(0.0, 0.0, 3.0, 4.0)
# 绘制接口
Text(Point(1,3), " Celsius Temperature:").draw(win)
Text(Point(1,1), "Fahrenheit Temperature:").draw(win)
input = Entry(Point(2,3), 5)
input.setText("0.0")
input.draw(win)
output = Text(Point(2,1),"")
output.draw(win)
button = Text(Point(1.5,2.0),"Convert It")
button.draw(win)
Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
# 等待鼠标点击
win.getMouse()
# 转换输入
celsius = eval(input.getText())
fahrenheit = 9.0/5.0 * celsius + 32.0
# 显示输出,改变按钮
output.setText(fahrenheit)
button.setText("Quit")
# 等待响应鼠标点击,退出程序
win.getMouse()
win.close()
使用Tkinter创建GUI
小栗子1:tk库初试
from tkinter import *
tk = Tk()
label = Label(tk,text = "Welcome to Python Tkinter")
button = Button(tk, text = "Click Me")
label.pack()
button.pack()
tk.mainloop()
小栗子2:响应用户事件
#coding=gbk
from tkinter import *
#定义回调函数
def processOK():
print("OK button is clicked")
def processCancel():
print("Cancel button is clicked")
def main():
tk=Tk()
btnOK = Button(tk,text = "OK",fg = "red",command = processOK)
btnCancel = Button(tk, text = "Cancel", bg = "yellow",command = processCancel)
btnOK.pack()
btnCancel.pack()
tk.mainloop()
main()
小栗子3:画布显示文字、图片或者绘制图形
from tkinter import *
def main():
tk = Tk()
canvas = Canvas(tk,width = 200, height = 200)
canvas.pack()
canvas.create_text(100,40,text = "Welcom to Tkinter",fill = "blue",font = ("Times",16))
myImage = PhotoImage(file = "python_logo.gif")
canvas.create_image(10,70,anchor = NW, image = myImage)
canvas.create_rectangle(10,70,190,130)
tk.mainloop()
main()
小栗子4:上下左右键控制图形移动
from tkinter import *
def main():
tk = Tk()
canvas = Canvas(tk, width = 400, height = 400)
canvas.pack()
def moverectangle(event):
if event.keysym == "Up":
canvas.move(1,0,-5)
elif event.keysym == "Down":
canvas.move(1,0,5)
elif event.keysym == "Left":
canvas.move(1,-5,0)
elif event.keysym == "Right":
canvas.move(1,5,0)
canvas.create_rectangle(180,180,220,220,fill="red")
canvas.bind_all("<KeyPress-Up>",moverectangle)
canvas.bind_all("<KeyPress-Down>",moverectangle)
canvas.bind_all("<KeyPress-Left>",moverectangle)
canvas.bind_all("<KeyPress-Right>",moverectangle)
main()