当前位置: 首页>>代码示例>>Python>>正文


Python Canvas.create_text方法代码示例

本文整理汇总了Python中Canvas.create_text方法的典型用法代码示例。如果您正苦于以下问题:Python Canvas.create_text方法的具体用法?Python Canvas.create_text怎么用?Python Canvas.create_text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Canvas的用法示例。


在下文中一共展示了Canvas.create_text方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Plot

# 需要导入模块: import Canvas [as 别名]
# 或者: from Canvas import create_text [as 别名]
class Plot(object):
    '''
    A canvas will be created where the selected values will be plotted as lines, with the best fitting scale.
    
    Initial version by:
        Michel Heiniger and Sandra Lang
    
    Latest source code can be found at:
        https://github.com/nebelriss/FluidX
    '''
    
    
    def __init__(self, master, frame, sw, sh):
        '''
        Constructor for the plot area.
        '''
        # initialization of 
        self.sw = sw
        self.sh = sh
        self.frame = frame
        
        # empty value list
        self.value_list = []
        
        # canvas area where the value lines and the coordinate system
        self.canvas = Canvas(self.frame, bg = "white")
        self.canvas.pack(expand = YES, fill = BOTH)
        
        #if the used os is a windows or a mac, they need a factor 1.5 for the y-axis
        if os.name == "nt":
            self.win_osx_factor = 1.5
        elif os.name == "mac":
            self.win_osx_factor = 1.5
            
        # if its a linux machine factor is 1           
        else:
            self.win_osx_factor = 1
        
        
        
    def createCoordSystem(self):
        '''
        This creates the coordinate system with a given max value for x,y axis.
        '''
        # Interval for the numbers 
        self.xInterval = 2
        self.yInterval = 0.2
        
        
        # delete all canvas with the tag "text"
        self.canvas.delete("text")                
        
        
        # draw axis lines
        self.canvas.create_line(self.sw-315, self.sh-150, 50, self.sh-150, fill = "black", width = 1)
        self.canvas.create_line(70, self.sh-130, 70, 70, fill = "black", width = 1)
        
        
        # text for Axis
        y = 'Y-Axis'
        x = 'X-Axis'
        # write the text at the right position
        self.canvas.create_text(70,40, text= y, tag = "text")
        self.canvas.create_text(self.sw-315,self.sh-120, text= x, tag = "text")
        
        
        # numbers for x and y axis
        maximumx = ((self.xMax) + 2) / self.xInterval
        maximumy = ((self.yMax) + 1) / self.yInterval
        
        
        # loop to write the numbers under the axis line and draw the separation lines for the x-axis
        for i in range(int(maximumx)):
            x = 70 + (i * self.dist_x * self.xInterval)
            self.canvas.create_line(x,self.sh-150,x,self.sh-155, width = 2)
            self.canvas.create_text(x,self.sh-140, text='%d'% (i * self.xInterval), anchor=N, tag = "text")   
        
        # loop to write the numbers under the axis line and draw the separation lines for the x-axis      
        for i in range (int(maximumy)):
            y = self.sh-150-(i * self.dist_y * self.yInterval) * self.win_osx_factor  
            self.canvas.create_line(70,y,75,y, width = 2)
            self.canvas.create_text(45,y, text=str((i * self.yInterval)), anchor=W, tag = "text")
        
        
            
    def createline (self, meta, values, idx, sel_idx, colors):
        '''
        Fist the given values are written in a list with the index number of the listbox and the selected items.
        '''
        # remove all lines with the tag "plot"
        self.canvas.delete("plot")
        
        
        # overwrite values with the given index with none
        try:
            self.value_list[idx][1] = None
        except IndexError:
            print "Index Error - List out of range"
  

#.........这里部分代码省略.........
开发者ID:nebelriss,项目名称:FluidX,代码行数:103,代码来源:plot.py

示例2: SudokuUI

# 需要导入模块: import Canvas [as 别名]
# 或者: from Canvas import create_text [as 别名]
class SudokuUI(Frame):
    """
    The Tkinter UI, responsible for drawing the board and accepting user input.
    """
    def __init__(self, parent, game):
        self.game = game
        Frame.__init__(self, parent)
        self.parent = parent

        self.row, self.col = -1, -1

        self.__initUI()

    def __initUI(self):
        self.parent.title("Sudoku")
        self.pack(fill=BOTH)
        self.canvas = Canvas(self,
                             width=WIDTH,
                             height=HEIGHT)
        self.canvas.pack(fill=BOTH, side=TOP)
        clear_button = Button(self,
                              text="Clear answers",
                              command=self.__clear_answers)
        clear_button.pack(fill=BOTH, side=BOTTOM)

        self.__draw_grid()
        self.__draw_puzzle()

        self.canvas.bind("<Button-1>", self.__cell_clicked)
        self.canvas.bind("<Key>", self.__key_pressed)

    def __draw_grid(self):
        """
        Draws grid divided with blue lines into 3x3 squares
        """
        for i in xrange(10):
            color = "blue" if i % 3 == 0 else "gray"

            x0 = MARGIN + i * SIDE
            y0 = MARGIN
            x1 = MARGIN + i * SIDE
            y1 = HEIGHT - MARGIN
            self.canvas.create_line(x0, y0, x1, y1, fill=color)

            x0 = MARGIN
            y0 = MARGIN + i * SIDE
            x1 = WIDTH - MARGIN
            y1 = MARGIN + i * SIDE
            self.canvas.create_line(x0, y0, x1, y1, fill=color)

    def __draw_puzzle(self):
        self.canvas.delete("numbers")
        for i in xrange(9):
            for j in xrange(9):
                answer = self.game.puzzle[i][j]
                if answer != 0:
                    x = MARGIN + j * SIDE + SIDE / 2
                    y = MARGIN + i * SIDE + SIDE / 2
                    original = self.game.start_puzzle[i][j]
                    color = "black" if answer == original else "sea green"
                    self.canvas.create_text(
                        x, y, text=answer, tags="numbers", fill=color
                    )

    def __draw_cursor(self):
        self.canvas.delete("cursor")
        if self.row >= 0 and self.col >= 0:
            x0 = MARGIN + self.col * SIDE + 1
            y0 = MARGIN + self.row * SIDE + 1
            x1 = MARGIN + (self.col + 1) * SIDE - 1
            y1 = MARGIN + (self.row + 1) * SIDE - 1
            self.canvas.create_rectangle(
                x0, y0, x1, y1,
                outline="red", tags="cursor"
            )

    def __draw_victory(self):
        # create a oval (which will be a circle)
        x0 = y0 = MARGIN + SIDE * 2
        x1 = y1 = MARGIN + SIDE * 7
        self.canvas.create_oval(
            x0, y0, x1, y1,
            tags="victory", fill="dark orange", outline="orange"
        )
        # create text
        x = y = MARGIN + 4 * SIDE + SIDE / 2
        self.canvas.create_text(
            x, y,
            text="You win!", tags="victory",
            fill="white", font=("Arial", 32)
        )

    def __cell_clicked(self, event):
        if self.game.game_over:
            return
        x, y = event.x, event.y
        if (MARGIN < x < WIDTH - MARGIN and MARGIN < y < HEIGHT - MARGIN):
            self.canvas.focus_set()

            # get row and col numbers from x,y coordinates
#.........这里部分代码省略.........
开发者ID:More4me,项目名称:AI,代码行数:103,代码来源:GUI.py


注:本文中的Canvas.create_text方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。