About Lesson
In this TKinter Tutorial we are going to learn about TKinter TextBox Focus Widget, we want to create a TextBox and after that we want to add focus to the textbox, also we want to learn how you can disable a button in tkinter.
Basically we are going to use the complete code from our previous lesson, that was create textbox in tkinter. and we want to add focus to the textbox.
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 44 45 46 47 48 |
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) #added focus to textbox self.textbox.focus() 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) #The button is disabled self.button.configure(state = "disabled") window = Window() window.mainloop() |
In here we have used two new line of codes, this is used for giving focus for the textbox or text entry.
1 |
self.textbox.focus() |
This is used for disabling the button.
1 |
self.button.configure(state = "disabled") |
Run the code and this will be the result, we have focus widget for the textbox and the buttons is disabled.