Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add indentation inside tkinter text box?

-EDIT I used the word indentation incorrectly in this question. I meant to to refer to the starting of new lines.


I’m creating a python weather app with the tkinter UI library, It works but using the Open weather map API. The user will input a city name (CITY var) and using a predefined link body the program plugs in the city name (CITY var) and requests the city’s weather data from the open weather map site using that link. The whole process of requesting the data is handled by this function

 def getData(CITY):
    BASE_URL = "https://api.openweathermap.org/data/2.5/weather?"
    API_KEY = "MY-API-KEY"

    #our url to send to
    URL = BASE_URL + "q=" + CITY + "&appid=" + API_KEY

    # HTTP request
    response = requests.get(URL)

    # checking the status code of the request
    if response.status_code == 200:
        # getting data in the json format
        data = response.json()
        # getting the main dict block
        main = data['main']
        # getting temperature
        temperature = main['temp']
        # getting the humidity
        humidity = main['humidity']
        # getting the pressure
        pressure = main['pressure']
        # weather report
        report = data['weather']
        # print info
        print(f"{CITY:-^30}")
        print(f"Temperature: {temperature}")
        print(f"Humidity: {humidity}")
        print(f"Pressure: {pressure}")
        print(f"Weather Report: {report[0]['description']}")



    else:
        # showing the error message
        print("Error in the HTTP request")

When run with the city name new York the terminal outputs something like this.
output in terminal
Its clean and has good indents. But now I need to put this info into the apps text box’s. I do this using this code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

City.set(CITY)

#add the citys name to a list of all citys 
History.tag_configure("right", justify='right')
History.insert("1.0", f"{CITY:-^30}")
History.tag_add("right", "1.0", "end")

#adds info to main textbox
InfoBox.tag_configure("right", justify='right')
InfoBox.delete('1.0', END)
InfoBox.insert("1.0", (f"{CITY:-^30}"))
InfoBox.insert("1.0", (f"Temperature: {temperature}"))
InfoBox.insert("1.0", (f"Humidity: {humidity}"))
InfoBox.insert("1.0", (f"Pressure: {pressure}"))
InfoBox.insert("1.0", (f"Weather Report: {report[0]['description']}"))
InfoBox.tag_add("right", "1.0", "end")

#add city info to our info box with all previous citys info
CityUpdate.tag_configure("right", justify='right')
CityUpdate.insert("1.0", (f"{CITY:-^30}"))
CityUpdate.insert("1.0", (f"Temperature: {temperature}"))
CityUpdate.insert("1.0", (f"Humidity: {humidity}"))
CityUpdate.insert("1.0", (f"Pressure: {pressure}"))
CityUpdate.insert("1.0", (f"Weather Report: {report[0]['description']}"))
CityUpdate.tag_add("right", "1.0", "end")

The issue comes up here when the code shows something like this in the text boxes.
output in app
Its the correct information but the indentation is gone. My question is how can I get that indentation back so it looks as good as it was in the terminal?

>Solution :

I don’t see a problem with the indentation. What I see is that you’re failing to add a newline when inserting a line of text. You also keep inserting at the start instead of appending at the end which is unusual; I don’t know whether or not that is intentional.

To add a newline, your insert statement should look something like this:

InfoBox.insert("1.0", (f"{CITY:-^30}\n"))

As to the question in the title, the text widget supports tabs via the tabs configuration option. By default tab stops are every 8 average-sized characters but you have complete control over the placement of tabstops, as well as how text is aligned to those tab stops (eg: left, center, right, or numeric).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading