Tkinter 做简单的窗口视窗 Python GUI

来源于:莫烦python https://mofanpy.com/tutorials/python-basic/tkinter/why/

例1 :Label 和 Buttom

# -*- coding: utf-8 -*-

import tkinter as tk

window = tk.Tk()
window.title('my window')
window.geometry('200x100')

var = tk.StringVar()
l = tk.Label(window,textvariable=var,bg="yellow",
           font=("Arial",12),width=15,height=2) #text="OMG!This is TK"
l.pack() #l.place()

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

点击按钮,label显示标签,再次点击,标签消失

 

例2 :Enrty 和 Text

# -*- coding: utf-8 -*-

import tkinter as tk

window = tk.Tk()
window.title("my window")
window.geometry("200x200")

e = tk.Entry(window,show=None) #show="*"
e.pack()

t = tk.Text(window,height=2)
t.pack()


def insert_point():
    var = e.get()
    t.insert("insert",var)
    
def insert_end():
    var = e.get()
    t.insert("end",var)    

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

window.mainloop()

  

 

posted on 2021-08-04 20:43  蔡军帅  阅读(296)  评论(0编辑  收藏  举报