python6程序:写一个画图软件

wxPython是一个基于Python的GUI开发框架,它使用了wxWidgets C++库作为其底层GUI引擎,提供了一套跨平台的GUI工具包。wxPython模块可以帮助开发者快速地构建桌面应用程序,支持Windows、Linux和MacOS等多个平台。

wxPython模块提供了丰富的GUI组件,例如按钮、文本框、标签、菜单、对话框等,同时也支持自定义组件的开发。开发者可以使用wxPython模块创建各种类型的应用程序,例如文本编辑器、图形界面工具、音频播放器等。

用wx 模块写一个画图软件,代码如下,中文释义以写出(代码生成有问题,做个参考,改不动了。。):

   import wx
class PaintWindow(wx.Window):
def init(self, parent):
super().init(parent)
self.SetBackgroundColour("WHITE") # 设置窗口背景颜色为白色
self.color = "BLACK" # 默认颜色为黑色
self.thickness = 1 # 默认线条粗细为1
self.pen = wx.Pen(self.color, self.thickness, wx.SOLID) # 创建画笔对象
self.lines = [] # 存储绘制的所有线条
self.curLine = [] # 存储当前正在绘制的线条
self.pos = (0, 0) # 当前鼠标位置
self.InitBuffer()self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)  # 绑定鼠标左键按下事件
    self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)  # 绑定鼠标左键释放事件
    self.Bind(wx.EVT_MOTION, self.OnMotion)  # 绑定鼠标移动事件
    self.Bind(wx.EVT_SIZE, self.OnSize)  # 绑定窗口大小改变事件
    self.Bind(wx.EVT_IDLE, self.OnIdle)  # 绑定窗口空闲事件
    self.Bind(wx.EVT_PAINT, self.OnPaint)  # 绑定窗口重绘事件

def InitBuffer(self):
    size = self.GetClientSize()  # 获取窗口尺寸
    self.buffer = wx.Bitmap(size.width, size.height)  # 创建缓存位图
    dc = wx.BufferedDC(None, self.buffer)  # 创建缓存设备上下文
    dc.SetBackground(wx.Brush(self.GetBackgroundColour()))  # 设置背景刷
    dc.Clear()  # 清空缓存设备上下文
    self.DrawLines(dc)  # 绘制已保存的线条
    self.reInitBuffer = False

def GetLinesData(self):
    return self.lines[:]

def SetLinesData(self, lines):
    self.lines = lines[:]
    self.InitBuffer()
    self.Refresh()

def OnLeftDown(self, event):
    self.curLine = []
    self.pos = event.GetPosition()
    self.CaptureMouse()

def OnLeftUp(self, event):
    if self.HasCapture():
        self.lines.append((self.color, self.thickness, self.curLine))
        self.curLine = []
        self.ReleaseMouse()

def OnMotion(self, event):
    if event.Dragging() and event.LeftIsDown():
        dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
        self.drawMotion(dc, event)
    event.Skip()

def drawMotion(self, dc, event):
    dc.SetPen(self.pen)
    newPos = event.GetPosition()
    coords = (self.pos, newPos)
    self.curLine.append(coords)
    dc.DrawLine(*coords)
    self.pos = newPos

def OnSize(self, event):
    self.reInitBuffer = True

def OnIdle(self, event):
    if self.reInitBuffer:
        self.InitBuffer()
        self.Refresh()

def OnPaint(self, event):
    dc = wx.BufferedPaintDC(self, self.buffer)

def DrawLines(self, dc):
    for color, thickness, line in self.lines:
        pen = wx.Pen(color, thickness, wx.SOLID)
        dc.SetPen(pen)
        for coords in line:
            dc.DrawLine(*coords)class PaintFrame(wx.Frame):
def init(self):
super().init(None, title="Simple Paint")
self.paintWindow = PaintWindow(self)# 创建菜单栏
    menubar = wx.MenuBar()

    # 创建文件菜单
    fileMenu = wx.Menu()
    saveItem = fileMenu.Append(-1, "&Save\tCtrl+S", "Save the current drawing")
    menubar.Append(fileMenu, "&File")

    # 创建颜色菜单
    colorMenu = wx.Menu()
    colors = ["BLACK", "RED", "GREEN", "BLUE"]
    colorRadioGroup = wx.Menu()

    for index, color in enumerate(colors):
        colorRadioGroup.AppendRadioItem(-1, color)
    colorMenu.Append(-1, "&Color", colorRadioGroup)  # 将颜色选项添加到菜单中
    menubar.Append(colorMenu, "&Color")

    # 创建线条粗细菜单
    thicknessMenu = wx.Menu()
    thicknesses = ["1", "2", "4", "8"]
    thicknessRadioGroup = wx.Menu()

    for index, thickness in enumerate(thicknesses):
        thicknessRadioGroup.AppendRadioItem(-1, thickness)
    thicknessMenu.Append(-1, "&Thickness", thicknessRadioGroup)  # 将线条粗细选项添加到菜单中
    menubar.Append(thicknessMenu, "&Thickness")

    self.SetMenuBar(menubar)  # 将菜单栏设置到窗口

    statusBar = self.CreateStatusBar()  # 创建状态栏

    saveFileDialog = wx.FileDialog(
        self,
        "Save As",
        "",
        "",
        "PNG (*.png)|*.png",
        wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT,
    )

    def OnSave(event):
        if saveFileDialog.ShowModal() == wx.ID_OK:
            filename = saveFileDialog.GetPath()
            bitmap = self.paintWindow.buffer.ConvertToImage()
            bitmap.SaveFile(filename, wx.BITMAP_TYPE_PNG)

    def OnColor(event):
        selectedColorIndex = colorRadioGroup.GetSelection()
        selectedColor = colors[selectedColorIndex]
        self.paintWindow.color = selectedColor
        self.paintWindow.pen = wx.Pen(
            self.paintWindow.color, self.paintWindow.thickness, wx.SOLID
        )

    def OnThickness(event):
        selectedThicknessIndex = thicknessRadioGroup.GetSelection()
        selectedThickness = thicknesses[selectedThicknessIndex]
        self.paintWindow.thickness = int(selectedThickness)
        self.paintWindow.pen = wx.Pen(
            self.paintWindow.color, self.paintWindow.thickness, wx.SOLID
        )

    self.Bind(wx.EVT_MENU, OnSave, saveItem)
    self.Bind(wx.EVT_RADIOGROUP, OnColor, colorRadioGroup)
    self.Bind(wx.EVT_RADIOGROUP, OnThickness, thicknessRadioGroup)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self.paintWindow, 1, wx.EXPAND)
    self.SetSizer(sizer)
if name == "main":
app = wx.App()
frame = PaintFrame()
frame.Show()
app.MainLoop()

发表回复