tkinter 02 Entry & Text 输入, 文本框

  •  运行结果:https://s1.ax1x.com/2018/04/16/Ceuu4g.gif
  • # coding=gbk
    # 注意:输入框entry没有width、height属性
    # 注意:button中command=insert_cursor()是错误的,不能加括号
    #       也即button命令都是无参的函数
    # 运行结果:https://s1.ax1x.com/2018/04/16/Ceuu4g.gif
    import tkinter
    
    window = tkinter.Tk()
    window.title("jkn1234")
    window.geometry("500x500")
    
    # 文本框
    text = tkinter.Text(
        window,
        height=4
    )
    text.insert('end', '******************')
    text.pack()
    
    # 输入框, 注意:输入框entry没有width、height属性
    entry = tkinter.Entry(
        window,
        show='*'
    )
    entry.pack()
    
    def insert_cursor():
        var = entry.get()
        text.insert("insert", var)
    
    def insert_end():
        var = entry.get()
        text.insert("end", var)
    
    button1 = tkinter.Button(
        window,
        command = insert_cursor,
        text = "在光标处插入"
    )
    button1.pack()
    
    button2 = tkinter.Button(
        window,
        command = insert_end,
        text = "在结尾处插入"
    )
    button2.pack()
    
    window.mainloop()

     

posted on 2018-04-16 10:58  jkn1234  阅读(958)  评论(0编辑  收藏  举报