Python GUI Programming

Reply

Join Date: Oct 2004
Posts: 3,941
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Python GUI Programming

 
-2
  #1
May 8th, 2009
Contributions should have an emphasis on comparing the various GUI toolkits, showing example code and solutions to problems.

The example code should be usable as templates, so folks get a feel for the toolkit and can use those examples/templates as a basis for more complex programs.

Again, don't clutter up the sticky with questions. Ask question in the regular forum.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,603
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Python GUI Programming

 
1
  #2
May 8th, 2009
Here's an example of a basic window frame in pyQT.

You can get pyQT from here: http://www.riverbankcomputing.co.uk/.../pyqt/download

  1. # Code Example: Display a window in PyQt4
  2. # Python 2.6 with PyQt 4
  3.  
  4. import sys
  5. from PyQt4 import QtGui
  6.  
  7. class MainFrame(QtGui.QMainWindow):
  8.  
  9. def __init__(self):
  10.  
  11. QtGui.QMainWindow.__init__(self)
  12.  
  13. self.setWindowTitle("Window title") # title
  14. self.resize(1024, 768) # size
  15. self.setMinimumSize(800, 600) # minimum size
  16. self.move(0, 0) # position window frame at top left
  17.  
  18.  
  19. if __name__ == "__main__":
  20. app = QtGui.QApplication(sys.argv)
  21.  
  22. frame = MainFrame()
  23. frame.show()
  24.  
  25. exit_code = app.exec_()
  26. sys.exit(exit_code)
Last edited by scru; May 8th, 2009 at 3:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,603
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Python GUI Programming

 
0
  #3
May 8th, 2009
This next pyQT example builds on the last and incorporates a simple box layout used with a button and a canvas. It also shows how to hook up a button event.

  1. # Code Example: Display a window with a button, icon and canvas in PyQt4
  2. # Python 2.6 with PyQt 4
  3.  
  4. # This example build on the last one by adding a button, changing the
  5. # window icon, and adding a canvas where we can draw some shapes.
  6.  
  7. import sys
  8. from PyQt4 import QtGui, QtCore
  9.  
  10. class MainFrame(QtGui.QMainWindow):
  11.  
  12. def __init__(self):
  13.  
  14. QtGui.QMainWindow.__init__(self)
  15.  
  16. self.setWindowTitle("Window title") # title
  17. self.resize(1024, 768) # size
  18. self.setMinimumSize(800, 600) # minimum size
  19. self.move(0, 0) # position window frame at top left
  20.  
  21. # Adding the icon:
  22. self.setWindowIcon(QtGui.QIcon("myicon.png"))
  23.  
  24. # Set the central widget for the main window
  25. cwidget = QtGui.QWidget(self)
  26.  
  27. # Set up a layout for the button and canvas:
  28. layout = QtGui.QVBoxLayout() #vertical box layout
  29. #layout = QtGui.QHBoxLayout() #horizontal box layout
  30.  
  31. self.add_button(layout)
  32. self.add_canvas(layout)
  33. cwidget.setLayout(layout)
  34. self.setCentralWidget(cwidget)
  35.  
  36. # Now set up the shapes that we draw on each button click
  37. # get the button back from the layout
  38. button = layout.itemAt(0).widget()
  39.  
  40. # Create objects used for drawing
  41. sol_pen = QtGui.QPen(QtGui.QColor("black"))
  42.  
  43. dot_pen = QtGui.QPen(QtGui.QColor("black"))
  44. dot_pen.setStyle(QtCore.Qt.DotLine)
  45.  
  46. dash_pen = QtGui.QPen(QtGui.QColor("black"))
  47. dash_pen.setStyle(QtCore.Qt.DashLine)
  48.  
  49. r_brush = QtGui.QBrush(QtGui.QColor("red"))
  50. g_brush = QtGui.QBrush(QtGui.QColor("green"))
  51. b_brush = QtGui.QBrush(QtGui.QColor("blue"))
  52.  
  53. triangle = QtGui.QPolygonF()
  54. triangle.append(QtCore.QPointF(100, 50))
  55. triangle.append(QtCore.QPointF(200, 200))
  56. triangle.append(QtCore.QPointF(0, 200))
  57.  
  58.  
  59. self.scene_data = []
  60. # add data for drawing circle
  61. #self.scene.addEllipse(0, 0, 150, 150, sol_pen, r_brush) #x, y, w, h etc
  62. self.scene_data.append({'routine':self.scene.addEllipse,
  63. 'args':(0,0,150,150,sol_pen,r_brush),
  64. 'z': 0, #z-index
  65. 'next': "Draw Rectangle"})
  66.  
  67. # add data for drawing square
  68. #self.scene.addRect(100, 100, 150, 150, dot_pen, g_brush) #x, y, w, h etc
  69. self.scene_data.append({'routine':self.scene.addRect,
  70. 'args':(100,100,150,150, dot_pen, g_brush),
  71. 'z':1,
  72. 'next': "Draw Triangle."})
  73.  
  74. # add data for drawing triangle
  75. #self.scene.addPolygon(triangle, dash_pen, b_brush)
  76. self.scene_data.append({'routine':self.scene.addPolygon,
  77. 'z':2,
  78. 'args':(triangle, dash_pen, b_brush)})
  79.  
  80. # Set up self.draw_next_item to fire when button is clicked.
  81. self.connect(button, QtCore.SIGNAL("clicked()"), self.draw_next_item)
  82.  
  83.  
  84. def add_button(self, layout):
  85. """Create a button and then add it to the layout."""
  86. button = QtGui.QPushButton("Draw Circle")
  87. layout.addWidget(button)
  88.  
  89. def add_canvas(self, layout):
  90. """Create a canvas and then add it to the layout."""
  91. canvas = QtGui.QGraphicsView()
  92. layout.addWidget(canvas)
  93.  
  94. # Now create a graphics scene to draw shapes to.
  95. # Now remember to keep a reference to GraphicsScene for as long as
  96. # you're using it, because unlike widgets, these are automatically
  97. # destroyed when they lose reference count
  98. self.scene = QtGui.QGraphicsScene()
  99. canvas.setScene(self.scene)
  100.  
  101. def draw_next_item(self):
  102. # get the button. could've just saved as self.button, but I wanted to
  103. # show how it could be done this way
  104. button = self.centralWidget().layout().itemAt(0).widget()
  105.  
  106. d = self.scene_data.pop(0) # get first item
  107. item = d['routine'](*d['args']) # just a little python magic
  108. item.setZValue(d['z'])
  109.  
  110. if len(self.scene_data):
  111. # more stuff to draw, set button label
  112. button.setText(d['next'])
  113. else:
  114. # no more left, disable button
  115. button.setText("No more shapes!")
  116. button.setDisabled(True)
  117.  
  118.  
  119.  
  120. if __name__ == "__main__":
  121. app = QtGui.QApplication(sys.argv)
  122.  
  123. frame = MainFrame()
  124. frame.show()
  125.  
  126. exit_code = app.exec_()
  127. sys.exit(exit_code)
Last edited by scru; May 8th, 2009 at 4:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 20
Reputation: funfullson is an unknown quantity at this point 
Solved Threads: 1
funfullson funfullson is offline Offline
Newbie Poster

Re: Python GUI Programming question

 
0
  #4
May 10th, 2009
I want write a program with GUI but I don't know which one is better gtk- wx-qt and etc. please guide me.please compair them.
thank.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,275
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Python GUI Programming

 
3
  #5
May 10th, 2009
Here is a comparison of some common Python GUI toolkits. These were created on my Ubuntu/Linux machine, because I could never get PyGTK to go on my Windows Vista machine. I followed the installation routine carefully, but there was always a dll missing, or an entry point wasn't found, or a version missmatch, so I gave up. GTK and PyGTK are more natural on Linux.

Let's take a look at typical PyGTK code:
  1. #!/usr/bin/env python
  2.  
  3. # display an image using PyGTK + GTK
  4.  
  5. import pygtk
  6. pygtk.require('2.0')
  7. import gtk
  8.  
  9. class ShowImage(object):
  10.  
  11. def __init__(self):
  12. # create the main window, and
  13. window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  14. # attach destroy signal to terminate the application
  15. window.connect("destroy", lambda w: gtk.main_quit())
  16. window.set_border_width(10)
  17. window.show()
  18.  
  19. # a horizontal box to hold the buttons
  20. hbox = gtk.HBox()
  21. hbox.show()
  22. window.add(hbox)
  23.  
  24. # pick an image file you have in the working directory, or give
  25. # the full path, can be a .jpg, .png, ,gif, .bmp image file
  26. # (filenames are case sensitive on Ubuntu/Linux)
  27. image_file = "LAKE2.gif"
  28. image = gtk.Image()
  29. image.set_from_file(image_file)
  30. image.show()
  31. # create a button to contain the image widget
  32. # auto adjusts to image size
  33. button = gtk.Button()
  34. button.add(image)
  35. button.show()
  36. hbox.pack_start(button)
  37.  
  38.  
  39. ShowImage()
  40. gtk.main()
The same test program using PyQT, PyQT installs easily on Windows and Linux machines:
  1. #!/usr/bin/env python
  2.  
  3. # display an image using PyQT
  4.  
  5. import sys
  6. from PyQt4 import QtGui
  7.  
  8. class MyImage(QtGui.QWidget):
  9. def __init__(self, parent, width, height):
  10. QtGui.QWidget.__init__(self, parent)
  11.  
  12. # pick an image file you have in the working directory, or give
  13. # the full path, can be a .jpg, .png, ,gif, .bmp image file
  14. # (filenames are case sensitive on Ubuntu/Linux)
  15. image_file = "LAKE2.gif"
  16. image = QtGui.QPixmap(image_file)
  17. # show the image name in the title
  18. self.setWindowTitle(image_file)
  19.  
  20. # use a label to display the image in
  21. label = QtGui.QLabel(self)
  22. label.setGeometry(10, 10, width, height)
  23. label.setPixmap(image)
  24.  
  25.  
  26. app = QtGui.QApplication(sys.argv)
  27. # assume image is about 400x300 pixels in size
  28. width = 400
  29. height = 300
  30. w = MyImage(None, width, height)
  31. # setGeometry(x, y, width, height) ULC coordinates x.y
  32. w.setGeometry(100, 100, width+20, height+20)
  33. w.show()
  34. app.exec_()
Here is a simplified wxPython program (could have used a class):
  1. #!/usr/bin/env python
  2.  
  3. # display an image using wxPython
  4.  
  5. import wx
  6.  
  7. app = wx.App(0)
  8. frame = wx.Frame(None, wx.ID_ANY, "Show an image file")
  9.  
  10. # pick an image file you have in the working directory, or give
  11. # the full path, can be a .jpg, .png, ,gif, .bmp image file
  12. # (filenames are case sensitive on Ubuntu/Linux)
  13. image_file = 'LAKE2.gif'
  14. image = wx.Bitmap(image_file)
  15. image_width = image.GetWidth()
  16. image_height = image.GetHeight()
  17. # set frame size to image size
  18. frame.SetClientSize((image_width, image_height))
  19. # show the image as static bitmap
  20. wx.StaticBitmap(frame, wx.ID_ANY, image)
  21. # show the image name in the title
  22. frame.SetTitle(image_file)
  23.  
  24. frame.Show()
  25. app.MainLoop()
Last not least Tkinter, which usually ships with Python:
  1. #!/usr/bin/env python
  2.  
  3. # display an image using Tkinter
  4.  
  5. import Tkinter as tk
  6.  
  7. root = tk.Tk()
  8.  
  9. # pick an image file you have in your working directory
  10. # or use full path, Tkinter only reads .gif image files
  11. # (filenames are case sensitive on Ubuntu/Linux)
  12. image_file = "LAKE2.gif"
  13. photo = tk.PhotoImage(file=image_file)
  14. root.title(image_file)
  15.  
  16. # put the image on a typical widget
  17. label = tk.Label(root,image=photo)
  18. label.pack(padx=5, pady=5)
  19.  
  20. root.mainloop()
Editor's note:
The first line in snee's code
#!/usr/bin/env python
tells Linux where it can find the Python interpreter
it is simply ignored on Windows machines.
Last edited by vegaseat; May 11th, 2009 at 1:59 pm. Reason: Linux note
Attached Thumbnails
LAKE2.gif  
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,941
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Python GUI Programming

 
0
  #6
May 10th, 2009
Thanks for the sample code and the nice picture. I thought I should add pyglet, which is an audio-visual module using GL and FFmpeg available for Windows and Unix systems ...
  1. # show an image using pyglet
  2. # download pyglet from: http://www.pyglet.org/download.html
  3. # (the event handler is attached via a function decorator)
  4.  
  5. import pyglet
  6.  
  7. # pick an image file you have in the working directory, or give
  8. # the full path, can be a .jpg, .png, ,gif, .bmp image file
  9. # (I understand filenames are case sensitive on Linux)
  10. image_file = 'LAKE2.gif'
  11. img = pyglet.image.load(image_file)
  12.  
  13. # create and size the window to the picture size +
  14. # a small frame around it
  15. w = img.width + 10
  16. h = img.height + 10
  17. win = pyglet.window.Window(width=w, height=h)
  18.  
  19. # give the window a title
  20. win.set_caption(image_file)
  21.  
  22. @win.event
  23. def on_draw():
  24. win.clear()
  25. # draw image in window at coordinates x=5, y=5
  26. # note that coordinates start at lower left corner
  27. img.blit(5, 5)
  28.  
  29. pyglet.app.run()
Pyglet is not a full GUI toolkit, but has its strength in audio and video presentations.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,941
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Python GUI Programming

 
0
  #7
May 11th, 2009
PyQT was no problem on Windows XP. I downloaded and installed:
http://www.riverbankcomputing.com/st...pl-4.4.3-1.exe

The cumbersome split between QtCore and QtGui is made simpler if you use the import statements show below ...
  1. # a simple window using PyQT
  2. # with 2 buttons and a label
  3.  
  4. import sys
  5. # might be easier to use this import option
  6. from PyQt4.QtCore import *
  7. from PyQt4.QtGui import *
  8.  
  9. class MyForm(QWidget):
  10. def __init__(self):
  11. QWidget.__init__(self)
  12. # setGeometry(x_pos, y_pos, width, height)
  13. self.setGeometry(100, 150, 320, 100)
  14. self.setWindowTitle("A simple window")
  15.  
  16. # use a grid layout for the widgets
  17. grid = QGridLayout()
  18.  
  19. btn_hello = QPushButton("Hello")
  20. # bind the button click to a function reference
  21. self.connect(btn_hello, SIGNAL("clicked()"), self.on_click)
  22.  
  23. btn_quit = QPushButton("Quit")
  24. self.connect(btn_quit, SIGNAL("clicked()"), app.quit)
  25.  
  26. self.label = QLabel("-------------")
  27.  
  28. # addWidget(widget, row, column, rowSpan=1, columnSpan=1)
  29. grid.addWidget(btn_hello, 0, 0)
  30. # this will span the label over 3 columns
  31. grid.addWidget(self.label, 1, 0, 1, 3)
  32. grid.addWidget(btn_quit, 2, 0)
  33.  
  34. self.setLayout(grid)
  35.  
  36. def on_click(self):
  37. self.label.setText("You clicked the Hello button!")
  38.  
  39.  
  40. app = QApplication(sys.argv)
  41. form = MyForm()
  42. form.show()
  43. app.exec_()
This little PyQT template can be used for quite a few basic programs. Now, if I could introduce some color! Hey, I am a slow learner!
Last edited by vegaseat; May 11th, 2009 at 2:23 pm. Reason: btn
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,941
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Python GUI Programming

 
0
  #8
May 11th, 2009
One of my standard test programs for any GUI toolkit, creating, loading a listbox and selecting an item. Here is the PyQT code ...
  1. # a simple window using PyQT
  2. # with a button and a listbox to load and select
  3.  
  4. import sys
  5. # might be easier to use this import option
  6. from PyQt4.QtCore import *
  7. from PyQt4.QtGui import *
  8.  
  9. class MyForm(QWidget):
  10. def __init__(self, name_list):
  11. QWidget.__init__(self)
  12. # setGeometry(x_pos, y_pos, width, height)
  13. self.setGeometry(100, 150, 300, 220)
  14. self.setWindowTitle("Load the listbox first")
  15.  
  16. # make name_list available for methods
  17. self.name_list = name_list
  18.  
  19. # use a grid layout for the widgets
  20. grid = QGridLayout()
  21.  
  22. btn_load = QPushButton("Load List")
  23. # bind the button click to a function reference
  24. self.connect(btn_load, SIGNAL("clicked()"), self.on_click)
  25.  
  26. self.listbox = QListWidget()
  27. self.connect(self.listbox, SIGNAL("itemSelectionChanged()"), self.on_select)
  28.  
  29. # addWidget(widget, row, column, rowSpan, columnSpan)
  30. grid.addWidget(btn_load, 0, 0, 1, 1)
  31. # listbox spans over 5 rows and 2 columns
  32. grid.addWidget(self.listbox, 1, 0, 5, 2)
  33. self.setLayout(grid)
  34.  
  35. def on_click(self):
  36. """the load button has been clicked, load the listbox"""
  37. self.listbox.addItems(self.name_list)
  38.  
  39. def on_select(self):
  40. """an item in the listbox has been clicked/selected"""
  41. selected_name = self.listbox.selectedItems()[0].text()
  42. self.setWindowTitle(selected_name)
  43.  
  44.  
  45. name_list = [
  46. "Erich Meitinger",
  47. "Udo Baus",
  48. "Jens Klein",
  49. "Bjorn Bork",
  50. "Heidrun Lovelace",
  51. "Klaus Abraham",
  52. "Ulla Jorgens",
  53. "Volger Jenkings",
  54. "Helmut Schmidt",
  55. "Freja Larse",
  56. "Larry Orkan",
  57. "Andreas Mauser",
  58. "Harry Heimlich"
  59. ]
  60.  
  61. app = QApplication(sys.argv)
  62. form = MyForm(name_list)
  63. form.show()
  64. app.exec_()
I do miss the colors. Here is the corresponding wxPython code with some colors added ...
  1. # a simple window using wxPython
  2. # with a button and a listbox to load and select
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7. def __init__(self, name_list):
  8. wx.Frame.__init__(self, parent=None)
  9. self.SetBackgroundColour("green") # ah, color!
  10. self.SetTitle('Load the listbox first')
  11.  
  12. # make name_list available for methods
  13. self.name_list = name_list
  14.  
  15. # use a grid layout for the widgets
  16. grid = wx.GridBagSizer()
  17.  
  18. self.btn_load = wx.Button(self, -1, "Load List")
  19. # bind the button click to a function reference
  20. self.btn_load.Bind(wx.EVT_BUTTON, self.on_click)
  21.  
  22. self.listbox = wx.ListBox(self)
  23. self.listbox.SetBackgroundColour("yellow")
  24. self.listbox.Bind(wx.EVT_LISTBOX, self.on_select)
  25.  
  26. # Add(widget, pos=(row, column), span=(rowspan, columnspan))
  27. grid.Add(self.btn_load, pos=(0, 0), flag=wx.ALL, border=5)
  28. # add a 180 pixel wide spacer
  29. grid.Add((180, 0), pos=(0, 1))
  30. grid.Add(self.listbox, pos=(1,0), span=(10,2),
  31. flag=wx.ALL|wx.EXPAND, border=5)
  32. self.SetSizerAndFit(grid)
  33.  
  34. def on_click(self, event):
  35. """the load button has been clicked, load the listbox"""
  36. self.listbox.Set(self.name_list)
  37.  
  38. def on_select(self, event):
  39. """an item in the listbox has been clicked/selected"""
  40. selected_name = self.listbox.GetStringSelection()
  41. self.SetTitle(selected_name)
  42.  
  43.  
  44. name_list = [
  45. "Erich Meitinger",
  46. "Udo Baus",
  47. "Jens Klein",
  48. "Bjorn Bork",
  49. "Heidrun Lovelace",
  50. "Klaus Abraham",
  51. "Ulla Jorgens",
  52. "Volger Jenkings",
  53. "Helmut Schmidt",
  54. "Freja Larse",
  55. "Larry Orkan",
  56. "Andreas Mauser",
  57. "Harry Heimlich"
  58. ]
  59.  
  60. app = wx.App(0)
  61. MyFrame(name_list).Show()
  62. app.MainLoop()
The wx.GridBagSizer() seems to be more complex.
Last edited by vegaseat; May 11th, 2009 at 9:18 pm. Reason: wx code added
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Python GUI Programming

 
1
  #9
May 12th, 2009
I got PyQT4 istalled and working on my Vista computer. Here is an example of my first program:
  1. # display a bunch of random circles using PyQT4
  2.  
  3. import random
  4. import sys
  5. # pray for minimal namespace conflicts
  6. from PyQt4.QtCore import *
  7. from PyQt4.QtGui import *
  8.  
  9. class DrawPoints(QWidget):
  10. def __init__(self, parent=None):
  11. QWidget.__init__(self, parent)
  12. # setGeometry(x_pos, y_pos, width, height)
  13. self.setGeometry(200, 200, 400, 400)
  14. self.setWindowTitle('Draw random Circles')
  15.  
  16. def paintEvent(self, event):
  17. painter = QPainter()
  18. painter.begin(self)
  19. # pen sets the edge color of the circles
  20. painter.setPen(Qt.black)
  21. w = self.size().width()
  22. h = self.size().height()
  23. # draw 150 circles of random sizes, locations and colors
  24. for i in range(150):
  25. # color uses red, green, blue values (0 to 255)
  26. r = random.randint(0, 255)
  27. g = random.randint(0, 255)
  28. b = random.randint(0, 255)
  29. # brush sets the fill color of the circles
  30. painter.setBrush(QBrush(QColor(r, g, b)))
  31. # get center coordinates x,y of the circle
  32. x = random.randint(1, w-1)
  33. y = random.randint(1, h-1)
  34. # get the radius of the circle
  35. radius = random.randint(5, 80)
  36. # to draw circles match the radius
  37. painter.drawEllipse(QPoint(x, y), radius, radius)
  38.  
  39. painter.end()
  40.  
  41.  
  42. app = QApplication(sys.argv)
  43. dp = DrawPoints()
  44. dp.show()
  45. app.exec_()
Last edited by Ene Uran; May 12th, 2009 at 2:32 pm.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Python GUI Programming

 
1
  #10
May 12th, 2009
An example how to use the QPainter to create a wallpaper background:
  1. # a simple window using PyQT
  2. # using a canvas with a texture/wallpaper background
  3.  
  4. import sys
  5. # pray for minimal namespace conflicts
  6. from PyQt4.QtCore import *
  7. from PyQt4.QtGui import *
  8.  
  9. class MyForm(QWidget):
  10. def __init__(self):
  11. QWidget.__init__(self)
  12. # setGeometry(x_pos, y_pos, width, height)
  13. self.setGeometry(100, 150, 350, 300)
  14. self.setWindowTitle("Creating a Canvas Wallpaper")
  15.  
  16. def paintEvent(self, event):
  17. """create a painting canvas"""
  18. painter = QPainter()
  19. painter.begin(self)
  20. painter.setRenderHint(QPainter.Antialiasing)
  21. # use the brush for a texture/wallpaper background
  22. # supply a background image file you have (add needed path)
  23. painter.setBrush(QBrush(QPixmap("BG_GoldSwirl.gif")))
  24. painter.drawRect(event.rect())
  25.  
  26. # optionally write something in the wallpaper
  27. # (check the fonts available on your computer)
  28. painter.setFont(QFont('Freestyle Script', 48))
  29. painter.drawText(50, 160, "Hello World!")
  30.  
  31. painter.end()
  32.  
  33.  
  34. app = QApplication(sys.argv)
  35. form = MyForm()
  36. form.show()
  37. app.exec_()
Attached Images
 
drink her pretty
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC