Skip to content

Instantly share code, notes, and snippets.

@danilobellini
Created September 29, 2012 14:49
Show Gist options
  • Save danilobellini/3804242 to your computer and use it in GitHub Desktop.
Save danilobellini/3804242 to your computer and use it in GitHub Desktop.
Python GUI toolkits simple example
#!/usr/bin/env python
import sys
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication(sys.argv)
# Main window, sizers and widgets
win = QtGui.QMainWindow()
wd = QtGui.QWidget()
vb = QtGui.QVBoxLayout()
lb = QtGui.QLabel("This is a test")
bt = QtGui.QPushButton("Yet another exit button")
# Button event handler (callback) and window title
win.setWindowTitle("PyQt4 test")
bt.clicked.connect(app.quit)
# Layout (2:5 sizing)
vb.setSpacing(0)
vb.setMargin(0)
vb.addWidget(lb, stretch=2, alignment=QtCore.Qt.AlignCenter)
vb.addWidget(bt, stretch=5)
bt.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
wd.setLayout(vb)
win.setCentralWidget(wd)
# Finished
win.show()
sys.exit(app.exec_())
#!/usr/bin/env python
import Tkinter
app = Tkinter.Tk()
# Widgets
lb = Tkinter.Label(app, text="This is a test")
bt = Tkinter.Button(app, text="Yet another exit button", command=app.quit)
# Title is given to the Tk() object
app.title("Tkinter test")
# Layout (2:5 sizing)
lb.grid()
bt.grid(sticky="nsew")
app.columnconfigure(0, weight=1)
app.rowconfigure(0, weight=2)
app.rowconfigure(1, weight=5)
# Finished
app.mainloop()
#!/usr/bin/env python
import wx
app = wx.App(False)
# Main window, sizers and widgets
win = wx.Frame(None, title="wxPython test")
vb = wx.BoxSizer(wx.VERTICAL)
hb = wx.BoxSizer(wx.HORIZONTAL) # StaticText stays on top if expanded
lb = wx.StaticText(win, wx.ID_ANY, "This is a test")
bt = wx.Button(win, wx.ID_ANY, "Yet another exit button")
# Button event handler (callback)
def on_button(evt):
app.Exit()
win.Bind(wx.EVT_BUTTON, on_button, bt)
# Layout (2:5 sizing)
win.SetSizer(vb)
hb.Add(lb, 0, wx.ALIGN_CENTER_VERTICAL)
vb.Add(hb, 2, wx.ALIGN_CENTER_HORIZONTAL)
vb.Add(bt, 5, wx.EXPAND)
# Finished
win.Show()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment