-3

I am new in creating GUI. I am doing it in Python with Tkinter. In my program I calculate following characteristics

def my_myfunction():
    my code ...
    print("Centroid:", centroid_x, centroid_y)
    print("Area:", area)
    print("Angle:", angle)

I would like to ask for any help/tips how to display those values in GUI window or how to save them in .txt file so that I can call them in my GUI Thanks in advance

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56
A.Klim
  • 31
  • 1
  • 3
  • 7
  • 2
    Possible duplicate of [Python How to simply redirect output of print to a TXT file with a new line created for each redirect](https://stackoverflow.com/questions/4110891/python-how-to-simply-redirect-output-of-print-to-a-txt-file-with-a-new-line-crea) – Vladislavs Burakovs Aug 09 '17 at 08:13
  • You want to display the ouput of `print` as tha title says or the values of `centroid_x`, `centroid_y`, etc. as the question suggests? – Stop harming Monica Aug 09 '17 at 08:14
  • You question is simple enough to answer but you should always provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) so people can better understand the context of your question/problem and help the specific issues. – Mike - SMT Aug 09 '17 at 13:34

2 Answers2

0

Tkinter is easy and an easy way to do a GUI, but sometimes it can be frustrating. But you should have read the docs before. However, you can do in this way.

from tkinter import *

yourData = "My text here"

root = Tk()
frame = Frame(root, width=100, height=100)
frame.pack()

lab = Label(frame,text=yourData)
lab.pack()

root.mainloop()
decadenza
  • 2,380
  • 3
  • 18
  • 31
0

There are several ways to display the results of any operation in tkiner.

You can use Label, Entry, Text, or even pop up messages boxes. There are some other options but these will probably be what you are looking for.

Take a look at the below example.

I have a simple adding program that will take 2 numbers and add them together. It will display the results in each kind of field you can use as an output in tkinter.

import tkinter as tk
from tkinter import messagebox

class App(tk.Frame):

    def __init__(self, master):
        self.master = master

        lbl1 = tk.Label(self.master, text = "Enter 2 numbers to be added \ntogether and click submit")
        lbl1.grid(row = 0, column = 0, columnspan = 3)

        self.entry1 = tk.Entry(self.master, width = 5)
        self.entry1.grid(row = 1, column = 0)

        self.lbl2 = tk.Label(self.master, text = "+")
        self.lbl2.grid(row = 1, column = 1)

        self.entry2 = tk.Entry(self.master, width = 5)
        self.entry2.grid(row = 1, column = 2)

        btn1 = tk.Button(self.master, text = "Submit", command = self.add_numbers)
        btn1.grid(row = 2, column = 1)

        self.lbl3 = tk.Label(self.master, text = "Sum = ")
        self.lbl3.grid(row = 3, column = 1)

        self.entry3 = tk.Entry(self.master, width = 10)
        self.entry3.grid(row = 4, column = 1)

        self.text1 = tk.Text(self.master, height = 1, width = 10)
        self.text1.grid(row = 5, column = 1)

    def add_numbers(self):

        x = self.entry1.get()
        y = self.entry2.get()

        if x != "" and y != "":
            sumxy = int(x) + int(y)

            self.lbl3.config(text = "Sum = {}".format(sumxy))

            self.entry3.delete(0, "end")
            self.entry3.insert(0, sumxy)

            self.text1.delete(1.0, "end")
            self.text1.insert(1.0, sumxy)

            messagebox.showinfo("Sum of {} and {}".format(x,y), 
                                "Sum of {} and {} = {}".format(x, y, sumxy))


if __name__ == "__main__":

    root = tk.Tk()
    myapp = App(root)
    root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79