リスト : スピンボックス
import tkinter as tk
root = tk.Tk()
root.option_add('*font', ('', 14))
v = ('apple', 'banana', 'cherry', 'grape', 'orange')
s1 = tk.Spinbox(root, from_ = 1, to = 10, increment = 1, width = 10)
s2 = tk.Spinbox(root, from_ = 1, to = 5, increment = 0.5, format = '%05.2f', width = 10)
s3 = tk.Spinbox(root, value = v, width = 10, state = 'readonly')
for w in (s1, s2, s3):
w.pack(padx = 5, pady = 5)
root.mainloop()
1.
2.
3.
| N.M | N 行の M 文字目 |
| @x,y | テキスト内の (x,y) の位置にある文字 |
| end | テキスト末尾 |
| マーク名 | その名前のマークをつけた位置 |
| タグ名.first | その名前のタグの最初の位置 |
| タグ名.last | その名前のタグの最後の位置 |
| +Nchars, -Nchars | そこから N 文字先、手前 |
| +Nlines, -Nlines | そこから N 行先、手前 |
| linestart, lineend | その行の先頭、末尾 |
| wordstart, wordend | その単語の先頭、末尾 |
リスト : テキストファイルを表示する
import tkinter as tk
import tkinter.filedialog as fd
import tkinter.scrolledtext as st
import sys, os.path
# MainWindow
root = tk.Tk()
root.option_add('*font', ('', 12))
root.title('Text Viewer')
# Global
path_name = os.getcwd()
# Text
t0 = st.ScrolledText()
t0.pack()
# File Select
def load_file():
global path_name
filename = fd.askopenfilename(filetypes = [('Text Files', ('.txt', '.py'))],
initialdir = path_name)
if filename != "":
path_name = os.path.dirname(filename)
fi = open(filename, encoding = 'utf-8')
t0.delete('1.0', 'end')
for x in fi:
t0.insert('end', x)
fi.close()
t0.mark_set(tk.INSERT, '1.0')
t0.focus_set()
# Menu
m0 = tk.Menu(root);
root.configure(menu = m0);
m1 = tk.Menu(m0, tearoff = 0)
m1.add_command(label = 'Open', under = 0, command = load_file)
m1.add_separator
m1.add_command(label = 'Exit', under = 0, command = sys.exit)
m0.add_cascade(label = 'File', under = 0, menu = m1 )
root.mainloop()
テキストウィジェット
ファイルの表示
リスト : 行番号の挿入と削除
import tkinter as tk
import tkinter.filedialog as fd
import tkinter.scrolledtext as st
import sys, os.path
# MainWindow
root = tk.Tk()
root.option_add('*font', ('', 12))
root.title('Text Viewer')
# Global
path_name = os.getcwd()
# Text
t0 = st.ScrolledText()
t0.pack()
#
num_flag = tk.BooleanVar()
num_flag.set(False)
# Change Number
def change_number():
line = int(float(t0.index('end')))
if num_flag.get():
for x in range(1, line):
t0.insert('{}.0'.format(x), '{:6d}:'.format(x))
else:
for x in range(1, line):
t0.delete('{}.0'.format(x), '{}.7'.format(x))
# File Select
def load_file():
global path_name
filename = fd.askopenfilename(filetypes = [('Text Files', ('.txt', '.py'))],
initialdir = path_name)
if filename != "":
path_name = os.path.dirname(filename)
fi = open(filename, encoding = 'utf-8')
t0.delete('1.0', 'end')
for x in fi:
t0.insert('end', x)
fi.close()
if num_flag.get(): change_number()
t0.mark_set(tk.INSERT, '1.0')
t0.focus_set()
# Menu
m0 = tk.Menu(root);
root.configure(menu = m0);
m1 = tk.Menu(m0, tearoff = 0)
m1.add_command(label = 'Open', under = 0, command = load_file)
m1.add_checkbutton(label = 'Number', under = 0, variable = num_flag, command = change_number)
m1.add_separator
m1.add_command(label = 'Exit', under = 0, command = sys.exit)
m0.add_cascade(label = 'File', under = 0, menu = m1 )
root.mainloop()
m1.add_checkbutton(label = 'Number', under = 0, variable = num_flag, command = change_number)
行番号の表示
| mark_set(markname, index) | マークの設定 |
| mark_unset(*markname) | マークの削除 |
| mark_names() | 定義されているすべてのマークを返す |
| mark_gravity(markname, left_or_right) | マークのつき方を left と right で指定 |
| mark_next(index) | index より後ろにあるマークを返す |
| mark_previous(index) | index より前にあるマークを返す |
t0.mark_set(first, '1.3')
t0.insert(first, '1234')
t0.insert(tk.INSERT, 'string')
| tag_add(tagname, index1, index2) | 指定した範囲に対して、タグ tagname を設定 |
| tag_delete(*tagname) | タグの削除 |
| tag_names(index) | index の位置にある文字と関連するすべてのタグを返す |
| tag_cget(tagname, option) | タグ tagname のオプションの値を返す |
| tag_configure(tagname, option, value) | タグ tagname のオプションを設定する |
| tag_bind(tagname, event, callback) | タグ tagname にバインドを設定する |
リスト : 行番号に色をつける
def change_number():
line = int(float(t0.index('end')))
if num_flag.get():
for x in range(1, line):
t0.insert('{}.0'.format(x), '{:6d}:'.format(x), 'LINENUM')
else:
for x in range(1, line):
t0.delete('{}.0'.format(x), '{}.7'.format(x))
t0.tag_configure('LINENUM', foreground = 'red')
t0.insert(index, string, tagname, ...)
行番号を赤く表示する
| window | ウィジェット名 |
| create | ウィジェットを生成するコマンドを指定(window を指定しない場合のみ有効) |
| align | 上下方向の揃え指定 (baseline, top, bottom, center) |
| stretch | 上下方向の引き延ばし |
| padx, pady | スペースの指定 |
リスト : ラベル (画像) の挿入
import tkinter as tk
import tkinter.filedialog as fd
import tkinter.scrolledtext as st
import sys, os.path
# MainWindow
root = tk.Tk()
root.option_add('*font', ('', 12))
root.title('Text Viewer')
# Global
path_name = os.getcwd()
path_name_image = os.getcwd()
image_buff = {}
# Text
t0 = st.ScrolledText()
t0.tag_configure('LINENUM', foreground = 'red')
t0.pack()
#
num_flag = tk.BooleanVar()
num_flag.set(False)
# Change Number
def change_number():
line = int(float(t0.index('end')))
if num_flag.get():
for x in range(1, line):
t0.insert('{}.0'.format(x), '{:6d}:'.format(x), 'LINENUM')
else:
for x in range(1, line):
t0.delete('{}.0'.format(x), '{}.7'.format(x))
# File Select
def load_file():
global path_name
filename = fd.askopenfilename(filetypes = [('Text Files', ('.txt', '.py'))],
initialdir = path_name)
if filename != "":
path_name = os.path.dirname(filename)
fi = open(filename)
t0.delete('1.0', 'end')
for x in fi:
t0.insert('end', x)
fi.close()
if num_flag.get(): change_number()
t0.mark_set(tk.INSERT, '1.0')
t0.focus_set()
# ラベルの挿入
def insert_image():
global path_name_image
filename = fd.askopenfilename(filetypes = [('Image Files', ('.gif', '.ppm')),
('GIF Files', '.gif'),
('PPM Files', '.ppm')],
initialdir = path_name_image)
if filename != "":
if filename in image_buff:
image_data = image_buff[filename]
else:
image_data = tk.PhotoImage(file = filename)
image_buff[filename] = image_data
path_name_image = os.path.dirname(filename)
label = tk.Label(root, image = image_data, relief = 'raised', borderwidth = 4)
t0.window_create(tk.INSERT, window = label, align = 'baseline')
# Menu
m0 = tk.Menu(root);
root.configure(menu = m0);
m1 = tk.Menu(m0, tearoff = 0)
m1.add_command(label = 'Open', under = 0, command = load_file)
m1.add_checkbutton(label = 'Number', under = 0, variable = num_flag, command = change_number)
m1.add_command(label = 'Image', under = 0, command = insert_image)
m1.add_separator
m1.add_command(label = 'Exit', under = 0, command = sys.exit)
m0.add_cascade(label = 'File', under = 0, menu = m1 )
root.mainloop()
m1.add_command(label = 'Image', under = 0, command = insert_image)
Tcl/Tk Logo を挿入
テキストの編集
| image | 表示する画像 |
| name | 画像を参照するための名前を付ける |
| align | 上下方向の揃え指定(baseline, top, bottom, center) |
| padx, pady | スペースの指定 |
リスト : 画像の挿入
def insert_image():
global path_name_image
filename = fd.askopenfilename(filetypes = [('Image Files', ('.gif', '.ppm')),
('GIF Files', '.gif'),
('PPM Files', '.ppm')],
initialdir = path_name_image)
if filename != "":
if filename in image_buff:
image_data = image_buff[filename]
else:
image_data = tk.PhotoImage(file = filename)
image_buff[filename] = image_data
path_name_image = os.path.dirname(filename)
t0.image_create(tk.INSERT, image = image_data, align = 'baseline')
Tcl/Tk Logo を挿入