Brendo
Brendo

Reputation: 1

How to print the same text multiple times(5 times) in tkinter/GUI?

   from tkinter import *

   root=Tk()
   textbox=Text(root)
   textbox.pack()
   button1=Button(root, text='Output Name', command=lambda : print('Hello'))
   button1.pack()
   def redirector(inputStr):
       textbox.insert(INSERT, inputStr)

   sys.stdout.write = redirector 

   root.mainloop()

This is my code with out a timer to do it five times.

Upvotes: 0

Views: 747

Answers (2)

Brian Haney
Brian Haney

Reputation: 57

This worked for me. It creates a table using the .messagebox module. You can enter your name into the entry label. Then, when you click the button it returns "Hello (name)".

from tkinter import *
from tkinter.messagebox import *

master = Tk()
label1 = Label(master, text = 'Name:', relief = 'groove', width = 19)
entry1 = Entry(master, relief = 'groove', width = 20)
blank1 = Entry(master, relief = 'groove', width = 20)


def show_answer():
    a = entry1.get()
    b = "Hello",a
    blank1.insert(0, b)


button1 = Button(master, text = 'Output Name', relief = 'groove', width = 20, command =show_answer)


#Geometry
label1.grid( row = 1, column = 1, padx = 10 )
entry1.grid( row = 1, column = 2, padx = 10 )
blank1.grid( row = 1, column = 3, padx = 10 )
button1.grid( row = 2, column = 2, columnspan = 2)

#Static Properties
master.title('Hello')

Upvotes: -1

APecha
APecha

Reputation: 128

This seems a little bit like homework, so lets try to get you on the right track over outright providing the code to accomplish this.

You're going to want to create a loop that performs your code a certain number of times. Let's say we just want to output a certain string 5 times. As an example, here's some really simple code:

def testPrint():
    print('I am text!')

for i in range(5):
    testPrint()

This will create a function called testPrint() that prints text "I am Text!", then run that function 5 times in a loop. If you can apply this to the section of code you need to run 5 times, it should solve the problem you are facing.

Upvotes: 2

Related Questions