Custom TKinter

Python

custom tkinter

tkinter

————————————————–

custom tkinter

classの仕組みがわからないため、チュートリアルでつまづいた

https://customtkinter.tomschimansky.com/tutorial/frames

—————————————————

pip install customtkinter

(pip install customtkinter –upgrade)

import customtkinter as ct

ct.set_appearance_mode(“System”)

ct.set_default_color_theme(“blue”)

app = ct.CTk()

app.title(“my app”)

app.geometry(“400×240”)

app.grid_columnconfigure((0), weight=1)①

def clicked():

print(“button pressed”)

button = ct.CTkButton(master=app, text=”my button”, command=clicked)

button.grid(row=0, column=0, padx=20, pady=20, sticky=”ew”, columnspan=2)②

checkbox_frame = ct.CTkFrame()

checkbox_frame.grid(row=1, column=0, padx=10, pady=(10,0), sticky=”nsw”, columnspan=2)

checkbox_1 = ct.CTkCheckBox(checkbox_frame, text=”checkbox 1”)

checkbox_1.grid(row=1, column=0, padx=20, pady=(0,20), sticky=”w”)

checkbox_2 = ct.CTkCheckBox(checkbox_frame, text=”checkbox 2”)

checkbox_2.grid(row=1, column=1, padx=20, pady=(0,20))

app.mainloop()

①0行目を等間隔に配置する

②stickyでボタンを領域分伸ばし、columnspanで2列分の幅にする

—————————————–

tkinter

—————————————–

import sys

import Tkinter

#——-①windowを作成——#

root = Tkinter.Tk()

root.title(u’ウィンドウ名’)

root.geometry(‘400*300’)

#—–②frameを作成(reliefは枠線)—–#

frame1 = Frame(root, width=500, height=100, borderwidth=2, relief=’solid’)

frame2 = Frame(root, width=250, height=100, borderwidth=2, relief=’solid’)

#frameサイズを固定

frame1.propagate(False)

frame2.propagate(False)

#frameの配置

frame1.grid(row=0,column=0,columnspan=2)

frame1.grid(row=0,column=2)

#—–③widgetを配置—–#

#ラベル(anchorは上=N、下=S、右=E、左=W、左上=NW)

label = Tkinter.Label(frame1, text=u’text’, font=(‘’,14), background=’#777’, foreground=’#ff0000’)

label.pack(padx=5,pady=10,anchor=W)

#一行入力ボックス

entry = Tkinter.Entry(frame1,width=50)

entry.insert(Tkinter.END,’挿入する文字’)

entry.pack()

#エントリーの中身を取得・削除

value = entry.get()

entry.delete(0,Tkinter.END)

#ボタン

button = Tkinter.Button(frame1,text=’ボタンです’, width=30, command=DeleteEntryValue)

button.pack()

def DeleteEntryValue(event):

entry.delete(0, Tkinter.END)

button[‘text’] = ‘クリックしました’

#チェックボタン

bv = Tkinter.BooleanVar()

bv.set(True)

checkbutton = Tkinter.Checkbutton(text=u’項目1’, variable=bv)

checkbutton.pack() 

#チェックされているか

text = ‘’

if bv.get() == True:

text += ‘チェックされてます’

tkMessageBox.showinfo(‘ウィンドウタイトル’,text)

#画像を表示

image = Tkinter.PhotoImage(file=’./logo.png’)

label2 = Label(frame2,image=image)

label2.pack(pady=20)

root.mainloop()

BACK