Search code examples
pythontkintertkinter-entrytkinter-text

How to get the text input from a tkinter text box?


I've made this converter. I want it to convert decimal values into binary and hexadecimal using Tkinter.. I made a text input box but I don't know how to get the input values from the input box. We aren't supposed to use OOP so we can't use classes. Here is my code (it's in french for some parts) :

import tkinter as tk
from tkinter import *

def ConverterWind():

    convertisseur = Tk()
    convertisseur.title("Convertisseur")

    inputZone = Text(convertisseur, height=2, width=50)
    inputZone.pack()
    getTextArea = Button(convertisseur, text = "Convertir !", command = getText)
    getTextArea.pack()

    convertisseur.mainloop


MainMenu = Tk()
MainMenu.title("Choix de Modes")

button1 = Button(MainMenu, text = "convertisseur", command = ConverterWind)
button1.pack(side = LEFT, padx= 10, pady = 10)

button2 = Button(MainMenu, text = "QUITTER", command = MainMenu.destroy)
button2.pack(side = RIGHT, padx= 10, pady = 10)

MainMenu.mainloop()

Solution

  • You can use the Text.get(Index1,Index2) to get the text from Text widget

    Try this.

    import tkinter as tk
    from tkinter import *
    
    
    def getText(inputText):
        print(inputText.get(1.0,END))
    
    def ConverterWind():
    
        convertisseur = Tk()
        convertisseur.title("Convertisseur")
    
        inputZone = Text(convertisseur, height=2, width=50)
        inputZone.pack()
        getTextArea = Button(convertisseur, text = "Convertir !", command = lambda: getText(inputZone))
        getTextArea.pack()
    
        convertisseur.mainloop
    
    
    MainMenu = Tk()
    MainMenu.title("Choix de Modes")
    
    button1 = Button(MainMenu, text = "convertisseur", command = ConverterWind)
    button1.pack(side = LEFT, padx= 10, pady = 10)
    
    button2 = Button(MainMenu, text = "QUITTER", command = MainMenu.destroy)
    button2.pack(side = RIGHT, padx= 10, pady = 10)
    
    MainMenu.mainloop()