Skip to content

Instantly share code, notes, and snippets.

@curious-eyes
Created January 13, 2019 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curious-eyes/ad386280fb43321260e7d07e0c1737a1 to your computer and use it in GitHub Desktop.
Save curious-eyes/ad386280fb43321260e7d07e0c1737a1 to your computer and use it in GitHub Desktop.
""" tkinter.Frame 継承
"""
import tkinter as tk
from PIL import ImageTk
class Application(tk.Frame):
""" メイン
同ディレクトリに cat01.png , cat02.png を配置する必要があります
$ pip install Pillow
も必要です
"""
def __init__(self, master=None):
super().__init__(master)
self.callnum = 0
self.img_data = ImageTk.PhotoImage(file=self.get_imagename())
self.pack()
self.create_widgets()
def get_imagename(self):
""" ファイル名生成 """
self.callnum += 1
filename = 'cat{:02d}.jpg'.format((self.callnum % 2) + 1)
# print('image: {}'.format(filename))
return filename
def create_widgets(self):
""" widgets 生成
"""
#キャンバスエリア
self.image_canvas = tk.Canvas(self, width=800, height=660)
self.image_canvas.create_image(0, 0, image=self.img_data, anchor=tk.NW, tag="cats")
#キャンバスバインド
self.image_canvas.place(x=0, y=0)
self.image_canvas.pack(side="top")
self.hi_there = tk.Button(self)
self.hi_there["text"] = "Click Me"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red",
command=TKROOT.destroy)
self.quit.pack(side="bottom")
def say_hi(self):
""" 動作
"""
self.img_data = ImageTk.PhotoImage(file=self.get_imagename())
self.image_canvas.itemconfig("cats", image=self.img_data, anchor=tk.NW)
TKROOT = tk.Tk()
TKROOT.title(u"TkinterのCanvasを使ってみる")
TKROOT.geometry("805x720") #ウインドウサイズ(「幅x高さ」で指定)
APP = Application(master=TKROOT)
APP.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment