#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12

    Tkinter index()? and Photo


    Okay hello Here i am back with a<nother question to teh comunity of python

    How do I locate the mouse pointer on a cavas or anywhere on the screen with Tkinter?

    How do I import photos from the same directory and display them with TKinter...

    Please help

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    Here is one answer to your first question:
    Code:
    from Tkinter import *
    
    def check_root_pos(event):
        l2.configure(text="Main Window: x= %i  y= %i" % (
            root.winfo_pointerx(), root.winfo_pointery()))
    
    def check_canvas_pos(event):
        l1.configure(text="Canvas: x= %i  y= %i" % (event.x, event.y))
    
    root = Tk()
    root.bind('<1>', check_root_pos)
    c = Canvas()
    c.pack()
    c.bind('<Motion>', check_canvas_pos)
    l1 = Label()
    l1.pack()
    l2 = Label()
    l2.pack()
    root.mainloop()
    The coordinates of the mousepointer in the canvas are shown if you move the mouse around in the canvas. To see the coordinates of the mp in the mainwindow, just click with the left mouse-button.

    Regards, mawe
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Thanks
    BUt what about a photo as background
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    First you create a PhotoImage.
    If you want a real background image, create a Label with this image and pack all other widgets into it:
    Code:
    root = Tk()
    pi = PhotoImage(file='file_name')
    
    background = Label(root, image=pi)
    background.pack()
    
    entry = Entry(background)
    entry.pack(padx=10,pady=10)
    
    root.mainloop()
    If you want the image just on the Canvas, use the create_image() method. Here is some info about it.
    I'm not sure, but with PhotoImage you can only use gif and ppm files. For all others you must use PIL:
    Code:
    from Tkinter import *
    from PIL import Image, ImageTk
    
    root = Tk()
    image = Image.open("homer.bmp")
    photo = ImageTk.PhotoImage(image)
    label = Label(image=photo)
    label.pack()
    root.mainloop()
    Regards, mawe
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Thanks
    Okay I dont get this... My code:

    Code:
    from random import choice as Choice
    from Tkinter import *
    
    class Gameplay:
        total_value = 0
        card_count = 0
        
        def __init__(self):                                     # Init deck and counting
            deck = {"king":10, "queen":10, "jack":10,
                    "ace":0, "ten":10, "nine":9,
                    "eight":8, "seven":7, "six":6
                    }
    
        def User_Keep(self):                                    # Stay
            top = Toplevel(root)
            top.minsize(200,200)
            Label(top,text = "Here you go...").pack(side = TOP)
            Label(top,text = "Total Value").pack()
            Label(top,textvariable = tot_value).pack()
            Label(top,text = "Total Number").pack()
            Label(top,textvariable = cards_count).pack()
            Button(top, text = "Exit",command = root.destroy).pack()
    
        def User_Call(self):                                    # Hit
            card = Choice(deck)
            card_count += 1
            if card == "ace":
                if total_value >= 11:
                    card = 1
                else:
                    card = 11
            else:
                pass
            total_value += card
            if total_value > 21:
                User_Keep()
    
    if __name__ == "__main__":
        game = Gameplay()                                    # Instance to access Gameplay()
    
        value = IntVar
        value.get(game.total_value)
        cards = IntVar
        cards.get(game.cards_count)
        
        root = Tk()
        root.minsize(300,300)
        root.title("Black Jack")
        root.maxsize(1000,900)
    
        pi = PhotoImage(file='Fog.gif')
        Label(root, image=pi).pack()
    
    
        Label(root, text = "Total Value").pack(side=TOP)
        Label(root, textvariable = tot_value).pack(side=TOP)
    
        Label(root, text = "Total Cards").pack(side=TOP)
        Label(root, textvariable = self.cards_count).pack(side=TOP)
    
        Button(root, text = "Hit", command = self.User_Call).pack(side=BOTTOM)
        Button(root, text = "Stay", command = self.User_Keep).pack(side=BOTTOM)
    
        root.mainloop()
    My question what is this error...

    Code:
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "E:\Python\lib\lib-tk\Tkinter.py", line 1345, in __call__
        return self.func(*args)
      File "H:\Programming\Python Scripts\Games\Black Jack.py", line 35, in User_Call
        card = Choice(self.deck)
      File "E:\Python\lib\random.py", line 250, in choice
        return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
    KeyError: 1

    Thanks for any help
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    I changed some things
    Code:
    from random import choice as Choice
    from Tkinter import *
    
    class Gameplay:
        total_value = 0
        card_count = 0
        
        def __init__(self):                                     # Init deck and counting
            self.deck = [["king",10], ["queen",10], ["jack",10],
                    ["ace",0], ["ten",10], ["nine",9],
                    ["eight",8], ["seven",7], ["six",6]
                    ]
    
        def User_Keep(self):                                    # Stay
            top = Toplevel(root)
            top.minsize(200,200)
            Label(top,text = "Here you go...").pack(side = TOP)
            Label(top,text = "Total Value").pack()
            Label(top,text = self.total_value).pack()
            Label(top,text = "Total Number").pack()
            Label(top,text = self.card_count).pack()
            Button(top, text = "Exit",command = root.destroy).pack()
    
        def User_Call(self):                                    # Hit
            card,card_value = Choice(self.deck)
            self.card_count += 1
            if card == "ace":
                if self.total_value >= 11:
                    card_value = 1
                else:
                    card_value = 11
            else:
                pass
            self.total_value += card_value
            value.set(self.total_value)
            cards.set(self.card_count)
            if self.total_value > 21:
                self.User_Keep()
    
    if __name__ == "__main__":
        game = Gameplay()                                    # Instance to access Gameplay()
            
        root = Tk()
        root.minsize(300,300)
        root.title("Black Jack")
        root.maxsize(1000,900)
        
        value = IntVar()
        value.set(game.total_value)
        cards = IntVar()
        cards.set(game.card_count)
    
        pi = PhotoImage(file='Fog.gif')
        Label(root, image=pi).pack()
    
        Label(root, text = "Total Value").pack(side=TOP)
        Label(root, textvariable = value).pack(side=TOP)
    
        Label(root, text = "Total Cards").pack(side=TOP)
        Label(root, textvariable = cards).pack(side=TOP)
    
        Button(root, text = "Hit", command = game.User_Call).pack(side=BOTTOM)
        Button(root, text = "Stay", command = game.User_Keep).pack(side=BOTTOM)
    
        root.mainloop()
    Regards, mawe
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Okay i managed to make it work but now i have this:

    Code:
        def User_Call(self):                                    # Hit
            card = Choice(self.deck)
            self.deck.remove(card)
            if card == "ace_s" or "ace_d" or "ace_p" or "ace_h":
                if total_value >= 11:
                    card = 1
                else:
                    card = 11
            else:
                pass
            total_value += card
            card_count += 1
            if total_value > 21:
                User_Keep()
    Code:
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "E:\Python\lib\lib-tk\Tkinter.py", line 1345, in __call__
        return self.func(*args)
      File "H:\Programming\Python Scripts\Games\Black Jack.py", line 38, in User_Call
        if total_value >= 11:
    UnboundLocalError: local variable 'total_value' referenced before assignment
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    Did you have a look at the code I posted? I guess not
    Code:
            if card == "ace":
                if self.total_value >= 11:
                    card_value = 1
                else:
                    card_value = 11
            else:
                pass
            self.total_value += card_value
            value.set(self.total_value)
    Regards, mawe
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    I did alittle
    :P
    That code does not work for anything else except Aces no kings...
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hm, I don't know what you mean, it works fine here.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Okay
    Code:
    from random import choice as Choice
    from Tkinter import *
    
    class Gameplay:
        total_value = 0
        card_count = 0
        
        def __init__(self):                                     # Init deck and counting
            self.deck = [
                        [10], [10], [10],
                        [0], [10], [9],
                        [8], [7], [6],
                        [10], [10], [10],
                        [0], [10], [9],
                        [8], [7], [6],
                        [10], [10], [10],
                        [0], [10], [9],
                        [8], [7], [6],
                        [10], [10], [10],
                        [0], [10], [9],
                        [8], [7], [6]
                        ]
    
        def User_Keep(self):                                    # Stay
            top = Toplevel(root)
            top.minsize(200,200)
            Label(top,text = "Here you go...").pack(side = TOP)
            Label(top,text = "Total Value").pack()
            Label(top,textvariable = value).pack()
            Label(top,text = "Total Number").pack()
            Label(top,textvariable = cards).pack()
            Button(top, text = "Exit",command = root.destroy).pack()
    
        def User_Call(self):                                    # Hit
            card = Choice(self.deck)
            self.deck.pop(card)
            if card == 0:
                if self.total_value >= 11:
                    card_value = 1
                else:
                    card_value = 11
            else:
                pass
            self.total_value += card_value
            value.set(self.total_value)
                
    
    if __name__ == "__main__":
        root = Tk()
        
        game = Gameplay()                                 # Instance to access Gameplay()
    
        value = IntVar()
        value.set(game.total_value)
        cards = IntVar()
        cards.set(game.card_count)
        
        root.minsize(300,300)
        root.title("Black Jack")
        root.maxsize(1000,900)
        
        Label(root, text = "Total Value").pack(side=TOP)
        Label(root, textvariable = value).pack(side=TOP)
    
        Label(root, text = "Total Cards").pack(side=TOP)
        Label(root, textvariable = cards).pack(side=TOP)
    
        Button(root, text = "Hit", command = game.User_Call).pack(side=BOTTOM)
        Button(root, text = "Stay", command = game.User_Keep).pack(side=BOTTOM)
    
        root.mainloop()
    Code:
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "E:\Python\lib\lib-tk\Tkinter.py", line 1345, in __call__
        return self.func(*args)
      File "H:\Programming\Python Scripts\Games\Black Jack.py", line 36, in User_Call
        self.deck.pop(card)
    TypeError: an integer is required
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Code:
        def __init__(self):                                     # Init deck and counting
            self.deck = [
                        10, 10, 10,
                        0, 10, 9,
                        8, 7, 6,
                        10, 10, 10,
                        0, 10, 9,
                        8, 7, 6,
                        10, 10, 10,
                        0, 10, 9,
                        8, 7, 6,
                        10, 10, 10,
                        0, 10, 9,
                        8, 7, 6
                        ]
    ...
        def User_Call(self):                                    # Hit
            card = Choice(self.deck)
            self.deck.remove(card)
            if card == 0:
                if self.total_value >= 11:
                    card = 1
                else:
                    card = 11
            else:
                pass
            self.total_value += card
            value.set(self.total_value)
    Okay?
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Hello

    Thanks for the help it nearly works teh way i want it to work

    Code:
    def User_Call(self):                                    # Hit
            card = Choice(self.deck)
            self.deck.remove(card)
            if card == 0:
                if self.total_value >= 11:
                    card = 1
                else:
                    card = 11
            else:
                pass
            if self.total_value >= 21:
                self.User_Call()
            else:
                pass
            self.total_value += card
            self.card_count += 1
            value.set(self.total_value)
            cards.set(self.card_count)
    Thats the function now which works perfectly except for the
    Code:
    if self.total_value >= 21:
                self.User_Call()
    part.

    It gives me this error after a while...

    Code:
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "E:\Python\lib\lib-tk\Tkinter.py", line 1345, in __call__
        return self.func(*args)
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "E:\Python\lib\lib-tk\Tkinter.py", line 1345, in __call__
        return self.func(*args)
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 35, in User_Call
        card = Choice(self.deck)
      File "E:\Python\lib\random.py", line 250, in choice
        return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
    IndexError: list index out of range
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "E:\Python\lib\lib-tk\Tkinter.py", line 1345, in __call__
        return self.func(*args)
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 35, in User_Call
        card = Choice(self.deck)
      File "E:\Python\lib\random.py", line 250, in choice
        return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
    IndexError: list index out of range
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 45, in User_Call
        self.User_Call()
      File "H:\Programming\Python Scripts\Games\Black Jack.pyw", line 35, in User_Call
        card = Choice(self.deck)
      File "E:\Python\lib\random.py", line 250, in choice
        return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
    IndexError: list index out of range
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Sorry I got it...
    Thanks for the help

IMN logo majestic logo threadwatch logo seochat tools logo