【python】wxpython的界面实例

需要先安装依赖

pip install wxPython

界面代码:

# coding=utf-8
import configparser
import os
import sys
import re
import time
import threading

import wx


class MyApp(wx.Frame):
    def __init__(self, window, properties):
        wx.Frame.__init__(
            self, window, id=wx.ID_ANY,
            title=properties.get("title"),
            pos=wx.DefaultPosition,
            size=wx.Size(properties.get("size")),
            style=wx.DEFAULT_FRAME_STYLE ^ wx.MAXIMIZE_BOX
        )
        self.ini = os.path.join(os.environ["appdata"], 'my_app.ini')
        self.configuration = None
        self.support_product = ["A", "B", "C", "D", "E", "F", "G", "H","I", "J"]
        self.support_version_map = {
            "A": ["AA", "AB"],
            "B": ["BB", "BCD", "BFD"],
            "C": ["CL"],
            "D": ["DD"],
            "E": ["EE", "EA"],
            "F": ["FF", "FE", "FG", "FFF", "F1"],
            "G": ["GG"],
            "H": ["HZ", "HP"],
            "I": ["I"],
            "J": ["JJ", "JP"],
        }
        self.dst_version = ["ABC", "10JK", "JQK", "10JQKA"]
        self.help_info = "使用说明:\n1、read me\n"
        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
        self.SetExtraStyle(wx.FRAME_EX_METAL)
        self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENU))
        table = wx.GridBagSizer(0, 0)
        table.SetFlexibleDirection(wx.BOTH)
        table.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_SPECIFIED)
        label_file = wx.StaticText(self, wx.ID_ANY, '文件路径', wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_RIGHT)
        label_file.Wrap(-1)
        label_file.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, '微软雅黑'))
        table.Add(label_file, wx.GBPosition(1, 1), wx.GBSpan(1, 1), wx.ALL, 5)
        self.file = wx.FilePickerCtrl(self, wx.ID_ANY, wx.EmptyString, '选择文件', '*.*', wx.DefaultPosition,
                               
### 使用PythonwxPython实现五子棋的完整代码示例 以下是基于PythonwxPython框架的一个简单五子棋游戏的完整代码示例。此代码实现了基本的游戏逻辑,包括棋盘绘制、玩家轮流下棋以及胜利条件检测等功能。 ```python import wx class GomokuFrame(wx.Frame): def __init__(self, parent=None, id=-1, title="Gomoku"): super(GomokuFrame, self).__init__(parent, id, title, size=(400, 400)) self.board_size = 15 # 棋盘大小 self.grid_size = 25 # 单格尺寸 self.margin = 20 # 边距 self.current_player = 1 # 当前玩家,默认为黑方 self.board = [[0 for _ in range(self.board_size)] for _ in range(self.board_size)] self.Bind(wx.EVT_PAINT, self.on_paint) self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down) def on_paint(self, event): dc = wx.PaintDC(self) dc.SetBrush(wx.Brush("WHITE")) dc.Clear() # 绘制棋盘网格线 for i in range(self.board_size): dc.DrawLine(self.margin, self.margin + i * self.grid_size, self.margin + (self.board_size - 1) * self.grid_size, self.margin + i * self.grid_size) dc.DrawLine(self.margin + i * self.grid_size, self.margin, self.margin + i * self.grid_size, self.margin + (self.board_size - 1) * self.grid_size) # 绘制已下的棋子 for row in range(self.board_size): for col in range(self.board_size): if self.board[row][col] == 1: # 黑子 dc.SetBrush(wx.Brush("BLACK")) dc.DrawCircle(self.margin + col * self.grid_size, self.margin + row * self.grid_size, 10) elif self.board[row][col] == 2: # 白子 dc.SetBrush(wx.Brush("WHITE")) dc.DrawCircle(self.margin + col * self.grid_size, self.margin + row * self.grid_size, 10) def on_left_down(self, event): x, y = event.GetPosition() col = int((x - self.margin) / self.grid_size) row = int((y - self.margin) / self.grid_size) if 0 <= row < self.board_size and 0 <= col < self.board_size and self.board[row][col] == 0: self.board[row][col] = self.current_player if self.check_win(row, col): # 胜利检测 wx.MessageBox(f"Player {self.current_player} wins!", "Game Over", style=wx.OK | wx.ICON_INFORMATION) self.current_player = 3 - self.current_player # 切换玩家 self.Refresh() def check_win(self, row, col): directions = [(1, 0), (0, 1), (1, 1), (1, -1)] # 方向定义 player = self.board[row][col] for dx, dy in directions: count = 1 r, c = row + dx, col + dy while 0 <= r < self.board_size and 0 <= c < self.board_size and self.board[r][c] == player: count += 1 r += dx c += dy r, c = row - dx, col - dy while 0 <= r < self.board_size and 0 <= c < self.board_size and self.board[r][c] == player: count += 1 r -= dx c -= dy if count >= 5: return True return False if __name__ == "__main__": app = wx.App(False) frame = GomokuFrame() frame.Show(True) app.MainLoop() ``` 上述代码展示了如何利用`wxPython`库创建一个简单的五子棋游戏界面[^4]。其中包含了棋盘绘制功能、鼠标点击事件绑定以及胜利条件检测的核心逻辑。 #### 关键点解析 - **棋盘初始化**:通过二维列表`board`存储当前棋局的状态,初始值均为0表示空白位置。 - **绘图方法**:重写`on_paint`函数完成棋盘背景与棋子的动态渲染。 - **交互机制**:监听左键点击事件(`EVT_LEFT_DOWN`)计算落子坐标并更新棋盘数据结构。 - **胜负判定**:遍历四个主要方向(水平、垂直、两个斜角),统计连续相同颜色棋子的数量是否达到或超过五个[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值