tkinter: Canvas+event bind

Posted SilentLittleCat

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tkinter: Canvas+event bind相关的知识,希望对你有一定的参考价值。

Draw a plot

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import random

if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
    import tkMessageBox as tk_messagebox
    import tkFont as tk_font
else:
    import tkinter as tk     ## Python 3.x
    import tkinter.messagebox as tk_messagebox
    import tkinter.font as tk_font


class App(tk.Frame):

	def __init__(self, master=None):

		tk.Frame.__init__(self, master)

		self.master.title("Canvas")

		self.lastx = 0
		self.lasty = 0

		self.active = False

		self.font = tk_font.Font(family='Helvetica', size=24, weight='bold')

		self.setScreenSize()

		self.createWidgets()

	def setScreenSize(self):
		ratio = 0.8
		screen_h = self.master.winfo_screenheight()
		screen_w = self.master.winfo_screenwidth()

		# print(screen_h, screen_w)
		self.app_h = round(screen_h * ratio)
		self.app_w = round(screen_w * ratio)

		y = round(screen_h * (1 - ratio) / 2.0)
		x = round(screen_w * (1 - ratio) / 2.0)

		geometry = '' + str(self.app_w) + 'x' + str(self.app_h) + '+' + str(x) + '+' + str(y)
		# print(geometry)
		self.master.geometry(geometry)

		self.config(width=self.app_w, height=self.app_h)

	def createWidgets(self):

		self.canvas = tk.Canvas(self, width=self.app_w, height=self.app_h, bg="white")

		dot_size = 20
		x0 = 100
		y0 = 100
		self.canvas.create_text(100, 50, text='A plot', font=self.font)
		for x in range(10):
			for y in range(10):
				x1 = x0 + dot_size * x
				y1 = y0 + dot_size * y
				x2 = x1 + dot_size
				y2 = y1 + dot_size

				self.canvas.create_rectangle(x1, y1, x2, y2, fill=self.getRandColor(), width=2)

		self.draw()

	def getRandColor(self):
		return '#' + self.getRandRGB() + self.getRandRGB() + self.getRandRGB()

	def getRandRGB(self):
		return ':02X'.format(random.randint(0, 255))

	def draw(self):

		self.pack()
		self.pack_propagate(0)
		self.canvas.pack()

		self.mainloop()

if __name__ == '__main__':

	App()


A simple palette

bind CMD
event type
Using Events With Tkinter Canvas Elements In Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import random

if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
    import tkMessageBox as tk_messagebox
else:
    import tkinter as tk     ## Python 3.x
    import tkinter.messagebox as tk_messagebox


class App(tk.Frame):

	def __init__(self, master=None):

		tk.Frame.__init__(self, master)

		self.master.title("Canvas")

		self.lastx = 0
		self.lasty = 0

		self.active = False

		self.setScreenSize()

		self.createWidgets()

	def setScreenSize(self):
		ratio = 0.8
		screen_h = self.master.winfo_screenheight()
		screen_w = self.master.winfo_screenwidth()

		# print(screen_h, screen_w)
		self.app_h = round(screen_h * ratio)
		self.app_w = round(screen_w * ratio)

		y = round(screen_h * (1 - ratio) / 2.0)
		x = round(screen_w * (1 - ratio) / 2.0)

		geometry = '' + str(self.app_w) + 'x' + str(self.app_h) + '+' + str(x) + '+' + str(y)
		# print(geometry)
		self.master.geometry(geometry)

		self.config(width=self.app_w, height=self.app_h)

	def createWidgets(self):

		self.canvas = tk.Canvas(self, width=self.app_w, height=self.app_h, bg="white")
		self.canvas.bind('<Button-1>', self.canvasClick)
		self.canvas.bind('<ButtonRelease-1>', self.canvasRelease)
		self.canvas.bind('<Motion>', self.canvasMotion)
		self.draw()

	def canvasClick(self, event):
		self.active = True
		self.lastx = event.x
		self.lasty = event.y

	def canvasRelease(self, event):
		self.active = False

	def canvasMotion(self, event):

		if(self.active):
			self.canvas.create_line(self.lastx, self.lasty, event.x, event.y, width=4, fill="red")
			self.lastx = event.x
			self.lasty = event.y

	def draw(self):

		self.pack()
		self.pack_propagate(0)
		self.canvas.pack()

		self.mainloop()

if __name__ == '__main__':

	App()


以上是关于tkinter: Canvas+event bind的主要内容,如果未能解决你的问题,请参考以下文章

tkinter:将鼠标滚轮绑定到滚动条

Python3 tkinter,怎么在Label/Canvas中插入图片?

如何在 Canvas 上的 Tkinter 中打开 PIL 图像

同时移动多个 tkinter.canvas 图形

Python tkinter canvas实现图片裁剪

Tkinter编程应知应会(23)-活用Canvas的对象tag