Rony K
Rony K

Reputation: 1

How to set a string to the text of a label in Tkinter?

I am trying to set a string as the text of a label in tkinter program but it is not visible on the window. Please help me. A piece of code is here :

str=input()   
lbl=Label(win,text=str)
lbl.grid(row=11, column=1, ipadx=20, ipady=10, sticky=W + S)

Upvotes: 0

Views: 514

Answers (2)

SNR
SNR

Reputation: 772

This code probably will work,

from tkinter import *

class Application:
    def __init__(self):
        self.my_string = ""

        self.root = Tk()
        self.root.geometry("300x200+50+50")
        self.entry = Entry(self.root)
        self.entry.bind("<Return>", self.onReturn)
        self.entry.pack()

        self.my_label = Label(self.root, text="Initial text!")
        self.my_label.pack()

        self.root.mainloop()

    def onReturn(self, event):
        self.my_label.config(text=self.entry.get())
        self.entry.delete(0, END)

    Application()   

Upvotes: 0

user3608646
user3608646

Reputation: 13

str is a keyword in Python. Try renaming it to str1

from tkinter import *
str1=input()
root = Tk()
lbl=Label(text=str1)
lbl.grid(row=11, column=1, ipadx=20, ipady=10, sticky=W + S)
root.mainloop()

This is what I am getting

Click for the output of the above

Upvotes: 1

Related Questions