Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Create a GUI Marksheet using Tkinter

Creating a GUI Marksheet using tkinter allows students to input their marks for different subjects, and then the application calculates the total, average, and grade. Here's a step-by-step tutorial on how to create such a Marksheet:

GUI Marksheet using Tkinter Tutorial:

1. Import Required Libraries:

import tkinter as tk
from tkinter import messagebox

2. Create the Main Application Window:

root = tk.Tk()
root.title("GUI Marksheet")

3. Define the Calculation Function: This function will compute the total, average, and grade when the user presses the "Calculate" button.

def calculate():
    try:
        # Retrieve marks from Entry widgets and convert to integers
        m1 = int(mark1_entry.get())
        m2 = int(mark2_entry.get())
        m3 = int(mark3_entry.get())
        
        # Calculate total and average
        total = m1 + m2 + m3
        avg = total / 3
        
        # Determine grade
        if avg >= 90:
            grade = "A"
        elif avg >= 75:
            grade = "B"
        elif avg >= 60:
            grade = "C"
        elif avg >= 45:
            grade = "D"
        else:
            grade = "F"
        
        # Display the results
        total_label.config(text="Total: " + str(total))
        avg_label.config(text="Average: {:.2f}".format(avg))
        grade_label.config(text="Grade: " + grade)
        
    except ValueError:
        messagebox.showerror("Error", "Please enter valid marks!")

4. Create Labels and Entry Fields for Subject Marks:

# Subject names
subjects = ["Maths", "Physics", "Chemistry"]

# Labels and Entries for each subject
tk.Label(root, text="Marks for:").grid(row=0, column=0)
for index, subject in enumerate(subjects, start=1):
    tk.Label(root, text=subject).grid(row=index, column=0)
    tk.Entry(root, width=10).grid(row=index, column=1)

5. Create the Button to Trigger the Calculation:

tk.Button(root, text="Calculate", command=calculate).grid(row=4, column=0, columnspan=2)

6. Display Labels for Total, Average, and Grade:

total_label = tk.Label(root, text="Total: ")
total_label.grid(row=5, column=0, columnspan=2)

avg_label = tk.Label(root, text="Average: ")
avg_label.grid(row=6, column=0, columnspan=2)

grade_label = tk.Label(root, text="Grade: ")
grade_label.grid(row=7, column=0, columnspan=2)

7. Run the Main Loop:

root.mainloop()

Complete Code: Putting all the above components together, you get the following code:

import tkinter as tk
from tkinter import messagebox

def calculate():
    # ... [Same as above]

root = tk.Tk()
root.title("GUI Marksheet")

# ... [Same as above for labels, entries, and buttons]

root.mainloop()

After running the code, you'll see a GUI window where you can input marks for three subjects. After inputting the marks and pressing the "Calculate" button, the application will display the total, average, and grade. Adjust the subjects and grading scale as needed!

  1. Python Tkinter Marksheet example:

    • Description: Create a basic Tkinter application for a Marksheet.
    • Code Example:
      import tkinter as tk
      
      def calculate_grade():
          total_marks = int(entry_total_marks.get())
          obtained_marks = int(entry_obtained_marks.get())
      
          percentage = (obtained_marks / total_marks) * 100
          grade = 'A' if percentage >= 90 else 'B' if percentage >= 80 else 'C' if percentage >= 70 else 'D'
      
          label_result.config(text=f"Percentage: {percentage:.2f}%\nGrade: {grade}")
      
      root = tk.Tk()
      root.title("Marksheet Application")
      
      label_total_marks = tk.Label(root, text="Total Marks:")
      label_total_marks.grid(row=0, column=0, padx=10, pady=10)
      
      entry_total_marks = tk.Entry(root)
      entry_total_marks.grid(row=0, column=1, padx=10, pady=10)
      
      label_obtained_marks = tk.Label(root, text="Obtained Marks:")
      label_obtained_marks.grid(row=1, column=0, padx=10, pady=10)
      
      entry_obtained_marks = tk.Entry(root)
      entry_obtained_marks.grid(row=1, column=1, padx=10, pady=10)
      
      button_calculate = tk.Button(root, text="Calculate Grade", command=calculate_grade)
      button_calculate.grid(row=2, column=0, columnspan=2, pady=20)
      
      label_result = tk.Label(root, text="")
      label_result.grid(row=3, column=0, columnspan=2)
      
      root.mainloop()
      
  2. Tkinter checkbutton for Marksheet options:

    • Description: Add checkbuttons to the Marksheet application for additional options.
    • Code Example:
      import tkinter as tk
      
      def calculate_grade():
          total_marks = int(entry_total_marks.get())
          obtained_marks = int(entry_obtained_marks.get())
      
          percentage = (obtained_marks / total_marks) * 100
          grade = 'A' if percentage >= 90 else 'B' if percentage >= 80 else 'C' if percentage >= 70 else 'D'
      
          if var_remarks.get():
              remarks = entry_remarks.get()
              label_result.config(text=f"Percentage: {percentage:.2f}%\nGrade: {grade}\nRemarks: {remarks}")
          else:
              label_result.config(text=f"Percentage: {percentage:.2f}%\nGrade: {grade}")
      
      root = tk.Tk()
      root.title("Marksheet Application")
      
      label_total_marks = tk.Label(root, text="Total Marks:")
      label_total_marks.grid(row=0, column=0, padx=10, pady=10)
      
      entry_total_marks = tk.Entry(root)
      entry_total_marks.grid(row=0, column=1, padx=10, pady=10)
      
      label_obtained_marks = tk.Label(root, text="Obtained Marks:")
      label_obtained_marks.grid(row=1, column=0, padx=10, pady=10)
      
      entry_obtained_marks = tk.Entry(root)
      entry_obtained_marks.grid(row=1, column=1, padx=10, pady=10)
      
      var_remarks = tk.BooleanVar()
      check_remarks = tk.Checkbutton(root, text="Add Remarks", variable=var_remarks)
      check_remarks.grid(row=2, column=0, columnspan=2, pady=10)
      
      label_remarks = tk.Label(root, text="Remarks:")
      label_remarks.grid(row=3, column=0, padx=10, pady=10)
      
      entry_remarks = tk.Entry(root)
      entry_remarks.grid(row=3, column=1, padx=10, pady=10)
      
      button_calculate = tk.Button(root, text="Calculate Grade", command=calculate_grade)
      button_calculate.grid(row=4, column=0, columnspan=2, pady=20)
      
      label_result = tk.Label(root, text="")
      label_result.grid(row=5, column=0, columnspan=2)
      
      root.mainloop()