tkinter制作

图形化界面设计的基本理解

当前流行的计算机桌面应用程序大多数为图形化用户界面(Graphic User Interface,GUI),即通过鼠标对菜单、按钮等图形化元素触发指令,并从标签、对话框等图型化显示容器中获取人机对话信息。
Python自带了tkinter 模块,实质上是一种流行的面向对象的GUI工具包 TK 的Python编程接口,提供了快速便利地创建GUI应用程序的方法。其图像化编程的基本步骤通常包括:

○ 导入 tkinter 模块
○ 创建 GUI 根窗体
○ 添加人机交互控件并编写相应的函数。
○ 在主事件循环中等待用户触发事件响应

from tkinter import *
root= Tk()
root.title('我的第一个Python窗体')
root.geometry('240x240') # 这里的乘号不是 * ,而是小写英文字母 x
root.mainloop()

在这里插入图片描述

利用 tkinter制作一个电子时钟

import tkinter
import time

def gettime():
      timestr = time.strftime("%H:%M:%S") # 获取当前的时间并转化为字符串
      lb.configure(text=timestr)   # 重新设置标签文本
      root.after(1000,gettime) # 每隔1s调用函数 gettime 自身获取时间

root = tkinter.Tk()
root.title('时钟')

lb = tkinter.Label(root,text='',fg='blue',font=("黑体",80))
lb.pack()
gettime()
root.mainloop()

在这里插入图片描述

使用tkinter制作一个猜数字游戏程序

#!/usr/bin/env python3

import tkinter
import math
import tkinter.messagebox
import random

root = tkinter.Tk()
root.minsize(350, 260)
root.title('猜数字游戏')

number = random.randint(1, 20)


def say_hello():
    print('hello,world!')


def send_low():
    tkinter.messagebox.showinfo("messagebox", "Your guess is too low.")


def check_num():
    guess = text_guess.get()
    guess = int(guess)
    if guess > number:
        tkinter.messagebox.showinfo("height", "Your guess is too height.")
    if guess < number:
        tkinter.messagebox.showinfo("low", "Your guess is too low.")
    if guess == number:
        tkinter.messagebox.showinfo("good", "Good job!")


def btn_confirm():
    myName = text_name.get()
    tkinter.messagebox.showinfo("name", 'Well,' + myName + ',I am thinking of a number between 1 and 20.')


# name
label = tkinter.Label(root, text="Wellcome to our game!")
label.pack()
label_name = tkinter.Label(root, text="What's your name?")
label_name.place(x=10, y=60)
text_name = tkinter.Entry(root, width=20)
text_name.place(x=10, y=90)
btnOK = tkinter.Button(root, text="OK", command=btn_confirm)
btnOK.place(x=200, y=90, height=28)

# input
label_guess = tkinter.Label(root, text='Take a guess:')
label_guess.place(x=10, y=150)
text_guess = tkinter.Entry(root, width=10)
text_guess.place(x=90, y=150)
btnCheck = tkinter.Button(root, text='Guess', command=check_num)
btnCheck.place(x=200, y=150, width=45, height=28)

root.mainloop()

在这里插入图片描述

将程序打包成一个可执行文件

  • 首先先要安装pyinstaller库
pip install pyinstaller 
  • 打包成——单独的exe文件
    在项目根目录下输入
pyinstaller -F cai.py

完成后,在dist文件夹下即可找到.exe文件,点击即可运行

该操作默认打包黑窗口,如果想不打包黑窗口,则输入

pyinstaller -F -w cai.py

在这里插入图片描述

使用tkinter制作贪吃蛇游戏

大家都知道用python做游戏用的比较多的是pygame。其实做游戏最重要的是思路,用什么库并不重要,特别是些小游戏,用自带的tkinter完全能胜任。虽然tkinter存在着各种的不便,但毕竟是自带的,用起来也简单,非常适合新手入门。
  
  废话少说,开始正题。选贪吃蛇做入门练手游戏最适合不过了,没玩过的也应该见过吧?先随我一起看看游戏的设计思路~
  贪吃蛇游戏画面很简单,若干个方格组成一个矩阵,颜色最低三色即可,空地,食物和蛇身。我这里用了五种颜色,增加了四周的墙和蛇头(蛇头和蛇身颜色分开,更直观些)。
  其实贪吃蛇这个游戏控制的就是蛇头的走向,蛇身其实就是蛇头的运动轨迹,只要记录下蛇头的每一步坐标,然后根据所需要的长度,就可以拼凑出蛇身来。有个这个思路,这个游戏制作起来就简单了。好了,开始了, 先加载需要的库。

import tkinter as tk
from tkinter.messagebox import showinfo
import random

showinfo是个弹窗,暂停游戏时用。
random都知道,生成随机数。

然后设置下游戏参数。

class Snake():
    """ 贪吃蛇游戏  """
    def __init__(self):
        """ 游戏参数设置 """
        global body_len, FPS
        body_len        = 1                 # 蛇身初始长度(最小设定值为1,不包括蛇头)
        FPS             = 120               # 蛇的移动速度(单位毫秒)
        self.row_cells  = 22                # 一行多少个单元格(含边框)
        self.col_cells  = 22                # 一共多少行单元格(含边框)
        self.canvas_bg  = 'white'           # 游戏背景色
        self.cell_size  = 25                # 方格单元格大小
        self.cell_gap   = 1                 # 方格间距
        self.frame_x    = 15                # 左右边距
        self.frame_y    = 15                # 上下边距
        self.win_w_plus = 220               # 窗口右边额外多出的宽度
        self.color_dict = {0:  '#d7d7d7',     # 0表示空白
                           1:   'yellow',     # 1代表蛇头
                           2:  '#009700',     # 2代表蛇身
                           3:      'red',     # 3代表食物
                           4:  '#808080'}     # 4代表墙

body_len不要设置为0,不然连蛇头都没有了。
之前提到的五种颜色,放置在color_dict这个字典中,方便调用。
然后游戏窗口设置成居中。

    def window_center(self,window,w_size,h_size):
        """ 窗口居中 """
        screenWidth  =  window.winfo_screenwidth()  # 获取显示区域的宽度
        screenHeight = window.winfo_screenheight()  # 获取显示区域的高度
        left =  (screenWidth - w_size) // 2
        top  = (screenHeight - h_size) // 2
        window.geometry("%dx%d+%d+%d" % (w_size, h_size, left, top))

显示窗体

    def run_game(self):
        """ 开启游戏 """
        global window
        window = tk.Tk()
        window.focus_force() # 主窗口焦点
        window.title('Snake')
        win_w_size = self.row_cells * self.cell_size + self.frame_x*2 + self.win_w_plus 
        win_h_size = self.col_cells * self.cell_size + self.frame_y*2
        Snake().window_center(window,win_w_size,win_h_size)
        txt_lable = tk.Label(window, text=
                              "方向键移动,或者"
                             +"\n字母键WSAD移动"
                             +"\n(大小写均可)"
                             +"\n"
                             +"\n空格键暂停",
                             font=('Yahei', 15),anchor="ne", justify="left")
        txt_lable.place(x= self.cell_size * self.col_cells + self.cell_size*2, 
                        y = self.cell_size*6)

窗体架构完了,开始游戏内容了。先创建个游戏地图,既然是若干个方格所组成的矩阵,那每个方格就对应一个坐标吧。

    def create_map(self):
        """ 创建地图列表 """
        global game_map
        game_map = [] 
        for i in range(0,self.col_cells):
            game_map.append([])
        for i in range(0,self.col_cells):
           for j in range(0,self.row_cells):
              game_map[i].append(j)   
              game_map[i][j] = 0  # 生成一个全是0的空数列

有了这张game_map就可以开始作画了,先画上四周的边框。

        """ 绘制边框 """
        for i in range(0,self.row_cells-1):
            game_map[0][i] = 4
            game_map[self.col_cells-1][i] = 4
        for i in range(0,self.col_cells-1):
            game_map[i][0] = 4
            game_map[i][self.row_cells-1] = 4
        game_map[-1][-1] = 4

边框有了,接着把蛇头画出来。

    def create_snake(self):
        """ 创建蛇头和蛇身 """
        global snake_body
        snake_body = [[self.col_cells // 2 , self.row_cells // 2]] # 蛇头出生地在地图的中央
        game_map[snake_body[0][0]][snake_body[0][1]] = 1  # 蛇头上色,颜色为定义的1

snake_body是个列表,初始只有蛇头的坐标,之后的蛇身因为蛇头还没开始走动,所以还没有生成。

蛇头有了,接下来就是食物了,这回随机数派上用处

    def create_food(self):
        """ 创建食物 """
        global food_xy
        food_xy = [0,0]
        food_xy[1] = random.randint(1, self.row_cells-2)
        food_xy[0] = random.randint(1, self.col_cells-2)
        while game_map[food_xy[0]][food_xy[1]] != 0:
            food_xy[0] = random.randint(1,self.row_cells-2)
            food_xy[1] = random.randint(1,self.col_cells-2)
        game_map[food_xy[0]][food_xy[1]] = 3

食物必须得出现在空地上,不然嵌在墙里也就算了,直接在蛇身上冒出来总不合适吧。。。

现在,边框,蛇头和食物都定义好了,我们现在加载看看效果。

    def load_map(self):
        """ 加载地图 """
        global canvas,window
        canvas_h = self.cell_size * self.col_cells + self.frame_y*2
        canvas_w = self.cell_size * self.row_cells + self.frame_x*2
        
        canvas = tk.Canvas(window, 
                                           bg = self.canvas_bg, 
                                       height = canvas_h,
                                        width = canvas_w,
                           highlightthickness = 0)

        for y in range(0,self.col_cells):
              for x in range(0,self.row_cells):
                canvas.create_rectangle(self.frame_x + self.cell_size*x,
                                        self.frame_y + self.cell_size*y,
                                        self.frame_x + self.cell_size*(x+1),
                                        self.frame_y + self.cell_size*(y+1),
                                        fill         = self.color_dict[game_map[y][x]],
                                        outline      = self.canvas_bg,
                                        width        = self.cell_gap)
                                        
        canvas.place(x=0,y=0)

这样就得到了蛇头的XY轴的坐标,那就让它动起来!

    def move_snake(self,event):
        """ 蛇体移动 """
        def move_key(a,b,c,d):  # 记录按键的方向,1上2下3左4右
            direction = event.keysym
            if   head_x != snake_body[-1][1]:
                if(direction == a):
                    dd[0] = 1
                if(direction == b):
                    dd[0] = 2
            else:
                if(direction == c):
                    dd[0] = 3
                if(direction == d):
                    dd[0] = 4
                  
            if   head_y != snake_body[-1][0]:
                if(direction == c):
                    dd[0] = 3
                if(direction == d):
                    dd[0] = 4
            else:
                if(direction == a):
                    dd[0] = 1
                if(direction == b):
                    dd[0] = 2

        def pause_key(key):
            """ 暂停键 """
            global loop
            direction = event.keysym
            if(direction == key):
                loop = 0
                showinfo('暂停','按确定键继续')
                loop = 1
                window.after(FPS, Snake().game_loop)
                
        move_key('w','s','a','d')
        move_key('W','S','A','D')
        move_key('Up','Down','Left','Right')
        pause_key('space')

四个方向键只是记录方位,修改dd[0]的值,1~4分别为上下左右。顺便把暂停键空格也定义上,一按空格键,loop值就等于0,停止游戏界面刷新。
之前说过,蛇身就是蛇头的运动轨迹,既然蛇头动起来了,蛇身的数据也就得到了。

    def snake_record(self):
        """ 蛇身 """ # 记录蛇头运行轨迹,生成蛇身
        global body_len,snake_body
        
        temp = []
        temp.append(head_y)
        temp.append(head_x)
        snake_body.append(temp) 
        
        if snake_body[-1] == snake_body[-2]: 
            del snake_body[-1]
        
        if [head_y,head_x] == food_xy: # 碰到食物身体加长,并再随机生成一个食物
            body_len = body_len + 1
            Snake().create_food()
        elif len(snake_body) > body_len:  # 限制蛇身长度,不超过设定值
            game_map[snake_body[0][0]][snake_body[0][1]] = 0
            del snake_body[0]

这样所有的素材都齐全了,大功快要告成了!

    def game_over(self):
        
        showinfo('Game Over','再来一局')
        Snake().game_start()

    def auto_move(self):
            """ 自动前进 """
            if [head_y,head_x] in snake_body[0:-2]:
                Snake().game_over()
            if head_x == self.row_cells - 1 or head_x == 0:
                Snake().game_over()
            if head_y == self.col_cells - 1 or head_y == 0:
                Snake().game_over()
            
            if dd[0] == 4:  # 根据方向值来决定走向
                game_map[head_y + 0][head_x + 1] = 1
                game_map[head_y + 0][head_x + 0] = 2
            if dd[0] == 3:
                game_map[head_y + 0][head_x - 1] = 1
                game_map[head_y + 0][head_x + 0] = 2             
            if dd[0] == 2:
                game_map[head_y + 1][head_x + 0] = 1
                game_map[head_y + 0][head_x + 0] = 2
            if dd[0] == 1:
                game_map[head_y - 1][head_x + 0] = 1
                game_map[head_y + 0][head_x + 0] = 2

根据游戏设定,蛇头撞墙或者撞到自身了就Game Over。位置的移动根据蛇头的坐标X轴或Y轴加一或减一就行。蛇头前方的格子变成蛇头(1),然后原本蛇头的位置变为蛇身(2)。

现在让窗体自动刷新起来,这样蛇头才会自动前进。
并把所有的函数串起来。

    def game_loop(self):
        """ 游戏循环刷新 """
        Snake().snake_record()
        Snake().auto_move()
        Snake().snake_xy()
        Snake().load_map()
        
        if loop == 1:
            window.after(FPS, Snake().game_loop)
        
        
    def game_start(self):
        """  """
        global window, backup_map, dd, loop
        loop = 1 # 暂停标记,1为开启,0为暂停
        dd = [0] # 记录按键方向
        Snake().create_map()
        Snake().create_wall()
        Snake().create_snake()
        Snake().create_food()
        Snake().load_map()
        window.bind('<Key>', Snake().move_snake)
        Snake().snake_xy()
        Snake().game_loop()
        window.mainloop()

loop值为1时,window.after起作用,让游戏界面根据FPS值不断刷新。

至此,这个贪吃蛇的游戏算是全部完成了,该有的功能都有,就缺个计分功能。请大家自行完成

import tkinter as tk
from tkinter.messagebox import showinfo
import random


class Snake():
    """ 贪吃蛇游戏  """

    def __init__(self):
        """ 游戏参数设置 """
        global body_len, FPS
        body_len = 3  # 蛇身初始长度(最小设定值为1,不包括蛇头)
        FPS = 120  # 蛇的移动速度(单位毫秒)
        self.row_cells = 22  # 一行多少个单元格(含边框)
        self.col_cells = 22  # 一共多少行单元格(含边框)
        self.canvas_bg = 'white'  # 游戏背景色
        self.cell_size = 25  # 方格单元格大小
        self.cell_gap = 1  # 方格间距
        self.frame_x = 15  # 左右边距
        self.frame_y = 15  # 上下边距
        self.win_w_plus = 220  # 窗口右边额外多出的宽度
        self.color_dict = {0: '#d7d7d7',  # 0表示空白
                           1: 'yellow',  # 1代表蛇头
                           2: '#009700',  # 2代表蛇身
                           3: 'red',  # 3代表食物
                           4: '#808080'}  # 4代表墙

        self.run_game()

    def window_center(self, window, w_size, h_size):
        """ 窗口居中 """
        screenWidth = window.winfo_screenwidth()  # 获取显示区域的宽度
        screenHeight = window.winfo_screenheight()  # 获取显示区域的高度
        left = (screenWidth - w_size) // 2
        top = (screenHeight - h_size) // 2
        window.geometry("%dx%d+%d+%d" % (w_size, h_size, left, top))

    def create_map(self):
        """ 创建地图列表 """
        global game_map

        game_map = []
        for i in range(0, self.col_cells):
            game_map.append([])
        for i in range(0, self.col_cells):
            for j in range(0, self.row_cells):
                game_map[i].append(j)
                game_map[i][j] = 0  # 生成一个全是0的空数列

    def create_wall(self):
        """ 绘制边框 """
        for i in range(0, self.row_cells - 1):
            game_map[0][i] = 4
            game_map[self.col_cells - 1][i] = 4

        for i in range(0, self.col_cells - 1):
            game_map[i][0] = 4
            game_map[i][self.row_cells - 1] = 4
        game_map[-1][-1] = 4

    def create_canvas(self):
        """ 创建画布 """
        global canvas, window
        canvas_h = self.cell_size * self.col_cells + self.frame_y * 2
        canvas_w = self.cell_size * self.row_cells + self.frame_x * 2

        canvas = tk.Canvas(window,
                           bg=self.canvas_bg,
                           height=canvas_h,
                           width=canvas_w,
                           highlightthickness=0)

    def create_cells(self):
        """ 创建单元格 """
        for y in range(0, self.col_cells):
            for x in range(0, self.row_cells):
                canvas.create_rectangle(self.frame_x + self.cell_size * x,
                                        self.frame_y + self.cell_size * y,
                                        self.frame_x + self.cell_size * (x + 1),
                                        self.frame_y + self.cell_size * (y + 1),
                                        fill=self.color_dict[game_map[y][x]],
                                        outline=self.canvas_bg,
                                        width=self.cell_gap)

        canvas.place(x=0, y=0)

    def create_snake(self):
        """ 创建蛇头和蛇身 """
        global snake_body

        snake_body = [[self.col_cells // 2, self.row_cells // 2]]  # 蛇头出生地在地图的中央
        game_map[snake_body[0][0]][snake_body[0][1]] = 1  # 蛇头上色,颜色为定义的1

    def create_food(self):
        """ 创建食物 """
        global food_xy

        food_xy = [0, 0]
        food_xy[1] = random.randint(1, self.row_cells - 2)
        food_xy[0] = random.randint(1, self.col_cells - 2)

        while game_map[food_xy[0]][food_xy[1]] != 0:
            food_xy[0] = random.randint(1, self.row_cells - 2)
            food_xy[1] = random.randint(1, self.col_cells - 2)

        game_map[food_xy[0]][food_xy[1]] = 3

    def snake_xy(self):
        """ 获取蛇头坐标 """
        global head_x, head_y
        xy = []
        for i in range(0, self.col_cells):
            try:  # 查找数值为1的坐标,没有就返回0。为防止在0列,先加上1,最后再减去。
                x = game_map[i].index(1) + 1
            except:
                x = 0
            xy.append(x)
        head_x = max(xy)
        head_y = xy.index(head_x)
        head_x = head_x - 1  # 之前加1,现在减回

    def move_snake(self, event):
        """ 蛇体移动 """

        def move_key(a, b, c, d):  # 记录按键的方向,1上2下3左4右
            direction = event.keysym

            if head_x != snake_body[-1][1]:
                if (direction == a):
                    dd[0] = 1
                if (direction == b):
                    dd[0] = 2
            else:
                if (direction == c):
                    dd[0] = 3
                if (direction == d):
                    dd[0] = 4

            if head_y != snake_body[-1][0]:
                if (direction == c):
                    dd[0] = 3
                if (direction == d):
                    dd[0] = 4
            else:
                if (direction == a):
                    dd[0] = 1
                if (direction == b):
                    dd[0] = 2

        def pause_key(key):
            """ 暂停键 """
            global loop
            direction = event.keysym
            if (direction == key):
                loop = 0
                showinfo('暂停', '按确定键继续')
                loop = 1
                window.after(FPS, self.game_loop)

        move_key('w', 's', 'a', 'd')
        move_key('W', 'S', 'A', 'D')
        move_key('Up', 'Down', 'Left', 'Right')
        pause_key('space')

    def game_over(self):

        showinfo('Game Over', '再来一局')
        self.game_start()

    def snake_record(self):
        """ 蛇身 """  # 记录蛇头运行轨迹,生成蛇身
        global body_len, snake_body

        temp = []
        temp.append(head_y)
        temp.append(head_x)
        snake_body.append(temp)

        if snake_body[-1] == snake_body[-2]:
            del snake_body[-1]

        if [head_y, head_x] == food_xy:  # 碰到食物身体加长,并再随机生成一个食物
            body_len = body_len + 1
            self.create_food()
        elif len(snake_body) > body_len:  # 限制蛇身长度,不超过设定值
            game_map[snake_body[0][0]][snake_body[0][1]] = 0
            del snake_body[0]

    def auto_move(self):
        """ 自动前进 """
        if [head_y, head_x] in snake_body[0:-2]:
            self.game_over()
        if head_x == self.row_cells - 1 or head_x == 0:
            self.game_over()
        if head_y == self.col_cells - 1 or head_y == 0:
            self.game_over()

        if dd[0] == 4:  # 根据方向值来决定走向
            game_map[head_y + 0][head_x + 1] = 1
            game_map[head_y + 0][head_x + 0] = 2
        if dd[0] == 3:
            game_map[head_y + 0][head_x - 1] = 1
            game_map[head_y + 0][head_x + 0] = 2
        if dd[0] == 2:
            game_map[head_y + 1][head_x + 0] = 1
            game_map[head_y + 0][head_x + 0] = 2
        if dd[0] == 1:
            game_map[head_y - 1][head_x + 0] = 1
            game_map[head_y + 0][head_x + 0] = 2

    def game_loop(self):
        """ 游戏循环刷新 """
        global loop_id
        self.snake_record()
        self.auto_move()
        self.snake_xy()
        canvas.delete('all')  # 清除canvas
        self.create_cells()

        if loop == 1:
            loop_id = window.after(FPS, self.game_loop)

    def game_start(self):
        """  """
        global window, backup_map, dd, loop
        loop = 1  # 暂停标记,1为开启,0为暂停
        dd = [0]  # 记录按键方向
        self.create_map()
        self.create_wall()
        self.create_snake()
        self.create_food()
        self.create_canvas()
        self.create_cells()
        window.bind('<Key>', self.move_snake)
        self.snake_xy()
        self.game_loop()

        def close_w():
            window.after_cancel(loop_id)
            window.destroy()

        window.protocol('WM_DELETE_WINDOW', close_w)
        window.mainloop()

    def run_game(self):
        """ 开启游戏 """
        global window

        window = tk.Tk()
        window.focus_force()  # 主窗口焦点
        window.title('Snake')

        win_w_size = self.row_cells * self.cell_size + self.frame_x * 2 + self.win_w_plus
        win_h_size = self.col_cells * self.cell_size + self.frame_y * 2
        self.window_center(window, win_w_size, win_h_size)

        txt_lable = tk.Label(window, text=
        "方向键移动,或者"
        + "\n字母键WSAD移动"
        + "\n(大小写均可)"
        + "\n"
        + "\n空格键暂停"
        + "\n作者:Eric",
                             font=('Yahei', 15), anchor="ne", justify="left")

        txt_lable.place(x=self.cell_size * self.col_cells + self.cell_size * 2,
                        y=self.cell_size * 6)

        self.game_start()


if __name__ == '__main__':
    Snake()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值