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
Related
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
I'm getting this issue as mentioned in the title.
I'm trying to run a file to print hello world in the widget. I'm getting what I want when I run it in my system, but when I'm running it in colab its not working.
Code:
import tkinter
root = tk()
myLabel = Label(root, text="Hello World!")
myLabel.pack()
root.mainloop()
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-db74150bd164> in <module>()
1 import tkinter
2
----> 3 root = tk()
4
5 myLabel = Label(root, text="Hello World!")
TypeError: 'module' object is not callable
I tried changing tk into various forms(Tk, tK, Tkinter, tKinter), but it isn't working anyhow.
When you see Tk(), it is an instance of Tk() class present in __init__.py file in tkinter folder.
Since you have imported tkinter, you have to specify tkinter.Tk()to create a instance of Tk()
import tkinter
root = tkinter.tk()
myLabel = Label(root, text="Hello World!")
myLabel.pack()
root.mainloop()
In some programs, you can also see tk.Tk(). This is because the module tkinter is imported as tk:
import tkinter as tk
See the source code of tkinter
At line 2273 in __init__.py, you can see:
class Tk(Misc, Wm):
"""Toplevel widget of Tk which represents mostly the main window
of an application. It has an associated Tcl interpreter."""
_w = '.'
def __init__(self, screenName=None, baseName=None, className='Tk',
useTk=True, sync=False, use=None):
...
There are some errors:
import tkinter as tk
root = tk.Tk() # tk() you can't call a module, write tk.Tk() instead.
myLabel = tk.Label(root, text="Hello World!") # add tk.
myLabel.pack()
root.mainloop()
you can just import tkinter as
from tkinter import *
by using this it will work
I want to have abutton like this in tkinter (Modern window 10 buttons):
However, I get this button:
The code for my button is:
from tkinter import Tk,Button
root=Tk()
Button(root,text='OK').pack()
Another alternative to create button is to create a label and bind it to the action functions. In the below example .bind() is used to connect the label with respective function. You can design according to your requirements.
from tkinter import *
def OnPressed(event):
print('Hello')
def OnHover(event):
But.config(bg='red', fg='white')
def OnLeave(event):
But.config(bg='white', fg='black')
root = Tk()
But = Label(root, text='Hi', bg='white', relief='groove')
But.place(x=10, y=10, width=100)
But.bind('<Button-1>', OnPressed)
But.bind('<Enter>', OnHover)
But.bind('<Leave>', OnLeave)
root.mainloop()
The themed widgets are in 'themed Tk' aka ttk.
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
ok = ttk.Button(root, text='OK')
ok.pack()
root.mainloop()
Avoid from tkinter import * as both tkinter and tkinter.ttk define Button and many other widgets.
If you use this on Windows you should get something looking like a native button. But this is a theme and can be changed. On Linux or MacOS you will get a button style that is appropriate to that platform.
vol_up = Button(root, text="+",activebackground='orange',bg='yellow')
vol_up.pack(side='top')
I have recently being trying to make pokedex using tkinter python and I was going well with it but a problem occurred, which made the whole interface ugly!
There is a dotted border which is coming in every active button and notebook tab which is spoiling look of the whole program.
Also the code of styling didn't worked out !
NOTE: I tried using focuscolor but it didn't help...
import tkinter as tk
from tkinter.ttk import *
root = tk.Tk()
root.geometry('700x500')
Style().configure('lefttab.TNotebook', tabposition='wn')
Style().configure('TNotebook.Tab', font='Courier 16')
notebook = Notebook(root, style='lefttab.TNotebook')
f1 = tk.Frame(notebook, width=500, height=400)
f2 = tk.Frame(notebook, width=500, height=400)
notebook.add(f1, text='Kantodex Vx')
notebook.add(f2, text=' Attacks ')
notebook.pack(anchor=tk.NW)
root.mainloop()
I am trying to use tkinter with ibPy. I am using Spyder (Spyder 2.3.0). When I enter the sample program
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
I receive the message:
File "/Users/Ameade/Tkinter.py", line 8, in <module>
from tkinter import *
ImportError: No module named tkinter
Do you know where I can get this module? I am working on a Mac (OSX 10.9.4).
It seems you named your sample program file Tkinter.py. You should change this name to something else and it should work.
EDIT
As Kevin said, give to the file any other name but not the name of a python module (the extension must remain .py), so you could name it my_amazing_program.py. And keep the content of the file the same as you originally posted it - if you're using python 3+:
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
If you're using python 2+ change tkinter to Tkinter on the import line:
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
You may need to check the version of python you are using.
import tkinter works on python 3(instead of Tkinter)