Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | GUI Calendar using Tkinter
Next article icon

Python offers multiple options for developing a 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 with Python. 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 information, refer to
Python GUI – tkinter
 

Creating Tables Using Tkinter


A table is useful to display data in the form of rows and columns. Unfortunately, Tkinter does not provide a Table widget to create a table. But we can create a table using alternate methods. For example, we can make a table by repeatedly displaying entry widgets in the form of rows and columns.
To create a table with five rows and four columns we can use two for loops as: 
 

for i in range(5):    for j in range(4):


Inside these loops, we have to create an Entry widget by creating an object of Entry class, as: 
 

e = Entry(root, width=20, fg='blue', font=('Arial', 16, 'bold')


Now, we need logic to place this Entry widget in rows and columns. This can be done by using grid() method to which we can pass row and column positions, as: 
 

# here i and j indicate # row and column positionse.grid(row=i, column=j)


We can insert data into the Entry widget using insert() method, as: 
 

e.insert(END, data)


Here, 'END' indicates that the data continuous to append at the end of previous data in the Entry widget.
This is the logic that is used in the program given below using the data that is coming from a list. We have taken a list containing 5 tuples and each tuple contains four values which indicate student id, name, city and age.
Hence, we will have a table with 5 rows and 4 columns in each row. This program can also be applied on data coming from a database to display the entire data in the form of a table.
Source Code: 
 

Python3
# Python program to create a tablefromtkinterimport*classTable:def__init__(self,root):# code for creating tableforiinrange(total_rows):forjinrange(total_columns):self.e=Entry(root,width=20,fg='blue',font=('Arial',16,'bold'))self.e.grid(row=i,column=j)self.e.insert(END,lst[i][j])# take the datalst=[(1,'Raj','Mumbai',19),(2,'Aaryan','Pune',18),(3,'Vaishnavi','Mumbai',20),(4,'Rachna','Mumbai',21),(5,'Shubham','Delhi',21)]# find total number of rows and# columns in listtotal_rows=len(lst)total_columns=len(lst[0])# create root windowroot=Tk()t=Table(root)root.mainloop()

Output: 
 


 


Create Table Using Tkinter
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