0

I like to show console window output result in tkinter window. How to do this?

Output

I want Output Like this

Here is my sample code:

from tkinter import*
from tkinter.ttk import *
window = Tk()
space = ""
window.title(80*space + "Addition and Subtraction")
window.geometry("800x600+100+100")
n1 = StringVar()
n2 = StringVar()
def result() :
   if combo1.get() == "Addition" :
     Res = int(n1.get())+int(n2.get())
     print("You have selected Addition, Answer is : ", Res)
   else :
     Res = int(n1.get())-int(n2.get())
     print("You have selected Subtraction, Answer is : ", Res)
combo1 = Combobox(window,width=50)
lbl1 = Label(window,text='  Select   Work Type :').grid(row=3,column=1,pady=10)
combo1['values']= ("Addition","Subtraction")
combo1.current(0)
combo1.grid(row=3,column=2)
lbl2 = Label(window,text='Enter First Number   : ').grid(row=4,column=1)
txt2 = Entry(window,textvariable=n1,width=20).grid(row=4,column=2,pady=5)
lbl3 = Label(window,text='Enter Second Number  : ').grid(row=5,column=1)
txt3 = Entry(window,textvariable=n2,width=20).grid(row=5,column=2,pady=5)
but1 = Button(window,text="Result",command=result).grid(row=10,column=2)
txt4 = Text(window,width=30,height=15).grid(row=15,column=2)
window.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
muthu
  • 1
  • 1
  • There's no simple way to do it, but the `errorwindow.py` module shown in [my answer](https://stackoverflow.com/a/49016673/355230) to another question makes it relatively easy. – martineau Oct 20 '20 at 16:02
  • You can use `txt4.insert(...)` to replace the `print(...)` statements. But first change `txt4 = Text(...).grid(...)` to `txt4 = Text(...)` and `txt4.grid(...)`. – acw1668 Oct 21 '20 at 03:37

1 Answers1

0

In order to Align the text from an entry you can use the keyword "justify" Entry documentation

If you want to display information inside a Label, you can use the keyword "text" Label documentation

You can use both keyword inside your definition

txt2 = Entry(window, text=n1, width=20, justify="left")

or you can use them afterward (for a dynamic use)

lbl2 = Label(window,text='Enter First Number   : ').grid(row=4,column=1)
lbl2["text"] = "text has been changed"

Note : There is other way to justify your content so be sure to check "anchor" keyword from Label or Entry but also "sticky" from the grid function

Herue
  • 1
  • 2