Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Menu widget in Tkinter
Next article icon
Before moving on to the topic lets see what isPython Tkinter. So, we all know that Python has different options for creating GUI(s) andtkinter is one of them. It is the standardGUI library for Python. And it makes the creation of GUI applications very quick still simple when python is merged with it. It also gives a very effective object-oriented interface to theTk GUI toolkit.Note: For more information, refer toPython GUI – tkinter

Widgets in tkinter

Moreover,Tkinter enables number of controls like labels, text boxes, list boxes, buttons, scrollbars etc, which are used in a GUI applications. These controls are known aswidgets.

Geometry management methods of tkinter

These methods are used to organize widgets across parents widget area. Moreover, these methods can be accessed by all the tkinter widgets. There are threegeometry management methods namelypack(),grid(), andplace(). All these methods have different roles.Now, lets discuss about the topicAutohiding Scrollbars using Python-tkinter.In this topic, we will see how auto-hiding scrollbars are created using tkinter in Python. So, firstly lets see the meaning of auto hiding scrollbars below:

Auto-hiding Scrollbars

When a scrollbar hides itself if its not required i.e, it is not visible when its not needed then that type of scrollbar is known asAuto-hiding Scrollbar. In PythonAutohiding scrollbars can be used withListbox andText widgets. It can be implemented using python tkinter with the help of some geometry management methods.Below examples illustrate the use ofAutohiding Scrollbars using Python-tkinter:Example 1:Python
# Python program to illustrate the usage of# autohiding scrollbars using tkinter# Importing tkinterfromtkinterimport*# Creating class AutoScrollbarclassAutoScrollbar(Scrollbar):# Defining set method with all# its parameterdefset(self,low,high):iffloat(low)<=0.0andfloat(high)>=1.0:# Using grid_removeself.tk.call("grid","remove",self)else:self.grid()Scrollbar.set(self,low,high)# Defining pack methoddefpack(self,**kw):# If pack is used it throws an errorraise(TclError,"pack cannot be used with\        this widget")# Defining place methoddefplace(self,**kw):# If place is used it throws an errorraise(TclError,"place cannot be used  with\        this widget")# creating tkinter windowroot=Tk()# Defining vertical scrollbarverscrollbar=AutoScrollbar(root)# Calling grid method with all its# parameter w.r.t vertical scrollbarverscrollbar.grid(row=0,column=1,sticky=N+S)# Defining horizontal scrollbarhoriscrollbar=AutoScrollbar(root,orient=HORIZONTAL)# Calling grid method with all its# parameter w.r.t horizontal scrollbarhoriscrollbar.grid(row=1,column=0,sticky=E+W)# Creating scrolled canvascanvas=Canvas(root,yscrollcommand=verscrollbar.set,xscrollcommand=horiscrollbar.set)canvas.grid(row=0,column=0,sticky=N+S+E+W)verscrollbar.config(command=canvas.yview)horiscrollbar.config(command=canvas.xview)# Making the canvas expandableroot.grid_rowconfigure(0,weight=1)root.grid_columnconfigure(0,weight=1)# creating canvas contentsframe=Frame(canvas)frame.rowconfigure(1,weight=1)frame.columnconfigure(1,weight=1)# Defining number of rows and columnsrows=20foriinrange(1,rows):forjinrange(1,9):button=Button(frame,padx=8,pady=8,text="[%d,%d]"%(i,j))button.grid(row=i,column=j,sticky='news')# Creating canvas windowcanvas.create_window(0,0,anchor=NW,window=frame)# Calling update_idletasks methodframe.update_idletasks()# Configuring canvascanvas.config(scrollregion=canvas.bbox("all"))# Calling mainloop methodroot.mainloop()
Output:

Improve
Article Tags :
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