美文网首页
Tkinter--Python的UI库

Tkinter--Python的UI库

作者: 易点梅 | 来源:发表于2019-04-08 21:53 被阅读0次

    Tkinter是Python的标准库,import  tkinter

    创建窗口

    win = tkinter.Tk()

    win.title("设置标题")

    #设置大小和位置 *号两边是大小,200横坐标,0竖坐标

    win.geometry("400*600+200+0")

    #进入控件生成、布局,事件编写......

    标签控件,显示文本

    label = tkinter.Lable(win,text="yuangs",bg="pink",fg="red",font=("宋体",20),width=5,height=20,wraplength=100,justify="left",anchor="e s w n ")

    #显示

    label.pack()

    按钮控件

    btn = tkinter.Button(win,text="确定" , command=func)

    btn2 = tkinter.Button(win,text="取消" , command=lambda : print("取消了"))

    btn3 = tkinter.Button(win,text="查询",width=5,height=5)

    btn.pack()

    输入控件,输入框

    e = tkinter.Variable()

    entry = tkinter.Entry(win,show="*", textvariable = e)

    #e就代表输入框这个对象

    e.set("yuangs is handsome") #设置输入框的值

    print(e.get()) #获取输入框的值

    entry.pack()

    文本控件

    scroll = tkinter.Scroll() #滚动条

    scroll.pack(side = tkinter.RIGHT , fill = tkinter.Y) #窗体滚动条

    text = tkinter.Text(win,width=30,height=8) #height显示行数

    str = ''' aaaaaaaaaaaaaaaaaaaaaaa'''

    text.insert(tkinter.INSERT,str) #设置text的内容

    text.delete(0.0,END)  #清空text的内容

    #关联text和scroll

    scroll.config(command = text.yview)

    text.config(yscrollcommand = scroll.set)

    多选框、复选框

    flag = tkinter.BooleanVar()

    check1 = tkinter.Checkbutton(win,text="Power",variable = flag , command = showText)

    check1.pack()

    flag2 = tkinter.BooleanVar()

    check2 = tkinter.Checkbutton(win,text="Beauty",variable = flag2 , command = showText)

    chekc2.pack()

    单选框

    r = tkinter.IntVar()  #StringVal(),根据value的数据类型

    radio1 = tkinter.Radiobutton(win,text="one",value=55,variable=r,command = showValue)

    radio1.pack()

    radio2 = tkinter.Radiobutton(win,text="two",value=66,variable=r,command = showValue)

    radio2.pack()

    #控件绑定的函数

    def    func():

              print("按下按钮....")

    def    showText():

            if flag.get() == True:

                    print("Power")

            if flag2.get() == True:

                    print("Beauty")

            tk.insert(tkinter.INSERT,"power--beauty")

    def      showValue():

                print(r.get())

    win.mainloop()

    相关文章

      网友评论

          本文标题:Tkinter--Python的UI库

          本文链接:https://www.haomeiwen.com/subject/ktrmiqtx.html