Skip to content

Instantly share code, notes, and snippets.

@vigilantPotato
Created January 20, 2019 12:17
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 vigilantPotato/32817c72e5ca9786af68c94b43777c8d to your computer and use it in GitHub Desktop.
Save vigilantPotato/32817c72e5ca9786af68c94b43777c8d to your computer and use it in GitHub Desktop.
tkinter button options
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# tkinter Buttonウィジェットオプションまとめ Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Buttonウィジェットのオプション"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- STANDARD OPTIONS(基本オプション)\n",
"\n",
"\n",
" activebackground, activeforeground, anchor,\n",
" background, bitmap, borderwidth, cursor,\n",
" disabledforeground, font, foreground\n",
" highlightbackground, highlightcolor,\n",
" highlightthickness, image, justify,\n",
" padx, pady, relief, repeatdelay,\n",
" repeatinterval, takefocus, text,\n",
" textvariable, underline, wraplength\n",
"\n",
"\n",
"- WIDGET-SPECIFIC OPTIONS(ウィジェット固有のオプション)\n",
"\n",
"\n",
" command, compound, default, height,\n",
" overrelief, state, width\n",
"\n",
"\n",
"使用する場面の多い、activebackground、background、relief、commandについてまとめる"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. activebackground, background"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- activebackground:クリック時の背景色を設定\n",
"- background (bgでも可):通常の背景色を設定"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import tkinter\n",
"\n",
"root = tkinter.Tk()\n",
"b = tkinter.Button(text=\"test\", activebackground='red', background='blue')\n",
"b.pack()\n",
"root.mainloop()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 通常時(背景青)\n",
"\n",
"![background=blue](https://gist.githubusercontent.com/vigilantPotato/ea369250c7f1399ad64389adc843d647/raw/d3d3587b71370a49e788d4f3286e279070e42664/tk_background1.jpg)\n",
"\n",
"- クリック時(背景赤)\n",
"\n",
"![activebackground=red](https://gist.githubusercontent.com/vigilantPotato/ea369250c7f1399ad64389adc843d647/raw/d3d3587b71370a49e788d4f3286e279070e42664/tk_background2.jpg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. relief"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- ボタンのデザインを以下の6種類から設定可能\n",
"- これらはtkinterライブラリ内の、constants.pyファイル内で規定されている\n",
"- 呼び出す際は、`tkinter.`を付ける必要がある\n",
"\n",
"\n",
"1. RAISED\n",
"2. SUNKEN\n",
"3. FLAT\n",
"4. RIDGE\n",
"5. GROOVE\n",
"6. SOLID"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import tkinter\n",
"\n",
"root = tkinter.Tk()\n",
"b1 = tkinter.Button(text=\"test1\", relief=tkinter.RAISED)\n",
"b2 = tkinter.Button(text=\"test2\", relief=tkinter.SUNKEN)\n",
"b3 = tkinter.Button(text=\"test3\", relief=tkinter.FLAT)\n",
"b4 = tkinter.Button(text=\"test4\", relief=tkinter.RIDGE)\n",
"b5 = tkinter.Button(text=\"test5\", relief=tkinter.GROOVE)\n",
"b6 = tkinter.Button(text=\"test6\", relief=tkinter.SOLID)\n",
"\n",
"b1.pack()\n",
"b2.pack()\n",
"b3.pack()\n",
"b4.pack()\n",
"b5.pack()\n",
"b6.pack()\n",
"root.mainloop()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- relief一覧\n",
"\n",
"![button reliefs](https://gist.githubusercontent.com/vigilantPotato/ea369250c7f1399ad64389adc843d647/raw/d3d3587b71370a49e788d4f3286e279070e42664/tk_relief.jpg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. command"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- ボタンを押したときに実行する関数を設定\n",
"- 引数を渡すのは不可"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello world\n"
]
}
],
"source": [
"import tkinter\n",
"\n",
"def hello_world():\n",
" print('hello world')\n",
"\n",
"root = tkinter.Tk()\n",
"b = tkinter.Button(text=\"hellow world\", command=hello_world)\n",
"b.pack()\n",
"root.mainloop()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment