本文整理汇总了Python中tkinter.Canvas.itemconfig方法的典型用法代码示例。如果您正苦于以下问题:Python Canvas.itemconfig方法的具体用法?Python Canvas.itemconfig怎么用?Python Canvas.itemconfig使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tkinter.Canvas
的用法示例。
在下文中一共展示了Canvas.itemconfig方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw_automata
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import itemconfig [as 别名]
def draw_automata(turing_machine=None):
w = len(turing_machine.states)
h = len(turing_machine.rules)
root = Tk()
frame = Frame(root, width=800, height=600)
frame.grid(row=0, column=0)
canvas = Canvas(frame, bg='#FFFFFF', width=800, height=600,
scrollregion=(0, -h * 15, w * 100, h * 15*3))
hbar = Scrollbar(frame, orient=HORIZONTAL)
hbar.pack(side=BOTTOM, fill=X)
hbar.config(command=canvas.xview)
vbar = Scrollbar(frame, orient=VERTICAL)
vbar.pack(side=RIGHT, fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=800, height=600)
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.pack(side=LEFT, expand=True, fill=BOTH)
for position, state in enumerate(turing_machine.states):
state_position[state] = position
loop_occurrences[position] = 0
canvas_id = canvas.create_text(10 + 80 * position, 400, anchor="nw")
canvas.itemconfig(canvas_id, text="state-")
canvas.insert(canvas_id, 12, "%d" % state)
counter = 1
for rule in turing_machine.rules:
counter = draw_arrow(state_position[rule.current_state],
state_position[rule.next_state],
canvas, counter, rule)
root.mainloop()
示例2: __init__
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import itemconfig [as 别名]
class Visualize:
def __init__(self, solution):
self.solution = solution
self.time = 0
self.full = solution.full
self.shiftRight = 20
self.shiftDown = 20
self.separation = 90
self.scale = 60
self.textVShift = 10
self.root = Tk()
self.canvas = Canvas(self.root)
self.canvas.pack(fill=BOTH, expand=YES)
self.root.bind("<KeyRelease>", self.key_released)
self.timeLabel = Label(self.root, text='time_lavel')
self.timeLabel.pack(side=RIGHT)
self.setup()
self.draw()
if self.full:
mainloop()
def key_released(self, event):
if event.char == 'j':
if self.time < self.solution.maxt:
self.time = self.time + 1
self.draw()
elif event.char == 'k':
if self.time > 0:
self.time = self.time - 1
self.draw()
elif event.char == 'q':
self.root.quit()
def getBBox(self, v):
return (self.shiftRight + self.separation*v[0],
self.shiftDown + self.separation*v[1],
self.shiftRight + self.separation*v[0] + self.scale,
self.shiftDown + self.separation*v[1] + self.scale)
def getCenter(self, v):
return (self.shiftRight + self.separation*v[0] + self.scale / 2,
self.shiftDown + self.separation*v[1] + self.scale / 2)
def getStatusPos(self, v):
(x,y) = self.getCenter(v)
return (x,y - self.textVShift)
def getDecisionPos(self, v):
(x,y) = self.getCenter(v)
return (x,y + self.textVShift)
def getEdgePos(self, e):
v0 = self.getCenter(e[0])
v1 = self.getCenter(e[1])
if v0[0] == v1[0]:
if v0[1] < v1[1]:
return (v0[0], v0[1] + self.scale / 2), (v1[0], v1[1] - self.scale / 2)
else:
return (v0[0], v0[1] - self.scale / 2), (v1[0], v1[1] + self.scale / 2)
elif v0[1] == v1[1]:
if v0[0] < v1[0]:
return (v0[0] + self.scale / 2, v0[1]), (v1[0] - self.scale / 2, v1[1])
else:
return (v0[0] - self.scale / 2, v0[1]), (v1[0] + self.scale / 2, v1[1])
return v0, v1
def setup(self):
self.nodeStatus = {}
self.nodeDecision = {}
for v in self.solution.nodes:
self.canvas.create_oval(self.getBBox(v))
self.nodeStatus[v] = self.canvas.create_text(self.getStatusPos(v), text="asfs")
self.nodeDecision[v] = self.canvas.create_text(self.getDecisionPos(v), text="fs")
self.edges = {}
for e in self.solution.edges:
self.canvas.create_line(self.getEdgePos(e), fill='gray')
self.edges[e] = self.canvas.create_line(self.getEdgePos(e), arrow='last',
state=HIDDEN)
def draw(self):
# quick reference
nstat = self.solution.nstat
command = self.solution.command
self.timeLabel.config(text = '%r' % self.time)
t = self.time
for v in self.solution.nodes:
self.canvas.itemconfig(self.nodeStatus[v], text=nstat[v,self.time])
self.canvas.itemconfig(self.nodeDecision[v], text=command[v,self.time])
if not self.full:
return
occu = self.solution.occu
for e in self.solution.edges:
state = HIDDEN
#.........这里部分代码省略.........
示例3: AppLogic
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import itemconfig [as 别名]
#.........这里部分代码省略.........
threading.Thread.__init__(self)
self.turn = 0
self.update = False
self.x = -1
self.y = -1
self.start()
def run(self):
self.game_gui = Canvas(self.root, width=600, height=600, background='green')
self.game_gui.bind("<Button-1>", self.click)
self.game_gui.focus_set()
self.game_gui.bind("<Key>", self.key)
self.game_gui.pack()
for i in range(1, 8):
self.game_gui.create_line(0, i*75, 600, i*75)
self.game_gui.create_line(i*75, 0, i*75, 600)
self.pieces = []
for i in range(8):
self.pieces.append([])
for j in range(8):
self.pieces[i].append(self.game_gui.create_oval(i*75+5, j*75+5, (i+1)*75-5, (j+1)*75-5, fill="green", outline="green"))
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
self.root.resizable(0,0)
self.running = True
config = EvaluateConfig()
tf_util.update_memory(config.gpu_mem_fraction)
AIPlayer.create_if_nonexistant(config)
self.game = Othello()
if(random() > 0.5):
self.human = 1
else:
self.human = -1
ai = create_player(config.model_1, config)
#print("You are playing against", config.model_1)
#print("Playing games with %d simulations per move" % config.game.simulation_num_per_move)
self.side = -1
self.draw_board()
self.value = ai.evaluate(self.game, self.side)
while self.running and not self.game.game_over():
#play move
if self.side != self.human:
self.value = ai.evaluate(self.game, self.side)
self.root.title("Othello (Thinking of Move) Current Value: %0.2f (1 white wins, -1 black wins)" % self.value)
self.root.config(cursor="wait")
t = ai.pick_move(self.game, self.side)
self.game.play_move(t[0], t[1], self.side)
self.draw_board()
self.side *= -1
self.value = ai.evaluate(self.game, self.side)
else:
if len(self.game.possible_moves(self.side)) == 0:
self.side *= -1
continue
if self.side == -1:
color = "black"
else:
color = "white"
self.root.title("Othello (Play as %s) Current Value: %0.2f (1 white wins, -1 black wins)" % (color, self.value))
self.root.config(cursor="")
if self.update:
self.update = False
if (self.x, self.y) in self.game.possible_moves(self.side):
self.game.play_move(self.x, self.y, self.side)
self.draw_board()
self.side *= -1
time.sleep(0.01)
self.root.config(cursor="")
if self.human == self.game.get_winner():
self.root.title("Othello (You Win!)")
elif self.game.get_winner() == 0:
self.root.title("Othello (Its a draw!)")
else:
self.root.title("Othello (You Lose!)")
def key(self, event):
if event.char == "z":
self.human *= -1
def click(self, event):
self.game_gui.focus_set()
if self.human == self.side and not self.update:
if self.x != event.x//75 or self.y != event.y//75:
self.update = True
self.x = event.x//75
self.y = event.y//75
def on_closing(self):
self.running = False
self.root.destroy()
def draw_board(self):
for i in range(8):
for j in range(8):
if self.game.board[i, j] == 1:
self.game_gui.itemconfig(self.pieces[i][j], fill="white")
if self.game.board[i, j] == -1:
self.game_gui.itemconfig(self.pieces[i][j], fill="black")
示例4: __init__
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import itemconfig [as 别名]
#.........这里部分代码省略.........
self.setScoreBoard()
self.quitMatch = 0
self.isQuitRound = 0
self.countRound = 0
self.nextRound(1)
def resetRound(self):
self.scorePlayerA = 0
self.scorePlayerB = 0
self.isQuitRound = 0
self.updateScorePlayer()
self.leftBar.registerKeyboard(self.frame)
self.rightBar.registerKeyboard(self.frame)
def update(self, status):
self.updateScorePlayer(status-3)
self.canvas.update()
if self.quitRound(status) == 1 and self.quitMatch == 0:
self.leftBar.unRegisterKeyboard(self.frame)
self.rightBar.unRegisterKeyboard(self.frame)
self.canvas_after_2 = self.canvas.after(800, self.nextRound, status)
if self.quitMatch == 1:
self.leftBar.unRegisterKeyboard(self.frame)
self.rightBar.unRegisterKeyboard(self.frame)
self.isQuitRound == 1
return self.quitMatch
def updateScoreRound(self, status):
if self.scorePlayerB == 0 and self.scorePlayerA == 0:
return
if status == 0:
self.scoreRoundA += 1
self.canvas.itemconfig("scoreplayera", text=self.scoreRoundA)
elif status == 1:
self.scoreRoundB += 1
self.canvas.itemconfig("scoreplayerb", text=self.scoreRoundB)
def updateScorePlayer(self, status = 5):
if status == 0:
self.scorePlayerA += 1
self.canvas.itemconfig(self.players[0], text=self.__countScore(self.scorePlayerA))
if self.scorePlayerA != 0 and self.scorePlayerA % 5 == 0:
self.ball.speed +=1
elif status == 1:
self.scorePlayerB += 1
self.canvas.itemconfig(self.players[1], text=self.__countScore(self.scorePlayerB))
elif status == 5:
self.canvas.itemconfig(self.players[0], text=self.__countScore(self.scorePlayerA))
self.canvas.itemconfig(self.players[1], text=self.__countScore(self.scorePlayerB))
self.canvas.update()
def __countScore(self, score):
if score < 10:
scorestr = "00" + str(score);
elif score < 100:
scorestr = "0" + str(score)
else:
scorestr = str(score)
return scorestr
def stopMatch(self, event = 0):
self.isQuitRound = 1
self.quitMatch = 1
self.leftBar.unRegisterKeyboard(self.frame)
示例5: View
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import itemconfig [as 别名]
class View():
def __init__(self, root, controller):
self.__controller = controller
root.wm_title("Bomber")
self.__windowsystem = root.call('tk', 'windowingsystem')
self.__frame = root
self.__canvas = Canvas(self.__frame, width=int(CANVAS_WIDTH),
height=int(CANVAS_HEIGHT), bg="white")
self.__canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
self.__init_fonts()
self.__init_arena()
self.__init_score()
self.__block_views = [] # type: List[BlockView]
self.__blockfield_view = BlockfieldView()
self.__messages = []
def __init_fonts(self):
self.bigfont = font.nametofont("TkDefaultFont")
self.bigfont.configure(size=int(48))
self.scorefont = font.nametofont("TkDefaultFont")
self.scorefont.configure(size=int(20))
def __init_score(self):
self.score_text = self.__canvas.create_text(5, 5, anchor="nw")
self.__canvas.itemconfig(self.score_text, text="Score:", font=self.scorefont)
def __init_arena(self):
self.__canvas.create_rectangle(LEFT_OFFSET, TOP_OFFSET,
LEFT_OFFSET + MAXCOL*GRID_SIZE,
TOP_OFFSET+MAXROW*GRID_SIZE, fill="black")
nextblocktext = self.__canvas.create_text(GRID_SIZE,
TOP_OFFSET + GRID_SIZE * 4, anchor="nw")
self.__canvas.itemconfigure(nextblocktext, text="Next:",
font=self.bigfont, fill="black")
self.__autoplay_text = self.__canvas.create_text(LEFT_OFFSET + GRID_SIZE * 5,
TOP_OFFSET - GRID_SIZE, anchor="c")
self.__canvas.itemconfigure(self.__autoplay_text, text="Play mode",
font=self.bigfont, fill="black")
def register_block(self, block):
block_view = BlockView(block)
self.__block_views.append(block_view)
def unregister_block(self, block):
for block_view in self.__block_views:
if block_view.block is block:
block_view.erase(self.__canvas)
self.__block_views.remove(block_view)
def update_blockfield(self, blockfield):
self.__blockfield_view.redraw(self.__canvas, blockfield)
def display_score(self):
self.__canvas.itemconfig(self.score_text, text="Score: " + str(self.__controller.score),
font=self.scorefont)
def show_autoplay(self, autoplay):
if autoplay:
self.__canvas.itemconfig(self.__autoplay_text, text="Auto-play mode",
font=self.scorefont, fill="black")
else:
self.__canvas.itemconfig(self.__autoplay_text, text="Manual mode",
font=self.scorefont, fill="black")
def game_over(self):
text1 = self.__canvas.create_text(LEFT_OFFSET + GRID_SIZE*MAXCOL//2,
CANVAS_HEIGHT/2, anchor="c")
text2 = self.__canvas.create_text(LEFT_OFFSET + GRID_SIZE*MAXCOL//2,
CANVAS_HEIGHT/2 + 100, anchor="c")
text1_shadow = self.__canvas.create_text(2 + LEFT_OFFSET + GRID_SIZE*MAXCOL//2,
2 + CANVAS_HEIGHT/2, anchor="c")
text2_shadow = self.__canvas.create_text(2 + LEFT_OFFSET + GRID_SIZE*MAXCOL//2,
2 + CANVAS_HEIGHT/2 + 100, anchor="c")
self.__messages.append(text1)
self.__messages.append(text2)
self.__messages.append(text1_shadow)
self.__messages.append(text2_shadow)
self.__canvas.itemconfig(text1, text="GAME OVER!",
font=self.bigfont, fill="white")
self.__canvas.itemconfig(text2, text="Press r to play again.",
font=self.scorefont, fill="white")
self.__canvas.itemconfig(text1_shadow, text="GAME OVER!",
font=self.bigfont, fill="black")
self.__canvas.itemconfig(text2_shadow, text="Press r to play again.",
font=self.scorefont, fill="black")
self.__canvas.tag_raise(text1)
self.__canvas.tag_raise(text2)
def clear_messages(self):
for txt in self.__messages:
self.__canvas.delete(txt)
self.__messages.clear()
def update(self):
for block_view in self.__block_views:
block_view.redraw(self.__canvas)
self.display_score()
示例6: World
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import itemconfig [as 别名]
#.........这里部分代码省略.........
if (x, y) not in list(self.robots.keys()):
return 0
return len(self.robots[(x, y)])
def crash(self, x1, y1, x2, y2):
if 0 in (x1, y1, x2, y2):
return True
if (x2, y2) in list(self.walls.keys()) and \
(x1, y1) in self.walls[(x2, y2)]:
return True
if (x1, y1) in list(self.walls.keys()) and \
(x2, y2) in self.walls[(x1, y1)]:
return True
return False
def add_infinite_beepers(self, x, y):
flag = (x, y) not in list(self.beepers.keys()) or \
self.beepers[(x, y)] is 0
self.beepers[(x, y)] = INFINITY
text = "oo"
a = self.t + x * self.t
b = self.n - (self.t + y * self.t)
t = self.t / 3
if flag:
self.ovals[(x, y)] = self.canvas.create_oval(a-t, b-t, a+t,
b+t, fill="black")
self.numbers[(x, y)] = self.canvas.create_text(a, b, text=text,
fill="white",
font=("Times",
max(-self.t/2,
-20),
""))
else:
self.canvas.itemconfig(self.numbers[(x, y)], text=text)
if (x, y) in list(self.robots.keys()):
for robot in self.robots[(x, y)]:
robot.lift()
def add_beeper(self, x, y):
if (x, y) in list(self.beepers.keys()) and \
self.beepers[(x, y)] is INFINITY:
return
flag = (x, y) not in list(self.beepers.keys()) or \
self.beepers[(x, y)] is 0
if flag:
self.beepers[(x, y)] = 1
else:
self.beepers[(x, y)] += 1
text = str(self.beepers[(x, y)])
a = self.t + x * self.t
b = self.n - (self.t + y * self.t)
t = self.t / 3
if flag:
self.ovals[(x, y)] = self.canvas.create_oval(a-t, b-t, a+t, b+t,
fill="black")
self.numbers[(x, y)] = self.canvas.create_text(a, b, text=text,
fill="white",
font=("Times",
max(-self.t/2,
-20),
""))
else:
self.canvas.itemconfig(self.numbers[(x, y)], text=text)
if (x, y) in list(self.robots.keys()):
for robot in self.robots[(x, y)]:
robot.lift()
示例7: Feedback
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import itemconfig [as 别名]
class Feedback(object):
def __init__(self, master, device):
self.master = master
self.device = device
self.frame = tk.Frame(master)
self.on_update = False
self.mapped_actuators = {}
self.quitButton = tk.Button(
self.frame, text='Quit', width=25, command=self.close_window)
self.quitButton.pack()
self.master.protocol("WM_DELETE_WINDOW", self.close_window)
self.canvas = Canvas(master, width=250, height=250)
self.canvas.pack()
self.set_mapping()
lc.log.debug("device actuators: {}".format(self.device.actuators))
self.frame.pack()
self.closed = False
def set_mapping(self):
cntr_x = 250 / 2
cntr_y = 250 / 2
straight = 90
wd = 8
self.north = self.canvas.create_line(
cntr_x, cntr_y - 8, cntr_x, cntr_y - straight, width=wd)
self.east = self.canvas.create_line(
cntr_x + 8, cntr_y, cntr_x + straight, cntr_y, width=wd)
self.south = self.canvas.create_line(
cntr_x, cntr_y + 8, cntr_x, cntr_y + straight, width=wd)
self.west = self.canvas.create_line(
cntr_x - 8, cntr_y, cntr_x - straight, cntr_y, width=wd)
if len(self.device.actuators) == 8:
diag = straight * 0.75
self.north_east = self.canvas.create_line(
cntr_x + 8, cntr_y - 8, cntr_x + diag, cntr_y - diag, width=wd)
self.south_east = self.canvas.create_line(
cntr_x + 8, cntr_y + 8, cntr_x + diag, cntr_y + diag, width=wd)
self.south_west = self.canvas.create_line(
cntr_x - 8, cntr_y + 8, cntr_x - diag, cntr_y + diag, width=wd)
self.north_west = self.canvas.create_line(
cntr_x - 8, cntr_y - 8, cntr_x - diag, cntr_y - diag, width=wd)
self.mapped_actuators = {
self.device.actuators[0]: self.east,
self.device.actuators[1]: self.north_east,
self.device.actuators[2]: self.north,
self.device.actuators[3]: self.north_west,
self.device.actuators[4]: self.west,
self.device.actuators[5]: self.south_west,
self.device.actuators[6]: self.south,
self.device.actuators[7]: self.south_east
}
else:
self.mapped_actuators = {
self.device.actuators[0]: self.east,
self.device.actuators[1]: self.north,
self.device.actuators[2]: self.west,
self.device.actuators[3]: self.south,
}
def update(self):
for actuator in self.mapped_actuators:
self.canvas.itemconfig(
self.mapped_actuators[actuator],
fill=color_from_level(actuator.level))
def close_window(self):
self.closed = True
self.master.destroy()
示例8: Board
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import itemconfig [as 别名]
#.........这里部分代码省略.........
direction="L"
if (dirJ==self.player): direction="R"
if(dirI==self.player): direction+="B"
return(direction)
def enfOfGame(self):
end=dr.checkEndOfGame(self.logicBoard,self.player)
if (end is False): return #le jeu n'est pas fini
self.guiBoard.unbind("<Button -1>")
if(end!=0):
winner="RED PLAYER" if end==1 else "BLUE PLAYER"
text=winner + " WON"
else:
text = "DRAW"
FixedLabel(self,text,16,6)
def changePlayer(self,cellId,i,j):
"""
:param cellId: Id du dernier pion joué
:param i: coordonnée de la ligne
:param j: coordonnée de la colonn
"""
self.player=-self.player
val="RED" if self.player ==1 else "BLUE"
self.currentVariableLabel.setVar(val)
self.hasPlayed=False
self.hasCaptured=False
dr.becomeKing(self.logicBoard,j,i)
self.enfOfGame()
if (dr.isKing(self.logicBoard,j,i)): self.guiBoard.itemconfig(cellId,outline="gold",width=2)
def coordToLetter(self,j):
"""
:param j: coordonée de la colonne
:return: coordonnée de la colonne en format lettre
"""
return(chr(int(j)+97))
def moveGuiPiece(self,i,j,ancientSelectedCellTags,ancientSelectedCellCoordI,ancientSelectedCellCoordJ,ancientSelectedCellId,playerColor):
"""
:param i: coordonnée de la ligne
:param j: coordonnée de la colonn
:param ancientSelectedCellTags: tags de la case précédemment sélectionnée
:param ancientSelectedCellCoordI: coordonnée x de la case précédemment sélectionnée
:param ancientSelectedCellCoordJ: coordonnée y de la case précédemment sélectionnée
:param ancientSelectedCellId: id de la case précédemment sélectionnée
:param playerColor: couleur du joueur (red ou blue)
"""
length=self.findLength(j,i,ancientSelectedCellTags)#chercher la longueur du mouvement
if (not(length)): return
#Le mouvement n'est pas autorisé, case d'arrivée n'est pas sur la diagonale
direction=self.findDirection(j,i,ancientSelectedCellTags)#chercher la destination
errCode=dr.checkMove(self.logicBoard,ancientSelectedCellCoordI,ancientSelectedCellCoordJ,direction,self.player,moves=length,hasPlayed=self.hasPlayed,hasCaptured=self.hasCaptured)
if (errCode==NO_ERROR):
#Le mouvement est autorisé
dest,cap=dr.movePiece(self.logicBoard,ancientSelectedCellCoordI,ancientSelectedCellCoordJ,direction,moves=length)
示例9: select
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import itemconfig [as 别名]
def select(self, canvas: Canvas, status=True):
self.selected = status
border = 'black' if self.selected else self.color
canvas.itemconfig(self.id, outline=border)
示例10: __init__
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import itemconfig [as 别名]
#.........这里部分代码省略.........
414, 10, width=2)
self.canvas.create_line(354, 275 - cos(2*pi/360*deg)*leng,
354, 10, width=2)
self.canvas.create_line(354, 10, 414, 10, width=2)
self.canvas.pack()
def animate(self):
while (self.machine.current_state not in self.machine.accept_states) and\
(self.machine.current_state not in self.machine.reject_states):
try:
self.step()
except TclError:
return
self.finalise()
return
def step(self):
if self.machine.current_state in self.machine.accept_states or\
self.machine.current_state in self.machine.reject_states:
self.finalise()
return
left_from_head = ''
right_from_head = ''
head = str(self.machine.tape.middle)
for i in self.machine.tape.right:
right_from_head += str(i)
for i in self.machine.tape.left:
left_from_head += str(i)
if len(right_from_head) > 8:
right_from_head = right_from_head[:8]
elif len(right_from_head) < 8:
right_from_head = right_from_head + '_'*(8 - len(right_from_head))
if len(left_from_head) > 8:
left_from_head = left_from_head[-8:]
elif len(left_from_head) < 8:
left_from_head = '_'*(8 - len(left_from_head)) + left_from_head
text = left_from_head + head + right_from_head
for i in range(17):
if self.tape_symbol[i]:
self.canvas.delete(self.tape_symbol[i])
self.tape_symbol[i] = self.canvas.create_text(10 + i * 44, 275,
text="%c" % text[i],
font=("Default",
30),
anchor="nw")
stack_text = ''
for i in range(min(5, len(self.machine.stack))):
if self.machine.stack[i] is not None:
stack_text += self.machine.stack[i]
else:
stack_text = '_' + stack_text
for i in range(min(5, len(self.machine.stack))):
if self.stack_symbol[i]:
self.canvas.delete(self.stack_symbol[i])
self.stack_symbol[i] = self.canvas.create_text(714, 10+i*44,
text="%c" %
stack_text[i],
font=("Default",
30),
anchor="nw")
rule = self.machine.step()
if rule is None:
self.finalise()
return
else:
self.canvas.delete(self.text_rule)
self.text_rule = self.canvas.create_text(54 + 374-sin(pi/4)*150,
275 - cos(pi/4)*150,
text="%s" % rule,
font=("Default", 12),
anchor="nw")
self.canvas.update()
def finalise(self):
stack_id = self.canvas.create_text(150, 350, font=("Default", 30),
anchor='nw')
self.canvas.itemconfig(stack_id)
self.canvas.insert(stack_id, 12, "%s" % ''.join(self.machine.stack))
mesg = self.canvas.create_text(0, 0, font=("Default", 30), anchor="nw")
self.canvas.itemconfig(mesg)
if self.machine.current_state in self.machine.accept_states:
self.canvas.insert(mesg, 12, "%s" % 'Accept !!!')
photo = PhotoImage(file="img/HappyTuring.gif")
label = Label(image=photo)
label.pack()
self.root.mainloop()
elif self.machine.current_state in self.machine.reject_states:
self.canvas.insert(mesg, 12, "%s" % 'Reject !!!')
photo = PhotoImage(file="img/SadTuring.gif")
label = Label(image=photo)
label.pack()
self.root.mainloop()
else:
self.canvas.insert(mesg, 12, "%s" % 'Halt !!!')
photo = PhotoImage(file="img/ConfusedTuring.gif")
label = Label(image=photo)
label.pack()
self.root.mainloop()