-3

I am trying to get text that a user has inputted and then alternate the case of the text - e.g. If you input 'hello' it will output 'HeLlO'

This is what my Tkinter window looks like

Here is the code I have:



from itertools import cycle
from tkinter import *


win = Tk()
win.title("Alternator")
win.resizable(0,0)


def Alternate():

   func = cycle([str.upper, str.lower])
   result = ''.join(next(func)(c) for c in entry.get() if c != '  ')
   return result




l1 = Label(win, text="Enter text: ")
entry = Text(win, width=50, height = 3, wrap=WORD)
button = Button(win, text="Alternator", width=20)
l2 = Label(win, text="Alternated text:")
output = Text(win, width=50, height=3, wrap=WORD)

l1.grid(row=1, column=1, padx=5, sticky=W)
entry.grid(row=2, column=1, columnspan=2, padx=5, pady=(0,10))
button.grid(row=3, column=1, columnspan=2, pady=5)
l2.grid(row=4, column=1, padx=5, sticky=W)
output.grid(row=5, column=1, columnspan=2, padx=5, pady=(0,10))

button.configure(command=Alternate)

win.mainloop()

What is the correct way to go about this problem? I have seen various questions related, but none that cover this at a basic level

mnine
  • 33
  • 4

2 Answers2

0

To get the text you need to give 2 parameters to entry.get(). Here is a nice answer explaining which. After that you have to set the text with output.insert(). You can change this depending on what you want to happen, right now it adds a new line with the alternated text from above.

def Alternate():
   func = cycle([str.upper, str.lower])
   result = ''.join(next(func)(c) for c in entry.get("1.0",END) if c != '  ')
   output.insert(END,result)
   #return result
Finn
  • 2,333
  • 1
  • 10
  • 21
-1

I have some important tips and facts here. First of all, instead of using the Text() widget, you can use the Entry() widget as you are only giving simple words as input. Then you can get the characters inside the Entry like this

variable = name_of_entry.get()

Then you can alternate the casing of the letters and display the jumble-cased letters as simple text in Label().

name_of_label.configure(text=variable)
Pranav Krishna S
  • 324
  • 2
  • 13