About Lesson
In this TKinter Tutorial we are going to learn creating TKinter TextBox, for tkinter textbox also we can call TextEntry.
This is the complete code for this lesson.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import tkinter as tk from tkinter import ttk class Window(tk.Tk): def __init__(self): super(Window, self).__init__() self.title("TextBox") self.minsize(500,400) self.wm_iconbitmap("myicon.ico") self.text_entry() def click_me(self): self.label.configure(text = "Hello " + self.name.get()) self.label.config(font=("Courier", 20)) def text_entry(self): self.name = tk.StringVar() self.label = ttk.Label(self, text = "Enter Your Name") self.label.grid(column = 0, row = 0) self.textbox = ttk.Entry(self, width = 20, textvariable = self.name) self.textbox.grid(column=0, row=1) self.button = ttk.Button(self, text = "Click ME", command = self.click_me) self.button.grid(column=0, row=2) window = Window() window.mainloop() |
You can use tkinter.Entry() for creating of the TextBox or EntryBox in tkinter, also you can give the width of the textbox.
1 |
self.textbox = ttk.Entry(self, width = 20, textvariable = self.name) |
You can add your textbox in the grid layout using this code.
1 |
self.textbox.grid(column=0, row=1) |
In here we have created our button , we have added a command in the button, because we want to connect this command with the click_me() method, also you need to add your button in the grid layout, specifying the row and column number.
1 2 |
self.button = ttk.Button(self, text = "Click ME", command = self.click_me) self.button.grid(column=0, row=2) |
And this is the method that we have already connected with the command button, basically in this method we are printing the textbox input in the label.
1 2 3 |
def click_me(self): self.label.configure(text = "Hello " + self.name.get()) self.label.config(font=("Courier", 20)) |
Run the complete code and this will be the result.