⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 draw4.py

📁 Python.Tkinter编程实例代码多多学习
💻 PY
📖 第 1 页 / 共 2 页
字号:
        if self.curFunc:
            if self.regular and self.curFunc in \
                   [self.func['oval'], self.func['rect'],
                    self.func['foval'],self.func['frect']]:
                dx = self.lastx - self.startx
                dy = self.lasty - self.starty
                delta = max(dx, dy)
                self.lastx = self.startx + delta
                self.lasty = self.starty + delta
            self.curFunc(self.startx, self.starty, self.lastx,
                 self.lasty, self.prevx, self.prevy, self.foreground,
                 self.background, self.fillStyle, self.lineWidth,
                 self.lineData)
            self.inGrab      = FALSE
            self.releaseGrab = TRUE
            self.growFunc    = None
            self.storeObject(self.startx, self.starty, self.lastx,
                 self.lasty, self.prevx, self.prevy, self.curFunc, 
                 self.foreground, self.background, self.fillStyle,
                 self.lineWidth, self.lineData)
        else:
            if self.inGrab:
                tags = self.canvas.gettags(CURRENT)
                for tag in tags:
                    if '*' in tag:
                        key, value = string.split(tag, '*')
                        var = transDict[key]
                        setattr(self, var, string.atoi(value))
                x1,y1,x2,y2, px, py, self.growFunc, \
                    fg,bg,fill,lwid,ld = self.objects[self.uniqueID]
                if self.boundX == 1 and self.adjX:
                    x1 =  x1 + self.lastx-self.prevx
                elif self.boundX == 2 and self.adjX:
                    x2 =  x2 + self.lastx-self.prevx
                if self.boundY == 1 and self.adjY:
                    y1 = y1 + self.lasty-self.prevy
                elif self.boundY == 2 and self.adjY:
                    y2 = y2 + self.lasty-self.prevy
                self.growFunc(x1,y1,x2,y2,px,py,fg,bg,fill,lwid,ld)
                self.storeObject(x1,y1,x2,y2,px,py,self.growFunc,
                                 fg,bg,fill,lwid,ld)
                self.addGrabHandles(self.uniqueID, self.uniqueID)
            if self.selObj:
                self.canvas.itemconfig(self.selObj,
                                       width=self.savedWidth)
        self.canvas.config(cursor='arrow')
        
    def addGrabHandles(self, objectID, tag):
        self.canvas.delete("grabHandle")
        self.canvas.dtag("grabHandle")
        self.canvas.dtag("grab")                
        self.canvas.dtag("grabbedObject")                

        self.canvas.addtag("grab", "withtag", CURRENT)
        self.canvas.addtag("grabbedObject", "withtag", CURRENT)
        x1,y1,x2,y2 = self.canvas.bbox(tag)
        for x,y, curs, tagBx, tagBy, tagX, tagY in [
                (x1,y1,TLC,            'bx*1','by*1','x*1','y*1'),
                (x2,y1,TRC,            'bx*2','by*1','x*1','y*1'),
                (x1,y2,BLC,            'bx*1','by*2','x*1','y*1'),
                (x2,y2,BRC,            'bx*2','by*2','x*1','y*1'),
                (x1+((x2-x1)/2),y1,TS, 'bx*0','by*1','x*0','y*1'),
                (x2,y1+((y2-y1)/2),RS, 'bx*2','by*0','x*1','y*0'),
                (x1,y1+((y2-y1)/2),LS, 'bx*1','by*0','x*1','y*0'),
                (x1+((x2-x1)/2),y2,BS, 'bx*0','by*2','x*0','y*1')]:
            ghandle = self.canvas.create_rectangle(x-2,y-2,x+2,y+2,
                      outline='black', fill='black', tags=('grab',
                      'grabHandle','%s'%tagBx,'%s'%tagBy,'%s'%tagX,
                      '%s'%tagY,'%s'%objectID))
            self.canvas.tag_bind(ghandle, '<Any-Enter>',
                      lambda e, s=self, c=curs: s.setCursor(e,c))
            self.canvas.tag_bind(ghandle, '<Any-Leave>',
                                 self.resetCursor)
            self.canvas.lift("grab")

    def setCursor(self, event, cursor):
        self.savedCursor = self.canvas.cget('cursor')
        self.canvas.config(cursor=cursor)

    def resetCursor(self, event):
        if self.releaseGrab and self.savedCursor:
            self.canvas.config(cursor=self.savedCursor)  

    def drawLine(self,x,y,x2,y2,x3,y3,fg,bg,fillp,wid,ld):
        self.canvas.delete(self.curObject)
        self.curObject = self.canvas.create_line(x,y,x2,y2,
                tags=self.uniqueID, fill=fg,stipple=fillp,width=wid)

    def drawFree(self,x,y,x2,y2,x3,y3,fg,bg,fillp,wid,ld):
        self.drawFreeSmooth(x,y,x2,y2,x3,y3,FALSE,fg,bg,fillp,wid,ld)

    def drawSmooth(self,x,y,x2,y2,x3,y3,fg,bg,fillp,wid,ld):
        self.drawFreeSmooth(x,y,x2,y2,x3,y3,TRUE,fg,bg,fillp,wid,ld)

    def drawFreeSmooth(self,x,y,x2,y2,x3,y3,smooth,fg,bg,fillp,
                       wid,ld):
        if not ld:
            for coord in [[x3, y3, x2, y2], [x2, y2]][smooth]:
                self.lineData.append(coord)
                ild = self.lineData
        else:
            ild = ld
        if len(ild) > 2:
            self.curObject = self.canvas.create_line(ild, fill=fg,
              stipple=fillp,tags=self.uniqueID,width=wid,smooth=smooth)

    def drawRect(self,x,y,x2,y2,x3,y3,fg,bg,fillp,wid,ld):
        self.drawFilledRect(x,y,x2,y2,x3,y3,fg,'',fillp,wid,ld)
        
    def drawFilledRect(self,x,y,x2,y2,x3,y3,fg,bg,fillp,wid,ld):
        self.canvas.delete(self.curObject)
        self.curObject = self.canvas.create_rectangle(x,y,x2,y2,
                   tags=self.uniqueID,outline=fg,fill=bg,
                   stipple=fillp, width=wid)

    def drawOval(self,x,y,x2,y2,x3,y3,fg,bg,fillp,wid,ld):
        self.drawFilledOval(x,y,x2,y2,x3,y3,fg,'',fillp,wid,ld)
        
    def drawFilledOval(self,x,y,x2,y2,x3,y3,fg,bg,fillp,wid,ld):
        self.canvas.delete(self.curObject)
        self.curObject = self.canvas.create_oval(x,y,x2,y2,
                   tags=self.uniqueID,outline=fg,fill=bg,
                   stipple=fillp,width=wid)

    def storeObject(self, x1,y1,x2,y2,px,py,func,fg,bg,fill,lwid,ld):
        self.objects[self.uniqueID] = ( x1,y1,x2,y2,px,py,func,fg,bg,
                                        fill,lwid,ld )

    def redraw(self):
        # **** Delete all tags in canvas first ****
        self.canvas.delete(ALL)
        keys = self.objects.keys()
        keys.sort()
        for key in keys:
            startx, starty, lastx, lasty, prevx, prevy, func, \
                    fg, bg, fill, lwid , ld= self.objects[key]
            self.curObject = None
            self.uniqueID = key
            func(startx, starty, lastx, lasty, prevx, prevy,
                 fg, bg, fill, lwid, ld)

    def newDrawing(self):
        self.canvas.delete(ALL)
        self.initData()

    def openDrawing(self):
        ofile = askopenfilename(filetypes=[("PTkP Draw", "ptk"),
                                           ("All Files", "*")])
        if ofile:
            self.currentName = ofile
            self.initData()
            fd = open(ofile)
            items = marshal.load(fd)
            for i in range(items):
                self.uniqueID, x1,y1,x2,y2,px,py,cfunc, \
                     fg,bg,fill,lwid,ld = marshal.load(fd)
                self.storeObject(x1,y1,x2,y2,px,py,self.func[cfunc],
                                 fg,bg,fill,lwid,ld)
            fd.close()
        self.redraw()
        
    def saveDrawing(self):
        self.doSave()

    def saveAsDrawing(self):
        ofile = asksaveasfilename(filetypes=[("PTkP Draw", "ptk"),
                                             ("All Files", "*")])
        if ofile:
            self.currentName = ofile
            self.doSave()
            
    def doSave(self):
        fd = open(self.currentName, 'w')
        keys = self.objects.keys()
        keys.sort()
        marshal.dump(len(keys), fd)
        for key in keys:
            startx, starty, lastx, lasty, prevx, prevy, func, \
                    fg, bg, fill, lwid , ld= self.objects[key]
            cfunc = self.transFunc[func]
            marshal.dump((key, startx, starty, lastx, lasty, prevx, \
                          prevy, cfunc, fg, bg, fill, lwid , ld), fd)
        fd.close()
        
    def initData(self):
        self.curFunc     = self.drawLine
        self.growFunc    = None
        self.curObject   = None
        self.selObj      = None
        self.lineData   = []
        self.savedWidth  = 1
        self.savedCursor = None
        self.objects     = {}       # Now a dictionary
        self.foreground  = 'black'
        self.background  = 'white'
        self.fillStyle   = None
        self.lineWidth   = 1
        self.serial      = 1000
        self.regular     = FALSE
        self.inGrab      = FALSE
        self.releaseGrab = TRUE
        self.currentName = 'Untitled'

    def ipostscript(self):
        postscript = self.canvas.postscript()
        fd = open('drawing.ps', 'w')
        fd.write(postscript)
        fd.close()
        
    def close(self):
        self.quit()

    def createInterface(self):
        AppShell.AppShell.createInterface(self)
        self.createButtons()
        self.initData()
        self.createMenus()
        self.createBase()
        self.createTools()
        self.createLineWidths()
        self.createLineColors()
        self.createFillColors()
        self.createPatterns()
        
if __name__ == '__main__':
    draw = Draw()
    draw.run()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -