Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to create a frame inside a frame tkinter?
Next article icon

Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped withPython. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.

Note: For more reference, you can read our article, 

  1. What is Widgets
  2. Python Tkinter Overview
  3. Python Tkinter Tutorial

What is Frame in Tkinter?

A frame is a rectangular region on the screen. A frame can also be used as a foundation class to implement complex widgets. It is used to organize a group of widgets.

Tkinter Frame WidgetSyntax

The syntax to use the frame widget is given below.

Syntax: w = frame( master, options)

Parameters:

  • master: This parameter is used to represent the parent window.
  • options: There are many options which are available and they can be used as key-value pairs separated by commas.

Tkinter Frame Options

The following are commonly used Options that can be used with this widget:

  • bg:This option used to represent the normal background color displayed behind the label and indicator.
  • bd:This option used to represent the size of the border around the indicator and the default value is 2 pixels.
  • cursor:By using this option, the mouse cursor will change to that pattern when it is over the frame.
  • height:The vertical dimension of the new frame.
  • highlightcolor:This option used to represent the color of the focus highlight when the frame has the focus.
  • highlightthickness:This option used to represent the color of the focus highlight when the frame does not have focus.
  • highlightbackground:This option used to represent the thickness of the focus highlight..
  • relief: The type of the border of the frame. It's default value is set to FLAT.
  • width:This option used to represents the width of the frame.

Frame Widget in Tkinter Example

In this example, below code defines functions to create Tkinter widgets with customized options, facilitating widget creation with fewer lines of code. It generates a GUI with a main window containing a frame, labels, and buttons, all with specified options like background color, border size, cursor style, and more, enhancing visual consistency and usability.

Python3
importtkinterastk# Function to create widgets with all optionsdefcreate_widget(parent,widget_type,**options):returnwidget_type(parent,**options)# Create the main windowwindow=create_widget(None,tk.Tk)window.title("GUI Example")# Create a Frame widget with all optionsframe=create_widget(window,tk.Frame,bg='lightblue',bd=3,cursor='hand2',height=100,highlightcolor='red',highlightthickness=2,highlightbackground='black',relief=tk.RAISED,width=200)frame.pack(padx=20,pady=20)# Create Label widget with all optionslabel=create_widget(frame,tk.Label,text='GeeksForGeeks',font='50',bg='lightblue',bd=3,cursor='hand2',highlightcolor='red',highlightthickness=2,highlightbackground='black',relief=tk.RAISED)label.pack()# Create a frame for buttonsbutton_frame=create_widget(window,tk.Frame,bg='lightblue',bd=3,cursor='hand2',height=50,highlightcolor='red',highlightthickness=2,highlightbackground='black',relief=tk.RAISED,width=200)button_frame.pack(pady=10)# Function to create buttons with all optionsdefcreate_button(parent,text,fg):returncreate_widget(parent,tk.Button,text=text,fg=fg,bg='lightblue',bd=3,cursor='hand2',highlightcolor='red',highlightthickness=2,highlightbackground='black',relief=tk.RAISED)# Create buttonsbuttons_info=[("Geeks1","red"),("Geeks2","brown"),("Geeks3","blue"),("Geeks4","green"),("Geeks5","green"),("Geeks6","green")]fortext,fginbuttons_info:button=create_button(button_frame,text=text,fg=fg)button.pack(side=tk.LEFT)# Run the Tkinter event loopwindow.mainloop()

Output

Tkinter Frame Widget
Python Tkinter - Frame Widget



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