Python('module' object is not callable )Tkinter - python

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

Related

how do I fix AttributeError: 'Tk' object has no attribute 'open'

I'm trying to make a button that opens another python file
import os
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('juststop')
os.startfile(r'C:\Users\75259\PycharmProjects\pythonProject9\main.py')
open_button = ttk.Button(
root,
text='calculater',
)
open_button.pack(
ipadx=5,
ipady=5,
expand=True
)
root.mainloop()
I have tried a multitude of things but non seem to work
The way you have your os.startfile implemented calls the function right away. One way to solve this is to create a function to use it when called and link it to your button. Below is a modification of your example that will only open the file when you press the calculater button.
import os
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('juststop')
def open_file():
os.startfile(r'C:\Users\75259\PycharmProjects\pythonProject9\main.py')
open_button = ttk.Button(root, text='calculater', command=open_file)
open_button.pack(ipadx=5, ipady=5, expand=True)
root.mainloop()

Tkinter independent windows

I have a gui that I wrote with tkinter and I want to call it again with a button how do I do this (They will be independent of each other)
I want to do like this:
import tkinter
root = Tk()
root.title("test")
def testt()
root()
Button(root, text='window +', command=testt).pack()
root.mainloop()
You also have to import some modules as shown.
Try this:
from tkinter import Tk, Button, Toplevel
def testt():
root1 = Toplevel()
root1.mainloop()
Just add TopLevel in test() function.
Code:
import tkinter as tk
root = tk.Tk()
root.title("test")
def testt():
root1 = tk.Toplevel()
tk.Button(root, text='window +', command=testt).pack()
root.mainloop()
Screenshot output:

I have a weird problem with tkinter when using python

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

using tkinter module with python 3.6

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

Using tkinter with ibPy

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)

Categories