Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Search String in Text using Python-Tkinter
Next article icon
Tkinter has a number of widgets to provide functionality in any GUI. It also supports a variety of universal widget methods which can be applied on any of the widget.focus_get() andfocus_set() methods are also universal widget methods. They can also be applied onTk() method.

focus_set() method-

This method is used to set the focus on the desired widget if and only if the master window is focused.Syntax:
widget.focus_set()
Below is the Python program-Python3
# Importing tkinter module# and all functionsfromtkinterimport*fromtkinter.ttkimport*# creating master windowmaster=Tk()# Entry widgete1=Entry(master)e1.pack(expand=1,fill=BOTH)# Button widget which currently has the focuse2=Button(master,text="Button")# here focus_set() method is used to set the focuse2.focus_set()e2.pack(pady=5)# Radiobuton widgete3=Radiobutton(master,text="Hello")e3.pack(pady=5)# Infinite loopmainloop()
Output:You may observe in above image that
Button widget has the focus.For better understanding copy and run above program.

focus_get() method-

This method returns the name of the widget which currently has the focus.Syntax:
master.focus_get()
Note: You can use it with any of the widget, master in not necessary.Below is the Python program -Python3
# Importing tkinter module# and all functionsfromtkinterimport*fromtkinter.ttkimport*# creating master windowmaster=Tk()# This method is used to get# the name of the widget# which currently has the focus# by clicking Mouse Button-1deffocus(event):widget=master.focus_get()print(widget,"has focus")# Entry widgete1=Entry(master)e1.pack(expand=1,fill=BOTH)# Button Widgete2=Button(master,text="Button")e2.pack(pady=5)# Radiobutton widgete3=Radiobutton(master,text="Hello")e3.pack(pady=5)# Here function focus() is binded with Mouse Button-1# so every time you click mouse, it will call the# focus method, defined abovemaster.bind_all("<Button-1>",lambdae:focus(e))# infinite loopmainloop()
Output: Every time you click on any widget OR if you click the mouse button-1 above program will print the name of the widget which has the focus. For better understanding copy and run above program.
.!radiobutton has focus.!entry has focus.!button has focus

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp