本文整理汇总了Python中tkinter.Canvas.insert方法的典型用法代码示例。如果您正苦于以下问题:Python Canvas.insert方法的具体用法?Python Canvas.insert怎么用?Python Canvas.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tkinter.Canvas
的用法示例。
在下文中一共展示了Canvas.insert方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw_automata
# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import insert [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 insert [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()