由于 Tkinter 是内置到 python 的安装包中、只要安装好 Python 之后就能 import Tkinter 库、而且 IDLE 也是用 Tkinter 编写而成、对于简单的图形界面 Tkinter 还是能应付自如。
-----有关TK模块的小应用
from tkinter import *
import time
tk = Tk()
canvas=Canvas(tk,width=4000,height=4000)#创建长宽4000的画布
# canvas.create_rectangle(100,100,300,300,fill="green")
# for i in range(0, 60):
# canvas.move(1, 0, -3)
# tk.update()
# time.sleep(0.05)
Id1=canvas.create_polygon(200,0,100,100,300,100,fill="black")
canvas.pack() #将画布展示出来
def press_x(event):
if event.keysym == "Up":
canvas.move(1, 0, -3)
canvas.itemconfigure(Id1,fill="green")
elif event.keysym == "Down":
canvas.move(1,0,3)
canvas.itemconfigure(Id1, fill="red")
elif event.keysym == "Left":
canvas.move(1,-3,0)
canvas.itemconfigure(Id1, fill="yellow")
else :
canvas.move(1,3,0)
canvas.itemconfigure(Id1, fill="blue")
canvas.bind_all("<KeyPress-Up>",press_x)
canvas.bind_all("<KeyPress-Down>",press_x)
canvas.bind_all("<KeyPress-Left>",press_x)
canvas.bind_all("<KeyPress-Right>",press_x)
# canvas.create_arc(10,10,200,80,start=0,extent=359,style=ARC)
tk.mainloop()
----用TK制作的小球Game
from tkinter import *
import random
import time
class Ball:
def __init__(self,canvas,paddle,color):
self.canvas=canvas
self.paddle=paddle
self.id=canvas.create_oval(10,10,25,25,fill=color)
self.canvas.move(self.id,245,100)
starts=[-3,-2,-1,1,2,3]#让x开始的坐标为列表里的任意一个坐标
random.shuffle(starts)
self.x=starts[0]
self.y=-3
self.canvas_width=self.canvas.winfo_width()#获取画布当前的宽度
self.canvas_height=self.canvas.winfo_height()#获取画布当前的高度
self.hit_bottom=False
def hit_paddle(self,pos):#击打函数,pos是小球的坐标
paddle_pos=self.canvas.coords(self.paddle.id)#获取球拍的位置
if pos[2]>=paddle_pos[0] and pos[0]<=paddle_pos[2]:#小球右侧大于球拍左侧且小球左侧小于球拍右侧
if pos[3]>=paddle_pos[1]and pos[3]<=paddle_pos[3]:
return True
return False
def draw(self):
self.canvas.move(self.id,self.x,self.y)
pos=self.canvas.coords(self.id)#用来传入画布上任何画好的东西的坐标(四个坐标)
if pos[1]<=0:#判断Y1坐标(即小球的顶部)是否小于等于0,成立即不再减少
self.y=3
if pos[3]>=self.canvas_height:#判断Y2坐标(即小球的底部)是否大于画布高度,成立也往回走
self.hit_bottom=True
if self.hit_paddle(pos)==True:
self.y=-3
if pos[0]<=0:
self.x=3
if pos[2]>=self.canvas_width:
self.x=-3
class Paddle:
def __init__(self,canvas,color):
self.canvas=canvas
self.id=canvas.create_rectangle(0,0,100,10,fill=color)
self.canvas.move(self.id,200,350)
self.x=0
self.canvas_width=self.canvas.winfo_width()
self.canvas.bind_all('<KeyPress-Left>',self.turn_left)
self.canvas.bind_all('<KeyPress-Right>',self.turn_right)
def draw(self):
self.canvas.move(self.id,self.x,0)
pos=self.canvas.coords(self.id)
if pos[0]<=0:
self.x=0
if pos[2]>=self.canvas_width:
self.x=0
def turn_left(self,evt):
self.x=-2
def turn_right(self,evt):
self.x=2
tk=Tk()
tk.title("Game")
tk.resizable(0,0)#窗口在垂直和水平方向都不能改变
tk.wm_attributes("-topmost",1)#画布窗口放在所有窗口之前
canvas=Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)#确保画布之外没有边框
canvas.pack()
tk.update()
paddle=Paddle(canvas,'green')
ball=Ball(canvas,paddle,'red')
while 1:
if ball.hit_bottom==False:
ball.draw()
paddle.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.1)