I am trying to learn how to use Tkinter, but whenever I want to execute my code I always get this problem: (NameError: name 'label' is not defined) or (NameError: name 'button' is not defined).
As a beginner, it seems to me that the problem is with my code editor. (BTW I am using VScode)
this is my code:
from tkinter import *
root = Tk()
mylabel = label(root, text='Hello World')
mylabel.pack()
root.mainloop()
And as I said, this also happens with this one:
from tkinter import *
root = Tk()
mybutton = button(root, text='Hello World')
mybutton.pack()
root.mainloop()
you have caps error its Button and Label not button and label
Related
this question will be relatively simple, just do not get anywhere. It's more of a basic question.
When I write
print ("hello world")
it appears in the script program in the message box bellow. But now I want it to open in a separate fesnter, created with tkinter. I wrote it down like this (see picture or code), but I know that print itself must not be in brackets, how can I solve this problem?
from tkinter import *
a = ("Hello World")
root = Tk()
T = Text(root, height=50, width=150)
T.pack() T.insert(END, print a)
mainloop()
The "hello world" must appear in a separately-opened window.
I am happy about any answer.
Thanks in advance
PyBeginner
I didn't understand your question but can you try this one if it helps .
from tkinter import *
root = Tk()
a = Label(root, text ="Hello World")
a.pack()
root.mainloop()
Resource: https://www.geeksforgeeks.org/python-tkinter-messagebox-widget/ Also, if you want to you can delete showinfo code and just run the w.
from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("400x300")
w = Label(root, text='Hello World!', font="50")
w.pack()
messagebox.showinfo("Hello World", "This is a Hello World message!!!")
root.mainloop()
I'm trying to write simple things in a Tkinter module using python 3.6 in Anaconda. This is my code
from tkinter import *
root = Tk()
thelabel = label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
but I get the error:
TclError: can't invoke "label" command: application has been destroyed
ERROR: execution aborted
and I keep getting a blank Tkinter window whenever I run my code and I don't understand what is the problem, thanks:)
You have a small error...
Here is the correct way:
from tkinter import *
root = Tk()
thelabel = Label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
You should try reading a bit on tkinter.
Here are some references specifically on label.
Hope you find this helpful!
change your code to this
from tkinter import *
root = Tk()
thelabel = Label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
Labels start with a capital L not small l it will fix your error
Hello I'm obviously not too experienced with tkinter very much and I couldnt find anything on what I was looking for, maybe someone could help me
def hide(x):
x.pack_forget()
d=Button(root, text="Click to hide me!" command=hide(d))
d.pack()
I would like it so that on click the command runs but the button is not defined when calling the command
You can not use anything if you are still building. Must you use configure and lambda function:
from tkinter import *
def hide(x):
x.pack_forget()
root = Tk()
d=Button(root, text="Click to hide me!")
d.configure(command=lambda: hide(d))
d.pack()
root.mainloop()
First, define the button, then add the command with the config method.
from tkinter import *
root = Tk()
def hide(x):
x.pack_forget()
d=Button(root, text="Click to hide me!")
d.pack()
d.config(command=lambda: hide(d))
root.mainloop()
I'm trying to bind a button to a simple function.
This is my code:
from Tkinter import *
root=Tk()
def printName(event):
print 'hi my name is Beni'
button_1.bind("<Button-1>",printName)
button_1.pack()
root.mainloop()
The error I get is:
TclError: can't invoke "bind" command: application has been destroyed
Any ideas?
You need to define button_1.
For example:
button_1 = Button(root, text="ButtonName")
So your entire code snippet would be:
from Tkinter import *
root=Tk()
def printName(event):
print('hi my name is Beni')
button_1 = Button(root, text="ButtonName")
button_1.bind("<Button-1>",printName)
button_1.pack()
root.mainloop()
Can anyone please explain why there are some functions unresolved like insert() and pack() for text widget particularly and there is an error in text = Text(root) line?
I have imported Tkinter and set PYTHONPATH to libs but I still can not run programm normally.
Thank you in advance
from Tkinter import *
import tkFileDialog
from nltk import *
import sentiment_analysis
root = Tk()
root.title('Semantic Orientation of the Text')
frame = Frame(root)
frame.pack()
text = Text(root)// error
text.tag_config("big", font=('Verdana', 14, 'normal'))
text.tag_config("color", font=('Times New Roman', 24))
text.tag_config("groove", relief=GROOVE, borderwidth=4)
text.pack(expand=YES, fill=BOTH) #pack() is unresolved
scroll = Tk.Scrollbar(text)
scroll.pack(side=RIGHT, fill=Y)
def onButtonText():
filename = tkFileDialog.askopenfilename(initialdir='C:/nltk_data/sentiment_analysis')
text.insert(END, open(filename).read()) #insert() in unresolved
There are also other functions for buttons' event handlers but they have the same mistake - insert() for text widget is unresolved
My guess is, since you are doing import *, you're importing two versions of Text so you're not getting the object you think you are.
There's really no good reason to do import *. Your code will be easier to maintain if you do something like this instead:
import Tkinter as tk
...
root = tk.Tk()
text = tk.Text(root, ...)