I want to Print my output on pop up window? how should I approach?
Print message inside tkinter window rather than terminal
If you just want to print the message in the root
window, you can use a label
widget like this in the following code:
import tkinter as tkmaster = tk.Tk()def write_slogan(): info_message = "Your message" tk.Label(master, text=info_message).grid(row=2, column=1) btn = tk.Button(master, text='ORDER Number', command=write_slogan)btn.grid(row=3, column=1, sticky=tk.W, pady=4)master.mainloop()
Print message in a separate pop-up window
You can use a message box from the tkinter messagebox
module to pop up some info with your info message displayed in it.
import tkinter as tkimport tkinter.messagebox as tkmbdef write_slogan(): info_message = "Your message" # info message box tkmb.showinfo("Output", info_message)master = tk.Tk()btn = tk.Button(master, text='ORDER Number', command=write_slogan)btn.grid(row=3, column=1, sticky=tk.W, pady=4)master.mainloop()
Creating a toplevel window as a pop up window
import tkinter as tkimport tkinter.messagebox as tkmbdef write_slogan(): # toplevel window top_win = tk.Toplevel(master) top_win.title('Toplevel') info_message = "Your message" # info message box tk.Label(top_win, text=info_message).grid(row=0, column=0) master = tk.Tk()master.title('Master')btn = tk.Button(master, text='ORDER Number', command=write_slogan)btn.grid(row=3, column=1, sticky=tk.W, pady=4)master.mainloop()
So if you have a list of messages (fruits) to print, you can use the following method to save previous result and print the new one
import tkinter as tkwindow = tk.Tk()# list of fruitsfruits = ['Mango', 'Apple', 'Orange', 'Banana', ]#action to be performed when button clickeddef clicked(): for ind, fruit in enumerate(fruits): # print names in the tkinter window # create a label widget names_label = tk.Label(window) # give it a position using grid names_label.grid(row=int(ind)+1, column=0) # print the fruit name in the label names_label.config(text=fruit)btn = tk.Button(window, text="Print fruits", command=clicked)btn.grid(column=0, row=0, padx=30, pady=2)window.mainloop()
Output:
Click on
Print fruits
button to print all the fruit names
The important thing to understand here is the for
loop is used to create as many Label
widgets equal to the number of elements in the list and the list item index
is used for giving row
and column
positions to these label widgets.
Printing the names in a toplevel window
import tkinter as tkwindow = tk.Tk()# list of fruitsfruits = ['Mango', 'Apple', 'Orange', 'Banana', ]#action to be performed when button clickeddef clicked(): # create a toplevel top_window = tk.Toplevel(window) for ind, fruit in enumerate(fruits): # print names in the tkinter window # create a label widget in top_window names_label = tk.Label(top_window) # give it a position using grid names_label.grid(row=int(ind)+1, column=0) # print the fruit name in the label names_label.config(text=fruit)btn = tk.Button(window, text="Print fruits", command=clicked)btn.grid(column=0, row=0, padx=30, pady=2)window.mainloop()
Output: