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


Python Canvas.after方法代码示例

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


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

示例1: loo_lumi

# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import after [as 别名]
def loo_lumi(aken, lume_pilt, kingi_pilt):
    """Create new snow, gifts and make both move with another function."""
    lumi = Canvas(aken, bg="Blue", highlightthickness=0, width=20, height=20)
    pilt = [lume_pilt, kingi_pilt]
    pilt = random.choice(pilt)
    positsioon_x = random.randint(0, 780)
    lumi.create_image(9, 9, image=pilt)
    lumi.pack()
    lumi.after(500, loo_lumi, aken, lume_pilt, kingi_pilt)
    lumesadu(aken, lumi, positsioon_x, 120)
开发者ID:rener2,项目名称:python-course,代码行数:12,代码来源:NeljasIseseisevYlesanne.py

示例2: Visual

# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import after [as 别名]

#.........这里部分代码省略.........
        self.canvas = Canvas(self._animationPane,
                             width=self.canvas_w,
                             height=self.canvas_h,
                             background="black")

        self.canvas.grid(row=1, column=0, columnspan=2)

    def _setup(self):
        '''Method for 'Setup' button.'''
        # Clearing the canvas and reset the go button
        self.canvas.delete('all')
        self._goButton['relief'] = 'raised'
        self.N = int(self._N.get())
        self.Ticks = int(self._Ticks.get())
        self.similar = float(self._Similar.get())
        self.data = []
        self.tick_counter = 0
        self._Tick_counter1['text'] = str(self.tick_counter)
        self._plot_setup(self.Ticks)
        self.grid_size = self.N
        self.world = World(750, 750, self.grid_size)
        self.create_turtles()
        self.neighbouring_turtles()
        self.draw_turtles()

    def _go(self):
        '''Method for the 'Go' button, i.e. running the simulation.'''
        self._goButton['relief'] = 'sunken'
        if self.tick_counter <= self.Ticks:
            self._Tick_counter1['text'] = str(self.tick_counter)
            self.canvas.update()

            self._graph.update()
            self._graph.after(0)

            # Data collection
            turtles_unhappy = self.check_satisfaction()
            prop_happy, prop_unhappy = self.calc_prop_happy(self.tick_counter)

            self.data_collection(self.tick_counter, prop_happy, prop_unhappy)

            if self.tick_counter >= 1:

                # HAPPY values (%)
                x0 = self.tick_counter-1
                x1 = self.tick_counter

                # Collecting values from stored data
                y0 = self.data[self.tick_counter-1][1]
                y1 = self.data[self.tick_counter][1]

                # Transforming to tkinter
                x1, y1 = self.trans.screen(x1, y1)
                x0, y0 = self.trans.screen(x0, y0)
                self._graph.create_line(x0, y0, x1, y1,
                                        fill="green", width=1.3,
                                        tag="happy")  # Draw "happy lines

                # UNHAPPY values (%)
                x0 = self.tick_counter-1
                x1 = self.tick_counter

                # Collecting values from stored data
                y0 = self.data[self.tick_counter-1][2]
                y1 = self.data[self.tick_counter][2]
开发者ID:cyneo,项目名称:feminism,代码行数:69,代码来源:schelling2.py

示例3: Visual

# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import after [as 别名]

#.........这里部分代码省略.........
        elif quarter <= v < 2*quarter:  # Yellow -> Green
            r = int(255*max(0, (1-(v-quarter)/quarter)))
            g = 255
        elif 2*quarter <= v < 3*quarter:  # Green -> Blue
            r = 0
            g = int(255*max(0, (1-(v-2*quarter)/quarter)))
            b = int(255*min(((v-2*quarter)/quarter), 1))
        else:  # Blue -> Purple
            g = 0
            r = int(255*min(((v-3*quarter)/quarter), 1))
            b = 255
        return "#{:02x}{:02x}{:02x}".format(r, g, b)

    def draw_random_point(self):
        """Draw randomly colored point on screen"""
        fill = self.get_fill(self.start_counter % 100)
        self.start_counter += 1
        x = random.randint(0, self.width)
        y = random.randint(0, self.height)
        self.canvas.create_rectangle(x, y, x+2, y+2, width=0, fill=fill)

    def timer_fired(self):
        """Called every frame refresh"""
        if self.mode == Mode.START:
            for _ in range(10):
                self.draw_random_point()
        if self.mode == Mode.MAIN and not self.paused:
            self.update_locations()
            self.redraw_all()

    def timer(self):
        """Setup timer loop"""
        self.timer_fired()
        self.canvas.after(self.timer_delay, self.timer)

    def init_animation(self):
        """Initialize or reset animation"""
        self.users = []
        self.timer_delay = 100
        self.start_counter = 0
        self.versions = 40
        self.default_users = 20
        self.default_coaches = 5
        self.default_students = 5
        self.max_users = 100
        self.version = None
        self.paused = False
        self.mode = Mode.START
        self.draw_start_screen()
        self.error_text = None
        self.error_font_size = 20
        self.version_select = None
        self.timer()

    def start_infection(self):
        """Initialize users and start infections"""
        num_users_text = self.num_users_entry.get()
        num_coaches_text = self.num_coaches_entry.get()
        num_students_text = self.num_students_entry.get()
        try:
            error = "Invalid Number of Users"
            num_users = int(num_users_text)
            if not (1 <= num_users <= self.max_users):
                raise ValueError
            num_coaches = int(num_coaches_text)
            error = "Invalid Number of Coaches"
开发者ID:rmaratos,项目名称:infection,代码行数:70,代码来源:visual.py

示例4: __init__

# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import after [as 别名]

#.........这里部分代码省略.........
                bj = self.balls[j]
                tij = bi.ball_collision_time(bj)
                self.PQ.insert(i, j, tij + self.time, self.balls[i].count, self.balls[j].count)
                # self.ball_collision_times[i][j] = tij
                # self.ball_collision_times[j][i] = tij
        return
    
    #update collision times is meant to update collision times of ball i with all
    #walls (horizontal and vertical) and all other balls within the PQ array
    def update_collision_times(self, i):
        bi = self.balls[i]
        tix = bi.horizontal_wall_collision_time(self.min_x, self.max_x)
        tiy = bi.vertical_wall_collision_time(self.min_y, self.max_y)
        self.PQ.insert(i, -1, tix + self.time,self.balls[i].count, -1)
        self.PQ.insert(-1, i, tiy + self.time, -1, self.balls[i].count)
        for j in np.arange(self.num_balls):
            bj = self.balls[j]
            tij = bi.ball_collision_time(bj) + self.time
            if i > j:
                self.PQ.insert(j, i, tij,self.balls[j].count,self.balls[i].count)
            else:
                self.PQ.insert(i, j, tij,self.balls[i].count, self.balls[j].count)
        return

    #draw will draw the borders and all balls within self.balls
    def draw(self):
        #Draw walls
        self.canvas.create_line((self.min_x, self.min_y), (self.min_x, self.max_y), fill = "red")
        self.canvas.create_line((self.min_x, self.min_y), (self.max_x, self.min_y), fill = "red")
        self.canvas.create_line((self.min_x, self.max_y), (self.max_x, self.max_y), fill = "red")
        self.canvas.create_line((self.max_x, self.min_y), (self.max_x, self.max_y), fill = "red")
        #Draw balls        
        for b in self.balls:
            obj = self.canvas.create_oval(b.x - b.radius, b.y - b.radius, b.x + b.radius, b.y + b.radius, outline=b.tk_rgb, fill=b.tk_rgb)
            self.ball_handles[b] = obj
        self.canvas.update()


    #refresh is called to update the state of the simulation
    #-each refresh call can be considered one iteration of the simulation
    #-all balls will be moved and if there is a collision then it will be computed
    def refresh(self):
        #get the next collision
        i, j, t, num_collisions_i, num_collision_j  = self.PQ.get_next()
        #gather the current collisions of the ith and jth ball
        current_collisions_i = self.balls[i].count
        current_collisions_j = self.balls[j].count
        
        #Check the difference in time between the predicted collision time and 
        #the current time stamp of the simulation
        delta = t - self.time
        #If the difference is greater than 1, then just move the balls
        if delta > 1.0:
            # cap delta to 1.0
            for bi in self.balls:
                bi.move()
                self.canvas.move(self.ball_handles[bi], bi.vx, bi.vy)
            self.time += 1.0
        #Otherwise a collision has occurred
        else:
            #Move all balls
            for bi in self.balls:
                bi.move(delta)
                self.canvas.move(self.ball_handles[bi], bi.vx*delta, bi.vy*delta)
            #increment the simulation time stamp
            self.time += delta
            #Delete the top element within the Priority Queue
            self.PQ.delete()
            #if i is -1 then this indicates a collision with a vertical wall
            #also this if statement checks if the number of collisions recorded 
            #when the collision returned by PQ.get_next() is equal to the 
            #number of collisions within the jth ball
            #this acts as a test to check if the collision is still valid
            if i == -1 and num_collision_j == current_collisions_j:
                #compute what happens from the vertical wall collision
                self.balls[j].collide_with_vertical_wall()
                #update collision times for the jth ball
                self.update_collision_times(j)
            #if j is -1 then this indicates a collision a horizontal wall
            #while also checking if the number of collisions match
            #to see if the collision is valid
            elif j == -1 and num_collisions_i == current_collisions_i:
                #compute what happens from the horizontal wall collision
                self.balls[i].collide_with_horizontal_wall()
                #update collision times for the ith ball
                self.update_collision_times(i)
            #Otherwise i and j are not equal to -1 indicating that the collision is between two balls
            #check if no collisions have occurred between both balls before the collision returned from PQ.get_next
            #if true then this means that the collision is still valid and must be executed
            elif num_collision_j == current_collisions_j and num_collisions_i == current_collisions_i:
                #Execute collision across the ith and jth ball                
                self.balls[i].collide_with_ball(self.balls[j])
                #update collision times for both the ith and jth ball
                self.update_collision_times(i)
                self.update_collision_times(j)
                
        #update the canvas to draw the new locations of each ball
        self.canvas.update()

        self.canvas.after(self.refresh_speed, self.refresh) #Calls the function again
开发者ID:sid55,项目名称:Abstract-Data-Types---Python,代码行数:104,代码来源:discrete_event.py

示例5: Tk

# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import after [as 别名]
            can.delete(line1[int(y/50)])
            can.delete(line2[int(y/50)])
            line1[int(y/50)] = [can.create_line(pos1, fill='#000')]
            line2[int(y/50)] = [can.create_line(pos2, fill='#f00',
                                dash=(3, 2))]
        can.update()

"""
본문
캔버스를 선언하고 원과 선들을 초기화
"""
master = Tk()
can = Canvas(master, width=500, height=500)
obj = can.create_oval(240, 240, 260, 260, fill='#aafcdf')
can.pack()

# 라인 초기화
line1 = [can.create_line(0, 0, 1, 1)]*11
line2 = [can.create_line(0, 0, 1, 1)]*11

# 애니메이션 구동
can.after(0, web)
onTarget = False

# bind: 미리 정해져 있는 입력에 따라서 함수를 실행
# 사용자의 드래그 동작은 클릭과 클릭한 후 이동으로 구성되어 있다.
can.bind('<Button-1>', onClick)
can.bind('<B1-Motion>', onDrag)

master.mainloop()
开发者ID:soma0sd,项目名称:python-study,代码行数:32,代码来源:05_drag_object.py

示例6: __init__

# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import after [as 别名]
class Gem:
	def __init__(self):
		self.frame = Tk();
		self.frame.resizable(False, False)

		self.status = 1
		self.scorePlayerA = 0
		self.scorePlayerB = 0

		self.scoreRoundA = 0
		self.scoreRoundB = 0

		self.countRound = 0
		self.quitMatch = 0
		self.isQuitRound = 0

		self.canvas_after_2 = 0
		self.canvas_after_1 = 0
		self.isPause = 0

		#register event
		self.frame.bind("<F4>", self.quitGame)
		self.frame.bind("<F5>", self.pauseGame)
		self.frame.protocol("WM_DELETE_WINDOW", self.on_closing)

		self.registerKeyboard()

	def setName(self, title):
		self.frame.title(title)

	def setBall(self, ball):
		self.ball = ball

	def setLeftBar(self, bar):
		self.leftBar = bar

	def setRightBar(self, bar):
		self.rightBar = bar

	def run(self):
		self.frame.mainloop()

	def getFrame(self):
		return self.frame;

	def getCanvas(self):
		return self.canvas

	def setSize(self, size):
		self.frame.geometry(("%dx%d")%(size[0], size[1]))
		self.frame.update()

	def getSize(self):
		return (self.frame.winfo_width(), self.frame.winfo_height())

	def setBackground(self, color):
		self.background = color

	def setPlayers(self, players):
		self.players = players

	def setScoreBoard(self):
		players = self.players
		size = self.getSize()
		mid = round(size[0]/2)
		# Board
		self.canvas.create_rectangle(mid - 100, 0, mid + 100, 35, fill="grey58", outline="white", tag="boarda")

		# Player name 1
		self.canvas.create_text(mid - 80, 15, text=players[0], fill="magenta2", tag="boardb")

		# Round score 1
		r1 = players[0]+"a"
		self.canvas.create_text(mid - 80, 28, text="0", fill="pale green", tag="scoreplayera")

		# Player name 2
		self.canvas.create_text(mid + 80, 15, text=players[1], fill="magenta2", tag="boardc")

		# Round score 2
		self.canvas.create_text(mid + 80, 28, text="0", fill="pale green", tag="scoreplayerb")

		# Box score 1
		self.canvas.create_rectangle(mid - 50, 5, mid - 10, 25, fill="thistle3", outline="white", tag="boardd")

		# Score 1
		self.canvas.create_text(mid - 30, 15, text="000", fill="cyan", tag=players[0])

		# Box score 2
		self.canvas.create_rectangle(mid + 10, 5, mid + 50, 25, fill="thistle3", outline="white", tag="boarde")

		# Score 2
		self.canvas.create_text(mid + 30, 15, text="000", fill="cyan", tag=players[1])

		self.canvas.pack()
		self.frame.update()

	def clearScoreBoard(self):
		self.canvas.delete(self.players[0])
		self.canvas.delete(self.players[1])
		self.canvas.delete("boarda")
#.........这里部分代码省略.........
开发者ID:DHTTeam,项目名称:raspberrypi2,代码行数:103,代码来源:gem.py

示例7: __init__

# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import after [as 别名]

#.........这里部分代码省略.........

                vx = rand.random_sample()
                vy = rand.random_sample()

                mass = rand.random_sample()
                new_ball = Ball(radius, x, y, vx, vy, mass)

                if not new_ball.check_overlap(self.balls):
                    self.balls.append(new_ball)
                    break

    def draw(self):
        # Draw walls
        self.canvas.create_line((self.min_x, self.min_y), (self.min_x, self.max_y), fill="red")
        self.canvas.create_line((self.min_x, self.min_y), (self.max_x, self.min_y), fill="red")
        self.canvas.create_line((self.min_x, self.max_y), (self.max_x, self.max_y), fill="red")
        self.canvas.create_line((self.max_x, self.min_y), (self.max_x, self.max_y), fill="red")
        # Draw balls
        for b in self.balls:
            obj = self.canvas.create_oval(b.x - b.radius, b.y - b.radius, b.x + b.radius, b.y + b.radius)
            self.ball_handles[b] = obj
        self.canvas.update()

    def next_wall_collision_idx(self):
        collided = []
        for i in np.arange(self.num_balls):
            if self.wall_collision_times[i] <= self.time:
                collided.append(i)
        return collided

    def next_ball_collision_idx(self):
        min_i = 0
        min_j = 0
        collided = []
        # min_tij = float('inf')
        for i in np.arange(self.num_balls):
            for j in np.arange(i, self.num_balls):
                tij = self.ball_collision_times[i][j]
                if tij <= self.time:
                    collided.append((i, j))
        return collided

    # CODE YOU NEED TO IMPLEMENT
    def init_wall_collision_times(self):
        for i in np.arange(self.num_balls):
            self.wall_collision_times[i] = self.balls[i].compute_wall_collision_time(
                self, self.min_x, self.max_x, self.min_y, self.max_y
            )

    #    def update_wall_collision_time(self, i):
    #
    def init_ball_collision_times(self):
        for i in np.arange(self.num_balls):
            for j in np.arange(self.num_balls):
                self.ball_collision_times[i] = self.balls[i].compute_ball_collision_time(self, other)

    #
    #    def update_ball_collision_time(self, i):

    # Do not update this
    def refresh(self):
        wall_i = self.next_wall_collision_idx()
        ball_i = self.next_ball_collision_idx()
        # print wall_i, ball_i
        for i in wall_i:
            bi = self.balls[i]
            old_x = bi.x
            old_y = bi.y
            bi.collide_with_wall(self.min_x, self.max_x, self.min_y, self.max_y)
            self.canvas.move(self.ball_handles[bi], bi.x - old_x, bi.y - old_y)

        for (i, j) in ball_i:
            bi = self.balls[i]
            bj = self.balls[j]
            bi.collide_with_ball(bj)

        collided = wall_i
        for (i, j) in ball_i:
            collided.append(i)
            collided.append(j)

        collided = set(collided)
        # print collided

        for i in collided:
            self.update_ball_collision_time(i)
            self.update_wall_collision_time(i)

        not_collided = set(np.arange(self.num_balls)) - collided

        for i in not_collided:
            bi = self.balls[i]
            old_x = bi.x
            old_y = bi.y
            bi.move()
            self.canvas.move(self.ball_handles[bi], bi.x - old_x, bi.y - old_y)

        self.time += 1
        self.canvas.update()
        self.canvas.after(self.refresh_speed, self.refresh)  # Calls the function again
开发者ID:obiejuan,项目名称:101,代码行数:104,代码来源:collision.py


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