本文整理汇总了Python中tkinter.Canvas类的典型用法代码示例。如果您正苦于以下问题:Python Canvas类的具体用法?Python Canvas怎么用?Python Canvas使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Canvas类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
'''runs the program'''
square_width = 64
num_cols = 7
num_rows = 6
canvas_width = (num_cols+1)*square_width
canvas_height = (num_rows+1)*square_width
window = Tk()
window.configure(background='black')
window.title("DynaBLASTER")
window.resizable(0,0) #removes maximize option
#window.iconbitmap('icon.ico')
#window.tk.call('tk', 'scaling', 20.0)
canvas = Canvas(window, width=canvas_width, highlightthickness=0,
height=canvas_height, background='#717171')
canvas.grid(row=1,column=0, columnspan=5)
graphics = Graphics(canvas, num_rows, num_cols, square_width, window)
board = Board(canvas, square_width, num_rows, num_cols,
canvas_width, canvas_height)
col=0
row=0
player1 = Player(canvas, board, square_width, graphics, col, row)
col = graphics.cols - 3
row = graphics.rows - 3
player2 = Player(canvas, board, square_width, graphics, col, row)
# Import settings from bindings file
bindings_file = open('bindings.json')
p1_bindings, p2_bindings, gen_bindings = json.load(bindings_file)
window.bind(key_release_of(p1_bindings["Up"]), lambda event:player1.key_release('Up'))
window.bind(key_release_of(p1_bindings["Down"]), lambda event:player1.key_release('Down'))
window.bind(key_release_of(p1_bindings["Left"]), lambda event:player1.key_release('Left'))
window.bind(key_release_of(p1_bindings["Right"]), lambda event:player1.key_release('Right'))
window.bind(p1_bindings["Up"], lambda event:player1.key_press('Up'))
window.bind(p1_bindings["Down"],lambda event:player1.key_press('Down'))
window.bind(p1_bindings["Left"], lambda event:player1.key_press('Left'))
window.bind(p1_bindings["Right"], lambda event:player1.key_press('Right'))
window.bind(p1_bindings["Bomb"], player1.place_bomb)
window.bind(key_release_of(p2_bindings["Up"]), lambda event:player2.key_release('Up'))
window.bind(key_release_of(p2_bindings["Down"]), lambda event:player2.key_release('Down'))
window.bind(key_release_of(p2_bindings["Left"]), lambda event:player2.key_release('Left'))
window.bind(key_release_of(p2_bindings["Right"]), lambda event:player2.key_release('Right'))
window.bind(p2_bindings["Up"], lambda event:player2.key_press('Up'))
window.bind(p2_bindings["Down"], lambda event:player2.key_press('Down'))
window.bind(p2_bindings["Left"], lambda event:player2.key_press('Left'))
window.bind(p2_bindings["Right"], lambda event:player2.key_press('Right'))
window.bind(p2_bindings["Bomb"], player2.place_bomb)
window.bind(gen_bindings["Pause"], lambda event:pause_game(player1, player2, graphics))
window.mainloop()
示例2: clear_can
def clear_can():
"""
캔버스를 초기화 하는 버튼명령
"""
global can, tx_counter, count
can.destroy()
can = Canvas(frame_left, width=400, height=400, bg="#FFFFFF")
tx_counter = can.create_text(20, 10, text="0")
can.pack()
count = 0
示例3: __init__
def __init__(self, parent):
Canvas.__init__(self, width=WIDTH, height=HEIGHT,
background="black", highlightthickness=0)
self.parent = parent
self.initGame()
self.pack()
示例4: initUI
def initUI(self):
self.parent.title("Mario's Picross Puzzle Editor")
self.puzzles = None
# Build the menu
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar)
self.fileMenu = fileMenu
fileMenu.add_command(label="Open Mario's Picross ROM...",
command=self.onOpen)
fileMenu.add_command(label="Save ROM as...",
command=self.onSave,
state=DISABLED)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command=self.onExit)
menubar.add_cascade(label="File", menu=fileMenu)
# Navigation
Label(self.parent).grid(row=0, column=0)
Label(self.parent).grid(row=0, column=4)
self.parent.grid_columnconfigure(0, weight=1)
self.parent.grid_columnconfigure(4, weight=1)
prevButton = Button(self.parent,
text="<--",
command=self.onPrev,
state=DISABLED
)
self.prevButton = prevButton
prevButton.grid(row=0, column=1)
puzzle_number = 1
self.puzzle_number = puzzle_number
puzzleNumber = Label(self.parent, text="Puzzle #{}".format(puzzle_number))
self.puzzleNumber = puzzleNumber
puzzleNumber.grid(row=0, column=2)
nextButton = Button(self.parent,
text="-->",
command=self.onNext,
state=DISABLED
)
self.nextButton = nextButton
nextButton.grid(row=0, column=3)
# Canvas
canvas = Canvas(self.parent)
self.canvas = canvas
for i in range(15):
for j in range(15):
fillcolor = "gray80"
self.canvas.create_rectangle(10+20*j, 10+20*i,
10+20*(j+1), 10+20*(i+1),
fill=fillcolor,
tags="{},{}".format(i, j)
)
self.canvas.bind("<ButtonPress-1>", self.onClick)
canvas.grid(row=1, columnspan=5, sticky=W+E+N+S)
self.parent.grid_rowconfigure(1, weight=1)
示例5: TkWindowMainLoop
class TkWindowMainLoop(Thread):
'''class for thread that handles Tk window creation and main loop'''
def __init__(self, client):
Thread.__init__(self)
self.window = None
self.canvas = None
self.client = client
self.start()
def callbackDeleteWindow(self):
self.client.stop()
def stop(self):
self.window.quit()
def run(self):
self.window = Tk()
self.canvas = Canvas(self.window, width=1024, height=768)
self.canvas.pack()
self.window.protocol("WM_DELETE_WINDOW", self.callbackDeleteWindow)
self.window.mainloop()
def getWindow(self):
return self.window
def getCanvas(self):
return self.canvas
示例6: main
def main():
# Basic starter code.
window = tkinter.Tk()
canvas = Canvas(window, width = CANVAS_SIZE, height = CANVAS_SIZE)
canvas.pack()
draw_shaky_line(canvas, 50, 50, 650, 650)
示例7: __init__
def __init__(self, *args, **kwargs):
Canvas.__init__(self, *args, **kwargs)
self.graphs = []
self.x = Axis()
self.y = Axis()
self.y.direction = -1
self.bind("<Configure>", lambda ev: self.after_idle(self.__onConfigure__))
示例8: Main
class Main(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.info = {}
self.window = None
self.size = (640, 480)
self.fields = []
self.init_ui()
def init_ui(self):
self.parent.title("Node Writer")
self.style = Style()
self.style.theme_use("alt")
self.pack(fill="both", expand=True)
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
menubar.add_command(label="New", command=self.onNew)
menubar.add_command(label="Show Info", command=self.get_info)
menubar.add_command(label="Exit", command=self.quit)
self.canvas = Canvas(self, background="white", width=self.size[0], height=self.size[1])
self.canvas.pack(fill="both", expand=1)
self.canvas.bind("<Motion>", self.move_boxes)
def move_boxes(self, event):
print(event.x, event.y)
"""
x, y = (event.x-1, event.y-1)
x1, y1, x2, y2 = self.canvas.bbox("test")
if x > x1 and y > y1 and x < x2 and y < y2:
print("Hit")
else:
print("Missed")
"""
def onNew(self):
new = Node(self, "Node_entry")
label = new.insert_entry_field("Labels")
label2 = new.insert_entry_field("Labels2")
text = new.insert_text_field("Text")
new.ok_cancel_buttons()
def get_info(self):
x, y = (self.size[0]/2, self.size[1]/2)
for i in self.info:
label_frame= LabelFrame(self, text="name")
label_frame.pack(fill="y")
for entry in self.info[i]["Entry"]:
frame = Frame(label_frame)
frame.pack(fill="x")
label = Label(label_frame, text=self.info[i]["Entry"][entry], width=6)
label.pack(side="left", anchor="n", padx=5, pady=5)
for text in self.info[i]["Text"]:
frame = Frame(label_frame)
frame.pack(fill="x")
label = Label(label_frame, text=self.info[i]["Text"][text], width=6)
label.pack(side="left", anchor="n", padx=5, pady=5)
window = self.canvas.create_window(x, y, window=label_frame, tag="test")
示例9: setUpCanvas
def setUpCanvas():
from tkinter import Tk, Canvas, YES, BOTH
root = Tk()
root.title("A Tk/Python Graphics Program")
canvas = Canvas(root, width = 1270, height = 780, bg = 'black')
canvas.pack(expand = YES, fill = BOTH)
return canvas, root
示例10: main
def main():
root = Tk(className="My graphics!")
root.geometry("400x400")
canvas = Canvas()
canvas.pack(fill=BOTH, expand=1)
# img = draw_gradient(canvas)
img = draw_danger_levels(canvas, 50, 50)
# что бы ограничить число
# if n > 255:
# n = 255
# хочу: n = 256 => 0
# n = 257 => 1
# ...
# n = 300 => 44
# 10 // 3 = 3(1)
# 10 - (10 // 3) * 3
# 10 % 3
# a % b < b
# 10 % 3 = 1
# 11 % 3 = 2
# 12 % 3 = 0
# 13 % 3 = 1
root.mainloop()
示例11: simulation_canvas
def simulation_canvas(parent, **config):
"""Initializes the canvas and sets it up to receive user input."""
global the_canvas
the_canvas = Canvas(parent, **config)
the_canvas.focus_set() # this makes tkinter canvas accept keyboard commands
the_canvas.bind("<Key>", lambda event: model.move_ship(event))
return the_canvas
示例12: __init__
def __init__(self, master=None, cnf=None, **kw):
if not cnf:
cnf = {}
Canvas.__init__(self, master, cnf, **kw)
self._polygons = list()
self._layer = None
self._image = None
示例13: set_view
def set_view(self, root_view: tk.Canvas=None, _x=0, _y=0):
if isinstance(root_view, tk.Canvas):
self.__Rect = RegionRect(root_view, self.__name, self.__current_price)
self.__Rect.grid(column=_x, row=2*_y)
frame = tk.Frame(root_view, height=38, width=0)
frame.grid(column=_x, row=2*_y+1)
x,y=_x*90, _y*80
root_view.create_rectangle(x,y, x+90, y+80, width=1, fill="white")
示例14: CameraView
class CameraView(View):
def __init__(self, params):
super(CameraView, self).__init__(params)
self._multiplier = int(params["multiplier"]) if "multiplier" in params else 1
self._multiplier = max(self._multiplier, 1)
self._tk = Tk()
self._canvas = Canvas(self._tk, width=80 * self._multiplier, height=60 * self._multiplier)
self._tk.title("Camera view")
self._tk.resizable(width=False, height=False)
self._canvas.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True)
self._tk.protocol("WM_DELETE_WINDOW", self.on_press_close)
self._base_dir = "camera_" + str(int(time.time() * 1000))
os.makedirs(self._base_dir)
def run(self):
super(CameraView, self).run()
self._tk.mainloop()
def on_new_input(self):
try:
hex_str = self.get_input()
img = self._get_image(hex_str)
except Exception as e:
logging.debug(str(e))
return
if img is None:
return
bmp = ImageTk.BitmapImage(image=img, foreground="white", background="black")
self._canvas.create_image(0, 0, image=bmp, anchor=tkinter.NW)
self._tk_image = bmp
img.save(self._base_dir + "/" + str(int(time.time() * 1000)) + ".png")
def on_dismiss(self):
self._tk.after_idle(self.on_press_close)
def on_press_close(self):
self._tk.destroy()
self.join_io_thread()
def _get_image(self, hex_str) -> Image:
try:
hex_data = binascii.unhexlify(hex_str)
# Invert data from MCU
hex_data = bytes([~h & 0xFF for h in hex_data])
except TypeError as e:
logging.debug(str(e))
return
img = Image.frombytes(mode="1", size=(80, 60), data=hex_data)
img = img.resize((80 * self._multiplier, 60 * self._multiplier))
return img
示例15: __init__
def __init__(self, parent):
"""Simple Canvas class."""
Canvas.__init__(self, parent)
self.parent = parent
self.config(background="white", width=960, height=640)
self.num = 1
self.pack()
self.element_list = []
self.bindings()