Monday, 6 November 2023

Write Python GUI program which accepts a sentence from the user and alters it when a button is pressed. Every space should be replaced by *, case of all alphabets should be reversed, digits are replaced by?.

 

Write Python GUI program which accepts a sentence from the user and alters it when a button is pressed. Every space should be replaced by *, case of all alphabets should be reversed, digits are replaced by?.  

 

import tkinter as tk

 

def alter_sentence():

    sentence = entry.get()

    altered_sentence = ""

 

    for char in sentence:

        if char.isalpha():

            if char.islower():

                altered_sentence += char.upper()

            else:

                altered_sentence += char.lower()

        elif char.isdigit():

            altered_sentence += "?"

        elif char.isspace():

            altered_sentence += "*"

        else:

            altered_sentence += char

 

    result_label.config(text=altered_sentence)

 

# Create the main window

root = tk.Tk()

root.title("Sentence Alteration")

 

# Entry widget for entering the sentence

entry = tk.Entry(root, width=50)

entry.pack()

 

# Button to alter the sentence

alter_button = tk.Button(root, text="Alter", command=alter_sentence)

alter_button.pack()

 

# Label to display the altered sentence

result_label = tk.Label(root, text="", font=("Arial", 12))

result_label.pack()

 

root.mainloop()