Mini game practice丨Backgammon mini game based on Tkinter

backgammon

write in front

Contents of this issue: Backgammon game based on tkinter

Download address: https://download.csdn.net/download/m0_68111267/88700190

lab environment

  • python3.11 and above
  • pycharm
  • tkinter

Tkinter

Tkinter is a standard graphical user interface (GUI) library for Python, which is based on the Tk toolkit. The Tkinter library provides various components and methods required for GUI application development, allowing developers to quickly create user-friendly interfaces.

In order to use the Tkinter library, you first need to import the Tkinter module. It is generally customary to use import tkinteror import tkinter as tkto import.

After that, creating a window is Tk()implemented using functions. For example:

import tkinter as tk

window = tk.Tk()
window.mainloop()

The above code creates a window named window and uses mainloop()a function to keep the window running until the user closes it.

In the window, you can add other components such as buttons, labels, text boxes, etc. For example, the code to create a button is as follows:

button = tk.Button(window, text="点击我")
button.pack()

The above code creates a button named button and uses pack()the method to add the button to the window.

In addition to buttons, you can also create components such as labels and text boxes. For example, the code to create a label is as follows:

label = tk.Label(window, text="这是一个标签")
label.pack()

The above code creates a label named label and uses pack()the method to add the label to the window.

For each component, you can set its properties such as text, color, size, etc. For example, the code to set the background color and text color of a button is as follows:

button.config(bg="red", fg="white")

The above code sets the background color of the button to red and the text color to white.

In addition to property settings, you can also bind event handling functions to components to trigger specific actions when users operate the components. For example, the code to bind a click event to a button is as follows:

button.config(command=button_click)

def button_click():
    print("按钮被点击了!")

The above code defines a function named button_click. When the button is clicked, the function will be triggered and a message will be output to the console.

In summary, GUI applications can be easily created through the Tkinter library. You can use various components and methods to create windows, add components, set properties and event handling, etc. This is just a simple introductory introduction. Tkinter has more functions and usages that can be further learned and explored.

backgammon

Background picture

Liu Shishi

programming

from tkinter.messagebox import *  # 导入消息弹出库
from tkinter import *  # 导入tkinter界面库
from random import *  # 导入random随机库
from PIL import Image, ImageTk  # 导入PIL包中的Image包和ImageTk包,用于打开图片,用作背景(可更换图片)
import winsound  # 导入声音库
import os

# 声明全局变量开始
global canvas
global back_x, back_y, last
global qipan
qipan = [[2 for i in range(16)] for i in range(16)]  # 2表示空,0表示蓝棋,1表示粉棋
col = ['蓝', '粉']
global index  # 创建落子计数变量
index = 0
global var_top
global hui
hui = 0
# 声明全局变量结束

……请下载后查看

​Run results

backgammon

Series of articles

serial number Table of contents direct link
1 Tetris mini game based on PyGame https://want595.blog.csdn.net/article/details/135427809
2 Backgammon game based on Tkinter https://want595.blog.csdn.net/article/details/135427644
3 Xiaoxiaole mini game based on PyGame https://want595.blog.csdn.net/article/details/135390188
4 Snake game based on PyGame https://want595.blog.csdn.net/article/details/135373146

write on the back

I am a funny bunny, thank you for your like!

Guess you like

Origin blog.csdn.net/m0_68111267/article/details/135427644