Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need display to be on 3 lines not 1 line

Tags:

python

tkinter

My desired outcome is to have a python window with two buttons "Show Info" "Quit" next to each other and have the "Show Info" button display my name and address on 3 separate lines and then stop the program when "Quit" is clicked. I'm almost there - the text is all coming on one line though.

thanks in advance.

# This program will demonstrate a button widget within a dialog box
# that shows my information.

# Call TK interface
import tkinter
# Call message box
import tkinter.messagebox

# Create class for GUI.


class MyGUI:
    def __init__(self):
        #Create the main window widget.
        self.main_window = tkinter.Tk()

        #Create button widget 1.
        self.my_button = tkinter.Button(self.main_window, \
                                        text='Show Info', \
                                        command=self.show_info)

        #Creat a quit button.
        self.quit_button = tkinter.Button(self.main_window, \
                                          text='Quit', \
                                          command=self.main_window.destroy)

        # Pack the buttons.
        self.my_button.pack(side='left')
        self.quit_button.pack(side='left')

        #Enter the tkinter main loop.
        tkinter.mainloop()

        #The do_somethings will be defined.
    def show_info(self):
        tkinter.messagebox.showinfo('Text', \
                                    'My Name'
                                    '123 Any Rd'
                                    'Town Fl, 12345')




my_gui = MyGUI()
like image 876
user3780539 Avatar asked Sep 01 '25 10:09

user3780539


1 Answers

The line breaks where you have defined your 3 lines don't do what you think ... Python will smash those strings together as if there was no return there at all (which is what you are seeing). Instead, try putting this there:

def show_info(self):
    lines = ['My Name', '123 Any Rd', 'Town Fl, 12345']
    tkinter.messagebox.showinfo('Text', "\n".join(lines))
like image 96
Ajean Avatar answered Sep 04 '25 04:09

Ajean