T k i n t e r 常 用 组 件 系 列 Tkinter常用组件系列 Tkinter常用组件系列
1.Tkinter之Label
from tkinter import *
import cv2
from PIL import Image,ImageTk
def resize_img_to_canvas(img,width,height):
img2 = cv2.resize(img, (width, height),interpolation=cv2.INTER_LANCZOS4)
img3 = Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))
img_resized = ImageTk.PhotoImage(img3)
return img_resized
win = Tk()
Label1 = Label(win,text="test").pack()
img_back = cv2.imread("../masks.jpg")
img_back = resize_img_to_canvas(img_back, 100, 100)
Label2 = Label(win,image=img_back).pack()
win.mainloop()
2.Tkinter之Button
from tkinter import *
win = Tk()
bitmap_list = ['error', 'hourglass', 'questhead', 'info',
'question', 'warning', 'gray12', 'gray50',
'gray75']
for bitmap in bitmap_list:
Button(win, bitmap=bitmap).pack()
win.mainloop()
import tkinter as tk
win = tk.Tk()
def callback():
print("我被调用了!")
b = tk.Button(win, text="执行", command=callback)
b.pack()
win.mainloop()
3.Tkinter之checkBoutton
from tkinter import *
win = Tk()
v1 = IntVar()
checkbox1 = Checkbutton(win,text="1",variable=v1).pack()
v2 = IntVar()
checkbox2 = Checkbutton(win,text="2",variable=v2).pack()
v2.set(1)
win.mainloop()
4.Tkinter之RadioButton
import tkinter as tk
win = tk.Tk()
v = tk.IntVar()
tk.Radiobutton(win, text="One", variable=v, value=1).pack(anchor="w")
tk.Radiobutton(win, text="Two", variable=v, value=2).pack(anchor="w")
tk.Radiobutton(win, text="Three", variable=v, value=3).pack(anchor="w")
v.set(1)
print(v.get())
win.mainloop()
5.Tkinter之Entry
6.Tkinter之ListBox
from tkinter import *
win = Tk()
theLB = Listbox(win, selectmode=EXTENDED)
theLB.pack()
for item in ["1", "2", "3", "4"]:
theLB.insert(END, item)
theButton = Button(win, text="删除", command=lambda x=theLB: x.delete(ACTIVE))
theButton.pack()
mainloop()
from tkinter import *
def add(from1,to1):
item1=from1.get(from1.curselection())
to1.insert(END,item1)
from1.delete(from1.curselection())
win=Tk()
win.title("添加快捷消息列表")
win.geometry("500x400")
Label(win,text="left").grid(row=0,column=0)
Label(win,text="right").grid(row=0,column=2)
var1 = StringVar()
var1.set("1 2 3 4 5")
var2 = StringVar()
var2.set("1 2 3 4 5")
listbox1 = Listbox(win,selectmode="single",listvariable=var1)
listbox2 = Listbox(win,selectmode="single",listvariable=var2)
listbox1.grid(row=1,column=0,rowspan=2)
listbox2.grid(row=1,column=2,rowspan=2)
btn1=Button(win,text=">>>",command=lambda :add(listbox1,listbox2)).grid(row=1,column=1,padx=10)
btn2=Button(win,text="<<<",command=lambda :add(listbox2,listbox1)).grid(row=2,column=1,padx=10)
win.mainloop()