tkinter中的Entry和Text應用實例

原文地址

分類目錄——tkinter

  • 先看一下Entry和Text的效果

    EntryText

    其中上面爲輸入框,下面爲Text展示框

  • tk.Entry()

    e = tk.Entry(window, show='*' , bg='#aaaaaa')   # 聲明輸入框
    # 輸入框,可供傳入的變量有: background, bd, bg, borderwidth, cursor,
    # exportselection, fg, font, foreground, highlightbackground,
    # highlightcolor, highlightthickness, insertbackground,
    # insertborderwidth, insertofftime, insertontime, insertwidth,
    # invalidcommand, invcmd, justify, relief, selectbackground,
    # selectborderwidth, selectforeground, show, state, takefocus,
    # textvariable, validate, validatecommand, vcmd, width,
    # xscrollcommand.
    # show='*',以*代替輸入, = None,原始顯示,不會被代替
    # bg    背景顏色
    # boderwidth    邊框寬度
    e.pack()    # 放置輸入框
    
  • tk.Text()

    t = tk.Text(window, height=2)
    # 放置一個文本框,用來顯示
    # 可供傳入的參數有
    # STANDARD OPTIONS
    #     background, borderwidth, cursor,
    #     exportselection, font, foreground,
    #     highlightbackground, highlightcolor,
    #     highlightthickness, insertbackground,
    #     insertborderwidth, insertofftime,
    #     insertontime, insertwidth, padx, pady,
    #     relief, selectbackground,
    #     selectborderwidth, selectforeground,
    #     setgrid, takefocus,
    #     xscrollcommand, yscrollcommand,
    #
    # WIDGET-SPECIFIC OPTIONS
    #     autoseparators, height, maxundo,
    #     spacing1, spacing2, spacing3,
    #     state, tabs, undo, width, wrap,
    t.pack(padx=10, pady=5)
    
  • 另外做了兩個函數來通過兩個按鍵操作Entry框和Text框

    def insert_point():
        var = e.get()   # 從輸入框e獲取值
        t.insert('insert', var)     # 插入到Text框的光標處
        # t.insert(1.1, var)  # 在(1,1)位置插入(索引從0開始)
    
    def insert_end():
        var = e.get()   # 從輸入框e獲取值
        t.insert('end', var)    # 插入到Text框的末尾
        
    b1 = tk.Button(window, text='inster point', width=15, height=2, command=insert_point)
    b1.pack(pady=5)    # 放置Button
    b2 = tk.Button(window, text='inster end', width=15, height=2, command=insert_end)
    b2.pack(pady=5)    # 放置Button
    
  • 全部代碼

  • 參考文獻

    代碼主要來自Entry & Text 輸入, 文本框,略有改動

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章