#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9

    Python3 & python-tk


    I just downloaded/installed python-tk but I noticed that the python3 interpreter can not see or use TK, any ideas?

    One more thing. My python book says that strings or not mutable. If that's the case then how did I get away with writting the following code? sentence is a string and it works fine.


    Code:
    if(int(number) == 0):
                sentence = 'zero'
            elif(D1 == 0):
                if(D2 == 0 and D3 == 0):
                    sentence = sentence
                elif(D2 == 0 and D3 != 0):
                    sentence = sentence + resources.ones[D3]
                    sentence = sentence + resources.large[groups]
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    479
    Rep Power
    33
    The fact that you had to assign a new block of memory (also named "sentence") to the result says that you could not change the original but combined it with another string to create a third, note the id's (memory addres in CPython) change for strings but not for lists (which are mutable).
    Code:
    sentence = "abc"
    print id(sentence)
    sentence += "def"
    print id(sentence)
    #
    #  lists are mutable
    sentence = [0, 1, 2]
    print id(sentence), sentence
    sentence[1] = "x"
    print id(sentence), sentence
    sentence.append("z")     ## no equal sign necessary
    print id(sentence), sentence
    #---------------------------------------------------------------------
    # and so changing a string in a function does not change
    # the original because it occupies a separate block of memory
    def change_it(sentence):
        sentence += "def"
        print "change_it", sentence, id(sentence)
    
    sentence = "abc"
    change_it(sentence)
    print "after change", sentence, id(sentence)
    print
    sentence = [0, 1, 2]
    change_it(sentence)
    print "after change", sentence, id(sentence)
    Also this line of code does nothing
    Code:
     if(D2 == 0 and D3 == 0):
                    sentence = sentence
    Python comes with Tkinter so no installation is necessary, however you have to have TCL and Tk installed because Tkinter is a wrapper around Tk. We don't have any idea what "I just downloaded/installed python-tk but I noticed that the python3 interpreter can not see or use TK" means. How did you install it? What operating system are you on? How did you determine that that Python can not find it?
    Last edited by dwblas; August 6th, 2013 at 05:36 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9

    python-tk


    Originally Posted by dwblas
    The fact that you had to assign a new block of memory (also named "sentence") to the result says that you could not change the original but combined it with another string to create a third, note the id's (memory addres in CPython) change for strings but not for lists (which are mutable).
    Code:
    sentence = "abc"
    print id(sentence)
    sentence += "def"
    print id(sentence)
    #
    #  lists are mutable
    sentence = [0, 1, 2]
    print id(sentence), sentence
    sentence[1] = "x"
    print id(sentence), sentence
    sentence.append("z")     ## no equal sign necessary
    print id(sentence), sentence
    #---------------------------------------------------------------------
    # and so changing a string in a function does not change
    # the original because it occupies a separate block of memory
    def change_it(sentence):
        sentence += "def"
        print "change_it", sentence, id(sentence)
    
    sentence = "abc"
    change_it(sentence)
    print "after change", sentence, id(sentence)
    print
    sentence = [0, 1, 2]
    change_it(sentence)
    print "after change", sentence, id(sentence)
    Also this line of code does nothing
    Code:
     if(D2 == 0 and D3 == 0):
                    sentence = sentence
    Python comes with Tkinter so no installation is necessary, however you have to have TCL and Tk installed because Tkinter is a wrapper around Tk. We don't have any idea what "I just downloaded/installed python-tk but I noticed that the python3 interpreter can not see or use TK" means. How did you install it? What operating system are you on? How did you determine that that Python can not find it?
    Linux OS
    sudo apt-get install python-tk

    I did that because when I ran the program with python2.7 is said that I needed python-tk so I used apt-get to get it and then it worked fine, well there is no output but it doesn't give me an error. But when I run the code using the python3 interpreter is says "ImportError: No module named 'Tkinter'"


    Code:
    from Tkinter import *
    def fileopenbox(msg=None, title=None, default=None):
        root = Tk()
        root.withdraw()
        f = tkFileDialog.askopenfilename(parent=root,title=title, initialfile=default)
        if not f: return None
        return os.path.normpath(f)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9
    This line does nothing? Why is that. The program I wrote this in is working just fine so how is it doing nothing?

    Code:
     if(D2 == 0 and D3 == 0):
                    sentence = sentence


    Code:
    #!/usr/bin/python3
    
    import sys
    import resources
    
    if(str.isdigit(sys.argv[1]) == True):
        number = sys.argv[1]
        string = str(number)
        length = len(string)
        
        while(length % 3 != 0 or length < 3):
            string = '0' + string
            length = length + 1
            
        groups = length // 3
        
        position = 0
        sentence = ""
        
        while(groups > 0):
            D1 = int(string[position])
            D2 = int(string[position + 1])
            D3 = int(string[position + 2])
            
            if(int(number) == 0):
                sentence = 'zero'
            elif(D1 == 0):
                if(D2 == 0 and D3 == 0):
                    sentence = sentence
                elif(D2 == 0 and D3 != 0):
                    sentence = sentence + resources.ones[D3]
                    sentence = sentence + resources.large[groups]
                elif(D2 != 0 and D3 == 0):
                    sentence = sentence + resources.tens[D2]
                    sentence = sentence + resources.large[groups]
                elif(D2 == 1 and D3 != 0):
                    sentence = sentence + resources.teens[D3]
                    sentence = sentence + resources.large[groups]
                else:
                    sentence = sentence + resources.tens[D2]
                    sentence = sentence + resources.ones[D3]
                    sentence = sentence + resources.large[groups]
            elif(D2 == 1 and D3 != 0):
                sentence = sentence + resources.ones[D1] + 'hundred '
                sentence = sentence + resources.teens[D3]
                sentence = sentence + resources.large[groups]
            else:
                sentence = sentence + resources.ones[D1] + 'hundred '
                sentence = sentence + resources.tens[D2]
                sentence = sentence + resources.ones[D3]
                sentence = sentence + resources.large[groups]
    
            position = position + 3
            groups = groups - 1
        
    else:
        print("Input was not a number ", "formate as -> ", sys.argv[0], " 123")
        
    print(sentence)
    #END#

    resources.py
    Code:
    # Tuple for ones place
    ones=("", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine ")
    
    # Tuple for teens place
    teens = ("", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen")
    
    # Tuple for tens place
    tens = ("", "ten ", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety ")
    
    # Tuple for large number, thousand, million, ...
    large = ("", "", "thousand ", "million ", "billion ")
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    The module is called "tkinter" with no initial capital in Python 3.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9

    python3 and tkinter vs Tkinter


    Originally Posted by Nyktos
    The module is called "tkinter" with no initial capital in Python 3.
    ImportError: No module named '_tkinter'

    That was with python3
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    The python3 and python2 versions of libraries are packaged separately on most Linux distributions (Ubuntu, Debian, and Arch, anyway). You probably need to install a python3-tk package to make tkinter available on python3.

    This will be true for many libraries, so it's good to keep in mind.

IMN logo majestic logo threadwatch logo seochat tools logo