4

I have recently been using WxPython to create a GUI Network simulator like Cisco packet tracer but if I am honest it has been extremely difficult trying to find examples of what I need etc etc. Iv resorted back to the old faithful Tk.

My program thus far has a menu bar which consists of a File > Exit. It also has an Exit button at the bottom Right hand side of the application. As well as this it has a canvas of a set size and a variety of buttons which when clicked produce a small image of the hardware on the canvas. This was done using PIL

What I need next is to be able to drag these images around the canvas and this is proving a little difficult. I have looked at the following example of how it has been broken down and I kind of understand how you need an on click definition, motion (going from a to b) and on release definition, but how do I apply it to my code that I already have?

Here is the link to what I referenced above: http://www.python-forum.org/pythonforum/viewtopic.php?f=4&p=75789

Finally Here is the code I have already. I can appreciate that the layout and structure of my code isn't great as I am fairly new to programming but any guidance / examples / visual representations would be amazing.

  from Tkinter import*
from PIL import Image, ImageTk
class AllTkinterWidgets:
    def __init__(self, master):
        frame= Frame(master, width=900, height=600)
        frame.pack()

        iframe5 = Frame (frame, bd=2, relief=RAISED)
        iframe5.pack(expand=1, fill=X, pady=10, padx=5)

        c = Canvas(iframe5, bg='white', width=600, height=500)
        c.pack()

    # definitions to print hardware images to the canvas
    # -----------------------------------------------------------------------
        def show_imageRouter():
            c.create_image(30,30, image=image1)

        def show_imageSwitch():
            c.create_image(30,60, image=image2)

        def show_imageServer():
            c.create_image(30,100, image=image3)

        def show_imageIpPhone():
            c.create_image(30,140, image=image4)

        def show_imageWirelessRouter():
            c.create_image(30,180, image=image5)

        def show_imageHost():
            c.create_image(30, 220, image=image6)

    # Network hardware buttons created
    # ----------------------------------------------------
        self.button = Button(frame, text = "Router", height= 1, width= 8, padx=2, pady=2,command=show_imageRouter)
        self.button.pack(side = LEFT)

        self.button = Button(frame, text = "Switch",height= 1, width= 8, padx=2, pady=2, command=show_imageSwitch)
        self.button.pack(side = LEFT)

        self.button = Button(frame, text = "Server",height= 1, width= 8, padx=2, pady=2, command=show_imageServer)
        self.button.pack(side = LEFT)

        self.button = Button(frame, text = "IP Phone",height= 1, width= 8, padx=2, pady=2, command=show_imageIpPhone)
        self.button.pack(side = LEFT)

        self.button = Button(frame, text = "Wireless Router",height= 1, width= 12, padx=2, pady=2, command=show_imageWirelessRouter)
        self.button.pack(side = LEFT)

        self.button = Button(frame, text = "Host",height= 1, width= 8, padx=2, pady=2, command=show_imageHost)
        self.button.pack(side = LEFT)

        self.button = Button(frame, text = "Cabling",height= 1, width= 8, padx=2, pady=2)
        self.button.pack(side = LEFT)

        self.button = Button(frame, text = "Square",height= 1, width= 8, padx=2, pady=2)
        self.button.pack(side = LEFT)

    # Create the image objects for the hardware Images
    # ----------------------------------------------------------------------
        imageFile = "router.png"
        image1 = ImageTk.PhotoImage(Image.open(imageFile))

        imageFile = "switch.png"
        image2 = ImageTk.PhotoImage(Image.open(imageFile))

        imageFile = "Server.png"
        image3 = ImageTk.PhotoImage(Image.open(imageFile))

        imageFile = "ipPhone.png"
        image4 = ImageTk.PhotoImage(Image.open(imageFile))

        imageFile = "WirelessRouter.png"
        image5 = ImageTk.PhotoImage(Image.open(imageFile))

        imageFile = "Host.png"
        image6 = ImageTk.PhotoImage(Image.open(imageFile))

root = Tk()
all = AllTkinterWidgets(root)

def Exit():
    print "Exit"

# Create an Exit Button
toolbar = Frame(root)
b = Button(toolbar, text="Exit", width=6, height=3, command=Exit)
b.pack(side=RIGHT, padx=2, pady=2)
toolbar.pack(side=BOTTOM, fill=X)

# Press Esc to quit
root.bind("<Escape>", exit)

# Creation of a menu File > Exit
menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Exit", command=Exit)

root.mainloop()

Sorry if the indentation is a bit strange. I've adjusted it to make it block together here.

martineau
  • 119,623
  • 25
  • 170
  • 301
karl davies
  • 89
  • 1
  • 1
  • 7

1 Answers1

3

This answer to the question "board drawing code to move an oval" shows how to drag an object on a canvas.

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685