38
57

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Python】Tkinterを使った雛形(クラス化手法)

Last updated at Posted at 2019-02-18

はじめに

最近はPythonを主に勉強していて、特にtkinterに触れていました。
ある程度、最初の形が定まってきたので、ここに記そうと思います。

環境

  • Windows 10 home
  • Python 3.7.1

クラス化手法による雛形

Tkinter.py
import tkinter as tk

class Application(tk.Frame):
	def __init__(self,master):
		super().__init__(master)
		self.pack()

		master.geometry("300x300")
		master.title("雛形")


def main():
	win = tk.Tk()
	app = Application(master=win)
	app.mainloop()


if __name__ == "__main__":
	main()

実行

実行.PNG

このような画面が出れば、成功です。

実装例

上記の雛形を拡張した例を下記に記します。
スペースキーを押すと三角形の図形が右斜め下に動くものです。

move.gif

Test.py
import tkinter as tk

class Application(tk.Frame):
	def __init__(self,master = None):
		super().__init__(master)
		self.pack()

		master.geometry("300x300")
		master.title("Test")

		self.canvas = tk.Canvas(master, width=300, height=300)
		self.canvas.pack()

		self.canvas.create_polygon(10,10,10,60,50,35,tag="id1")

		master.bind("<space>",self.move)

	def move(self,event):
		self.canvas.move("id1",5,5)


def main():
	win = tk.Tk()
	app = Application(master = win)
	app.mainloop()


if __name__ == "__main__":
	main()

応用例

その他書き方

38
57
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
57

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?