-2

This code is from another user that had questions but I want to go a bit further. This button press will only print back to the console. What if I want this answer to print to a text box, how would I write that code?

import tkinter
from tkinter import Button

top = tkinter.Tk()

def callback():
    print ("click!")

button = Button(top, text="OK", command=callback)  
top.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
Raggaeboys
  • 15
  • 2
  • Possible duplicate of [Python tkinter: Make any output appear in a text box on GUI not in the shell](https://stackoverflow.com/questions/14879916/python-tkinter-make-any-output-appear-in-a-text-box-on-gui-not-in-the-shell) – CommonSense Dec 05 '18 at 14:32
  • 1
    You will need to have a textbox in the code to start with. Then simply use `textbox_name.insert("end", "click!")` to insert a value into the textbox. That said Tkinter is not as lacking as you may think. It has a large amount of widgets for various needs and for the most part can build anything GUI wise you need. There are other options for python like PyQt for GUI but I have only ever used Tkinter and it suits my needs just fine. – Mike - SMT Dec 05 '18 at 14:34
  • I was able to figure it out after many attempts. Thank you for guiding me. See my results below. – Raggaeboys Dec 05 '18 at 21:52
  • My insert needs to be done in a function. I also needed to call the correct function for the button. – Raggaeboys Dec 05 '18 at 21:53

1 Answers1

0
   import tkinter as tk
   from tkinter import *
   from tkinter import Label,Text


   top = tk.Tk()
   top.title("My App")
   top.geometry("600x500")

   def what_is_your_name():
       name1 = ('Hugh')
       name2 = (entry1.get())
       return name1 + name2

   def name_display():
       greeting = what_is_your_name()

    #-----textbox----
       textbox_name=tk.Text(master=top, height= 10, width=30)
       textbox_name.grid(column=2, row=5)

       textbox_name.insert(tk.END, greeting)


    #------labels------
    label1=tk.Label(text = "Enter your data here", font=("Helvetica", 10), fg="blue")
    label1.grid(column=0, row=1)

    #-----entry field-----
    entry1=tk.Entry()
    entry1.grid(column=0, row=0)



    #-----button-----
    button = tk.Button(text="Click Me", command=name_display)
    button.grid(column = 0, row=3)

top.mainloop()
Raggaeboys
  • 15
  • 2