Introduction to python2.7 Tkinter

Source: Internet
Author: User
Tags pack

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 ()


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.