本文整理汇总了Python中tkinter.Canvas.cget方法的典型用法代码示例。如果您正苦于以下问题:Python Canvas.cget方法的具体用法?Python Canvas.cget怎么用?Python Canvas.cget使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tkinter.Canvas
的用法示例。
在下文中一共展示了Canvas.cget方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import cget [as 别名]
class Painter:
def __init__(
self, root, width=800, height=600, offset=5, min_radius=5, max_radius=10, num_balls=20, refresh_speed=5
):
# Draw frame etc
self.app_frame = Frame(root)
self.app_frame.pack()
self.canvas = Canvas(self.app_frame, width=width, height=height)
self.canvas_size = (int(self.canvas.cget("width")), int(self.canvas.cget("height")))
self.canvas.pack()
self.refresh_speed = refresh_speed
# Work area
self.min_x = offset
self.max_x = width - offset
self.min_y = offset
self.max_y = height - offset
self.num_balls = num_balls
self.balls = []
self.ball_handles = dict()
self.init_balls(max_radius, min_radius, num_balls)
self.time = 0
self.wall_collision_times = np.zeros(num_balls)
self.init_wall_collision_times()
self.ball_collision_times = np.zeros((num_balls, num_balls))
self.init_ball_collision_times()
self.draw()
self.refresh()
return
def init_balls(self, max_radius, min_radius, num_balls):
for i in np.arange(num_balls):
while True:
radius = (max_radius - min_radius) * rand.random_sample() + min_radius
ball_min_x = self.min_x + radius
ball_max_x = self.max_x - radius
x = (ball_max_x - ball_min_x) * rand.random_sample() + ball_min_x
ball_min_y = self.min_y + radius
ball_max_y = self.max_y - radius
y = (ball_max_y - ball_min_y) * rand.random_sample() + ball_min_y
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):
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import cget [as 别名]
class Painter:
#__init__ performs the construction of a Painter object
def __init__(self, root, scale=500, border=5, refresh_speed=5, filename='balls.txt', min_radius=5, max_radius=10, num_balls=20):
#width and height are used to set the simulation borders
width = scale + border
height = scale + border
#Time is the time stamp for the simulation; it is set to 0 to indicate the beginning of the simulation
self.time = 0
#setup will set up the necessary graphics window and the ball list
self.setup(root, width, height, border, refresh_speed)
#Check the input parameter 'filename' to load predetermined simulation
#otherwise set up the default simulation
if filename is None:
self.init_balls(max_radius, min_radius, num_balls)
self.num_balls = num_balls
else:
self.num_balls = self.read_balls(scale, filename)
#Create the priority data structure
self.PQ = ArrayPQ(self.num_balls)
#Initialize all possible collision times
self.init_ball_collision_times()
self.init_wall_collision_times()
#draw will draw the graphics to the window
self.draw()
#refresh is a loop method intended to create animations
self.refresh()
#A blank return indicates the end of the function
return
#setup creates the window to display the graphics along with the red border
#of the simulation
def setup(self, root, width, height, border, refresh_speed):
# Draw frame etc
self.app_frame = Frame(root)
self.app_frame.pack()
self.canvas = Canvas(self.app_frame, width = width, height = height)
self.canvas_size = (int(self.canvas.cget('width')), int(self.canvas.cget('height')))
self.canvas.pack()
self.refresh_speed = refresh_speed
# Work area
self.min_x = border
self.max_x = width - border
self.min_y = border
self.max_y = height - border
#create array to hold the n number of balls
self.balls = []
self.ball_handles = dict()
return
#This function reads in predefined ball numbers and locations to implement predetermined simulations
def read_balls(self, scale, filename):
f = open(filename)
num_balls = int(f.readline().strip())
for l in f:
ll = l.strip().split(" ")
x = scale*float(ll[0])
y = scale*float(ll[1])
vx = scale*float(ll[2])
vy = scale*float(ll[3])
radius = scale*float(ll[4])
mass = float(ll[5])
r = int(ll[6])
g = int(ll[7])
b = int(ll[8])
tk_rgb = "#%02x%02x%02x" % (r, g, b)
new_ball = Ball(radius, x, y, vx, vy, mass, tk_rgb)
self.balls.append(new_ball)
return num_balls
#init_balls will create an array of size "num_balls" stored within self.balls
def init_balls(self, max_radius, min_radius, num_balls):
for i in np.arange(num_balls):
while(True):
radius = (max_radius - min_radius) * rand.random_sample() + min_radius
ball_min_x = self.min_x + radius
ball_max_x = self.max_x - radius
x = (ball_max_x - ball_min_x)*rand.random_sample() + ball_min_x
ball_min_y = self.min_y + radius
ball_max_y = self.max_y - radius
y = (ball_max_y - ball_min_y)*rand.random_sample() + ball_min_y
vx = rand.random_sample()
vy = rand.random_sample()
mass = 1.0 # 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
#init_wall_collision_times will set all of the balls' minimum collision time
#for both horizontal and vertical walls and store that time in their respective arrays
def init_wall_collision_times(self):
for i in np.arange(len(self.balls)):
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import cget [as 别名]
#.........这里部分代码省略.........
case = self.plateau[i][j]
if case is not None: # afficher le pion
self.plateau_affiche.create_rectangle(
i * self.cote_case, j * self.cote_case,
(i + 1) * self.cote_case, (j + 1) * self.cote_case,
outline=case, fill=case
)
else:
self.plateau_affiche.create_rectangle(
i * self.cote_case, j * self.cote_case,
(i + 1) * self.cote_case, (j + 1) * self.cote_case,
outline=couleur_fond, fill=couleur_fond
)
# tracer le contour des pièces
# 1) tracer les séparations entre deux pièces adjacentes de
# couleurs différentes dans la même colonne
for i in range(0, self.dim_plateau[0]): # par défaut 10
colonne = self.plateau[i]
for j in range(1, self.dim_plateau[1]): # par défaut 16
if colonne[j - 1] != colonne[j]:
self.plateau_affiche.create_rectangle(
(i) * self.cote_case, j * self.cote_case,
(i + 1) * self.cote_case, j * self.cote_case,
outline=couleur_fond, fill=couleur_fond, width=1
)
# 2) tracer les séparations entre deux pièces adjacentes de
# couleurs différentes dans la même ligne
for i in range(1, self.dim_plateau[0]): # par défaut 10
for j in range(0, self.dim_plateau[1]): # par défaut 16
if self.plateau[i - 1][j] != self.plateau[i][j]:
self.plateau_affiche.create_rectangle(
(i) * self.cote_case, j * self.cote_case,
(i) * self.cote_case, (j + 1) * self.cote_case,
outline=couleur_fond, fill=couleur_fond, width=1
)
def clicPlateau(self, event):
"""Récupère les coordonnées de la case sélectionnée, et joue le coup
correspondant s'il est permis."""
# remarque: le canevas de tkinter interprète (i, j) géométriquement
# (au lieu de (ligne, colonne)), d'où l'inversion de coordonnées dans
# la ligne ci-dessous
(i, j) = (event.x // self.cote_case, event.y // self.cote_case)
if self.plateau[i][j] is not None:
piece = set()
detecterPiece(self.plateau, i, j, piece, self.plateau[i][j])
#print (piece)
if len(piece) > 1: # si la pièce est valide, on la retire
# retirer la piece en mettant ses cases à None
for (p, q) in piece:
self.plateau[p][q] = None
# faire descendre les blocs situés au-dessus de la pièce
mettreAJour(self.plateau, piece)
# tasser le restant du plateau en supprimant les colonnes vides
eliminerColonnesVides(self.plateau)
# rafraîchir le plateau pour répercuter les modifications
self.rafraichirPlateau()
self.rafraichirNombreBlocs(piece)
self.compteur_de_coups += 1
self.compteCoups(self.compteur_de_coups)
messagevictoire = partieFinie(self.plateau)
if messagevictoire:
self.plateau_affiche.create_text(
int(self.plateau_affiche.cget("width")) // 2,
self.cote_case // 2,
text=messagevictoire,
font=tkinter.font.Font(
family="Courier", size=12, weight=tkinter.font.BOLD
),
fill="red"
)
def reinitialiserJeu(self):
"""Réinitialise le plateau de jeu et les scores."""
self.reinitialiserPlateau()
self.rafraichirNombreBlocs()
# réinitialiser le nombre de coups
self.compteur_de_coups = 0
self.nb_coups_affiche.delete(ALL)
self.nb_coups_affiche.create_text(self.largeur_plateau // 2, self.cote_case // 2, text="Coups effectués: " + str(self.compteur_de_coups), fill="black")
def reinitialiserPlateau(self):
"""Réinitialise le plateau de jeu."""
# réinitialiser la matrice
self.plateau = initialiserPlateau(*self.dim_plateau)
# réinitialiser l'affichage
self.plateau_affiche.delete(ALL)
if self.plateau:
self.rafraichirPlateau()