Hey guys, well how would you go about making a GUI, i've seen examples with Tkinter. But how would you place the button exactly where you want them. Also would you set an event for a button or field for example if the button is clicked it connects to a socket. Thanks, Clipper34.

Recommended Answers

All 8 Replies

Hi Clipper34. I'm not 100% sure what you're asking specifically, but I use wxPython. I prefer it to Tkinter - just a personal choice. You can go to http://wxpython.org/ to download wxPython and begin using it.

As for positioning a button absolutely, just set it's position during it's creation. Example:

self.my_button = wx.Button( self, id=-1, label="Click me!", pos=(25, 15) )

Or you could use:

self.my_button.SetPosition( (25, 15) )

for after it's creation.


That would set the button at the specified (x, y) coordinates of (25, 15). But you should browse through the wxPython documentation for many, many good examples to get you started with wxPython.

Here's an example app that positions a button at (25, 15) and handles the button click event:

import wx

class MyPanel( wx.Panel ):
    def __init__( self, id, parent ):
        wx.Panel.__init__( self, id, parent, size=(200, 200) )

        self.my_button = wx.Button( self, id=-1, label="Click me!", pos=(25, 15) )
        self.my_button.Bind( wx.EVT_BUTTON, self.clickButton )

    def clickButton( self, event ):
        print "Clicked!"

my_app = wx.App()
my_frame = wx.Frame( parent=None, id=-1, size=(200, 200) )
MyPanel( my_frame, -1 )
my_frame.Show( True )
my_app.MainLoop()

Good luck with your coding!

Here is simple example using Tkinter GUI toolkit that is usually included with Python:

# using geometry manager place() for absolute layouts

import Tkinter as tk

def connect():
    "make your connection"
    # your connection code here
    # ...
    root.title("Connected!")  # test

# the root window
root = tk.Tk()

# command --> does function connect when mouse is clicked
button = tk.Button(root, text='Connect', command=connect)
# position the button at points x, y
# (x=0, y=0 is upper left corner of root area)
button.place(x=90, y=10, width=100, height=20)

# the event loop checks on mouse action
root.mainloop()

I'm using wxPython and I love it!
If you want to see Go to www.wxpython.org and if you will love it you might consider to go here at www.zetcode.com

I hope that will be a good start!
Steve

Thanks for the replys guys, this may be in a FAQ somewheres but, whenver i compile and run something i dont have code highlighting do you guys know why? Thanks, Clipper34.

What program are you writing your code in? If you're using something like Notepad, that has no syntax highlighting. I personally use IDLE (which came with the download of python), but there are many other code editors out there.

Thanks Steve, now i got another question. i understand how to bind an event to a button via Tkinter. But how about binding an invent that takes input through a form in the GUI its self or by popup. Thanks, Clipper34.

TkInter? I don't know. Infact the first time I was looking for GU I failed to cope with TkInter and I decided to go for Wxpy and fell in love with it!
If you need that I can do for you but for TkInter, sorry I don't know!!
Steve

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.