python2.7 Tkinter
Web Course Links: Click to open the link
1,label
#coding =utf-8
import tkinter as tk
window = tk. TK ()
window.title (' My Window ')
window.geometry (' 200x100 ')
L = tk. Label (window,text= ' omg!this is tk! ', bg= ' green ', (' Arial ', '), width=15,
height=2)
l.pack () #安置
#l. Place ()
Window.mainloop () #循环
02.label&button
#coding =utf-8
import tkinter as tk
window = tk. TK ()
window.title (' My Window ')
window.geometry (' 200x100 ') #边框大小
var = tk. Stringvar () #tk中的字符串变量
#显示位置
l = tk. Label (window,textvariable=var,bg= ' green ', (' Arial ',),
width=15,height=2)
l.pack () #固定label
On_hit = False
def hit_me ():
global On_hit
if On_hit = = False:
On_hit = True
var.set (' You hit me ')
else:
on_hit = False
var.set (')
B = Tk. Button (window,text= ' hit me ', width=15,height=2,
command=hit_me)
b.pack ()
window.mainloop () #显示
03entry&text
#coding utf-8
import tkinter as tk
window = tk. The TK ()
window.title (' My Window ')
window.geometry (' 200x200 ')
is #show for presentation, None for display prototype
e = tk. Entry (window,show= ' * ',)
E.pack ()
def insert_point ():
var = e.get ()
t.insert (' Insert ', VAR)
Def insert_end ():
var = e.get ()
t.insert (' End ', var)
# T.insert (1.1,var) can represent coordinates in numbers (1,1)
B1 = Tk. Button (window,text= ' Insert Point ', width=15,
height=2,command=insert_point)
b1.pack ()
b2 = tk. Button (window,text= ' Insert End ', width=15,
height=2,command=insert_end)
b2.pack ()
t = tk. Text (window,height=2,width=25)
t.pack ()
Window.mainloop ()
04listbox
#coding =utf-8
import tkinter as tk
window = tk. TK ()
window.title (' My Window ')
window.geometry (' 200x200 ')
var1 = tk. Stringvar ()
var2 = tk. Stringvar ()
L = tk. Label (window,bg= ' yellow ', width=4,textvariable=var1)
L.pack ()
def print_selection ():
value = Lb.get ( Lb.curselection ()) #得到光标选中的值
var1.set (value)
B = Tk. Button (window,text= ' Print Selection ', width=15,
height=2,command=print_selection)
b.pack ()
Var2.set ((11,22,33,44,55))
lb = tk. Listbox (window,listvariable=var2)
list_items = [1,2,3,4,5]
for item in List_items:
lb.insert (' End ', Item)
Lb.insert (1, ' a ')
Lb.insert (2, ' second ')
Lb.delete (2,)
Lb.pack ()
Window.mainloop ()