Python之GUI组件Tkinter控件大合集

# -*- coding: utf8 -*-'
import tkinter as tk
from tkinter import Label, font
import tkinter.messagebox as messagebox

font_str = ('Arial', 20)


def InitWindow(_window):
    global window
    window = _window


def DrawTest():
    DrawLabel()
    DrawButton()
    DrawEntry()
    DrawText()
    DrawListbox()
    DrawRadioButton()
    DrawCheckButton()
    DrawScale()
    DrawCanvas()
    DrawMenu()
    DrawFrame()
    DrawMessageBox()

# DrawLabel ======================================


def DrawLabel():
    print("draw lable")
    global var
    var = tk.StringVar()
    l = tk.Label(window, textvariable=var, bg="green", font=font_str, width=30, height=2)
    l.pack()
# End DrawLabel ==================================


# DrawButton ======================================
def DrawButton():
    print("draw button")
    b = tk.Button(window, text="hit me", width=10, height=1, command=hit_me)
    b.pack()


on_hit = False


def hit_me():
    global on_hit
    if on_hit == False:
        var.set("hit me")
    else:
        var.set("")
    on_hit = not on_hit
# End DrawButton ==================================


# DrawEntry ======================================
def DrawEntry():
    print("draw entry")
    e1 = tk.Entry(window, show="*", font=font_str)
    e1.pack()
    e2 = tk.Entry(window, show=None, font=font_str)
    e2.pack()
# End DrawEntry ==================================


# DrawText ======================================
def DrawText():
    print("draw text")
    b1 = tk.Button(window, text="insert", width=10, height=1, command=on_inset)
    b1.pack()
    b2 = tk.Button(window, text="end", width=10, height=1, command=on_end)
    b2.pack()
    global e
    e = tk.Entry(window, show=None, font=font_str)
    e.pack()
    global t
    t = tk.Text(window, font=font_str, height=3)
    t.pack()


def on_inset():
    var_text = e.get()
    t.insert("insert", var_text)


def on_end():
    var_text = e.get()
    t.insert("end", var_text)
# End DrawText ==================================


# DrawListbox ======================================
def DrawListbox():
    print("draw list box")
    global var_listbox
    var_listbox = tk.StringVar()
    l_listbox = tk.Label(window, bg="green", fg="yellow", font=font_str, width=10, textvariable=var_listbox)
    l_listbox.pack()

    b_listbox = tk.Button(window, text="print selection", width=15, height=2, command=print_selection_lb)
    b_listbox.pack()

    list_items = ["aa", "bb", "cc", "dd"]
    global lb
    var_listbox2 = tk.StringVar()
    var_listbox2.set((1, 2, 3, 4))

    lb = tk.Listbox(window, listvariable=var_listbox2)
    for item in list_items:
        lb.insert("end", item)
    lb.insert(1, "first")
    lb.insert(2, "second")
    lb.delete(2)
    lb.pack()


def print_selection_lb():
    value = lb.get(lb.curselection())
    var_listbox.set(value)
# End DrawListbox ==================================



# DrawRadioButton ======================================
def DrawRadioButton():
    print("draw radio button")
    global var_rb
    var_rb = tk.StringVar()
    global l_rb
    l_rb = tk.Label(window, bg="yellow", width=20, text="empty")
    l_rb.pack()
    rb1 = tk.Radiobutton(window, text="选项1", variable=var_rb, value="A", command=print_selection_rb)
    rb1.pack()
    rb2 = tk.Radiobutton(window, text="选项2", variable=var_rb, value="B", command=print_selection_rb)
    rb2.pack()
    rb3 = tk.Radiobutton(window, text="选项3", variable=var_rb, value="C", command=print_selection_rb)
    rb3.pack()

def print_selection_rb():
    l_rb.config(text="You have selected " + var_rb.get())
# End DrawRadioButton ==================================


# DrawCheckButton ======================================
def DrawCheckButton():
    print("draw check button")
    global l_cb
    l_cb = tk.Label(window, bg="yellow", width=20, text="empty")
    l_cb.pack()
    global var_cb1, var_cb2
    var_cb1 = tk.IntVar()
    var_cb2 = tk.IntVar()
    cb1 = tk.Checkbutton(window, text="Python", variable=var_cb1, onvalue=1, offvalue=0, command=print_selection_cb)
    cb1.pack()
    cb2 = tk.Checkbutton(window, text="C++", variable=var_cb2, onvalue=1, offvalue=0, command=print_selection_cb)
    cb2.pack()

def print_selection_cb():
    if (var_cb1.get() == 1) & (var_cb2.get() == 0):
        l_cb.config(text="I love only python")
    elif (var_cb1.get() == 0) & (var_cb2.get() == 1):
        l_cb.config(text="I love only c++")
    elif (var_cb1.get() == 0) & (var_cb2.get() == 0):
        l_cb.config(text="I do not love either")
    else:
        l_cb.config(text="I love both")
# End DrawCheckButton ==================================


# DrawScale ======================================
def DrawScale():
    print("draw scale")
# End DrawScale ==================================



def DrawCanvas():
    print("draw canvas")


# DrawMenu ======================================
def DrawMenu():
    print("draw menu")
    menubar = tk.Menu(window)
    fileMenu = tk.Menu(menubar, tearoff=0)
    menubar.add_cascade(label="File", menu=fileMenu)

    fileMenu.add_command(label="Open", command=do_job)
    fileMenu.add_separator()
    fileMenu.add_command(label="Exit", command=window.quit)

    editMenu = tk.Menu(menubar, tearoff=0)
    menubar.add_cascade(label="Edit", menu=editMenu)
    editMenu.add_command(label="Cut", command=do_job)
    editMenu.add_command(label="Copy", command=do_job)
    editMenu.add_command(label="Paste", command=do_job)
    
    window.config(menu=menubar)

def do_job():
    print("do_job")
# End DrawMenu ==================================



def DrawFrame():
    print("draw frame")


# DrawMessageBox ======================================
def DrawMessageBox():
    print("draw message box")
    b_mb = tk.Button(window, text="Message Box", font=font_str, command=hit_me_mb)
    b_mb.pack()

def hit_me_mb():
    messagebox.showerror(title="Error!!", message="出错了!!")
# End DrawMessageBox ==================================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cchoop

有用的话请杯肥宅水

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值