Python GUI Programming

Reply

Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Python GUI Programming

 
1
  #41
Aug 5th, 2009
Look at PyQT's progress bar widget and simple timer to drive it:
  1. # pqt_progressbar1.py
  2. # explore PyQT's progress bar and simple timer widgets
  3. # Henri
  4.  
  5. import sys
  6. from PyQt4.QtCore import *
  7. from PyQt4.QtGui import *
  8.  
  9. class MyFrame(QWidget):
  10. def __init__(self, parent=None):
  11. QWidget.__init__(self, parent)
  12. # setGeometry(x_pos, y_pos, width, height)
  13. self.setGeometry(300, 300, 250, 150)
  14. self.setWindowTitle('QProgressBar')
  15.  
  16. self.pbar = QProgressBar(self)
  17. self.pbar.setGeometry(30, 40, 200, 25)
  18.  
  19. self.button = QPushButton('Start', self)
  20. self.button.setFocusPolicy(Qt.NoFocus)
  21. # absolute positioning
  22. self.button.move(40, 80)
  23.  
  24. self.connect(self.button, SIGNAL('clicked()'), self.onStart)
  25.  
  26. # QBasicTimer is simplified timer
  27. # use QTimer for more options and control
  28. self.timer = QBasicTimer()
  29. self.step = 0;
  30.  
  31. def timerEvent(self, event):
  32. """preset method for timer widget"""
  33. if self.step >= 100:
  34. self.timer.stop()
  35. return
  36. self.step += 1
  37. self.pbar.setValue(self.step)
  38.  
  39. def onStart(self):
  40. """toggle button action and label"""
  41. if self.timer.isActive():
  42. self.timer.stop()
  43. self.button.setText('Start')
  44. else:
  45. # timer interval of 100 milliseconds
  46. self.timer.start(100, self)
  47. self.button.setText('Stop')
  48.  
  49.  
  50. app = QApplication(sys.argv)
  51. frame = MyFrame()
  52. frame.show()
  53. app.exec_()
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Python GUI Programming

 
1
  #42
Aug 5th, 2009
Put PyQT widgets on a color background using canvas:
  1. # pqt_CanvasBackGround1.py
  2. # use PyQT's paintEvent to create full frame canvas
  3. # widgets will use the canvas as background
  4. # Henri
  5.  
  6. import sys
  7. from PyQt4.QtCore import *
  8. from PyQt4.QtGui import *
  9.  
  10. class MyFrame(QWidget):
  11. def __init__(self):
  12. QWidget.__init__(self)
  13. # setGeometry(x_pos, y_pos, width, height)
  14. self.setGeometry(100, 150, 350, 200)
  15. self.setWindowTitle("paintEvent canvas as background")
  16. # specify the canvas background color (r, g, b)
  17. self.plum = QColor(221, 160, 221)
  18.  
  19. # create some regular widgets
  20. # that will use the canvas as background
  21. btn_red = QPushButton("Red")
  22. btn_green = QPushButton("Green")
  23. btn_blue = QPushButton("Blue")
  24. # the label acts transparent, buttons do not
  25. label = QLabel(" 1 \n 2 \n 3 \n 4 \n 5 \n 6 \n")
  26.  
  27. # use grid layout for the widgets
  28. grid = QGridLayout()
  29. # addWidget(widget, row, column, rowSpan=1, columnSpan=1)
  30. grid.addWidget(btn_red, 0, 0, 1, 1)
  31. grid.addWidget(btn_green, 0, 1, 1, 1)
  32. grid.addWidget(btn_blue, 1, 0, 1, 1)
  33. grid.addWidget(label, 2, 0, 1, 1)
  34. self.setLayout(grid)
  35.  
  36. def paintEvent(self, event):
  37. """auto-create the painting canvas"""
  38. painter = QPainter()
  39. painter.begin(self)
  40. # use brush for rectangle background
  41. painter.setBrush(QBrush(self.plum))
  42. # set recangle to full frame size
  43. painter.drawRect(event.rect())
  44. painter.end()
  45.  
  46.  
  47. app = QApplication(sys.argv)
  48. frame = MyFrame()
  49. frame.show()
  50. app.exec_()
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,972
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: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Python GUI Programming

 
0
  #43
Aug 5th, 2009
I got inspired by Henri's nice PyQT studies, so here is my small contribution, an "almost IDE" ...
  1. # pqt_RunPyFile2.pyw
  2. # run a given Python script file with a given version of Python
  3. # show the script and the result
  4. # use with Python3 or change line 82 and 83
  5. # tested with Python31 and PyQT45 by vegaseat 05aug2009
  6.  
  7. import subprocess
  8. import sys
  9. from PyQt4.QtCore import *
  10. from PyQt4.QtGui import *
  11.  
  12. class MyWindow(QWidget):
  13. def __init__(self, python_path, initial_dir, *args):
  14. QWidget.__init__(self, *args)
  15. # setGeometry(x_pos, y_pos, width, height)
  16. self.setGeometry(300, 200, 640, 400)
  17. s = "Run a Python script file with %s" % python_path
  18. self.setWindowTitle(s)
  19. self.python_path = python_path
  20. self.initial_dir = initial_dir
  21.  
  22. self.show_fname = QLineEdit()
  23. self.show_fname.setToolTip("press enter key")
  24. self.show_script = QTextEdit()
  25. self.show_result = QTextEdit()
  26.  
  27. # use a vertical boc layout
  28. layout = QVBoxLayout(self)
  29. layout.addWidget(self.show_fname)
  30. layout.addWidget(self.show_script)
  31. layout.addWidget(self.show_result)
  32. self.setLayout(layout)
  33.  
  34. self.file_dialog()
  35. self.load_script()
  36.  
  37. # press return key in show_fname to start run_command()
  38. # allows you to add commandline args
  39. self.connect(self.show_fname, SIGNAL("returnPressed(void)"),
  40. self.run_command)
  41.  
  42. def run_command(self):
  43. """use subprocess.Popen to run the command line"""
  44. py_file = str(self.show_fname.text())
  45. self.setWindowTitle(py_file)
  46. py_exe = self.python_path
  47. option = "-u"
  48. py_script = py_file
  49. p = subprocess.Popen([py_exe, option, py_script],
  50. bufsize=2048, shell=True, stdin=subprocess.PIPE,
  51. stdout=subprocess.PIPE, close_fds=False)
  52. # write additional args to the external program
  53. #p.stdin.write("args")
  54. # allow external program to work
  55. p.wait()
  56. result_str = p.stdout.read()
  57. # the result is a bytearray in Python3
  58. if type(result_str) != str:
  59. # decode <class 'bytes'> to string
  60. result_str = result_str.decode("utf8")
  61. s = "my result -->\n%s" % result_str
  62. self.show_result.setText(s)
  63.  
  64. def file_dialog(self):
  65. """get the Python script file's mname"""
  66. #getOpenFileName (parent, caption, dir, filter,
  67. # selectedFilter, options)
  68. self.filename = QFileDialog.getOpenFileName(self,
  69. 'Open file', self.initial_dir,
  70. "Python files (*.py *.pyw)")
  71. self.show_fname.setText(self.filename)
  72.  
  73. def load_script(self):
  74. # PyQT needs this elaborate process
  75. # to deal with loading text ...
  76. try:
  77. fh = QFile(self.filename)
  78. if not fh.open(QIODevice.ReadOnly):
  79. raise IOError(str(fh.errorString()))
  80. stream = QTextStream(fh)
  81. stream.setCodec("UTF-8")
  82. self.show_script.setPlainText(stream.readAll())
  83. except IOError as e: # Python3
  84. #except IOError, e: # Python2
  85. QMessageBox.warning(self, "Load Error",
  86. "Failed to load %s: %s" % (self.filename, e))
  87. finally:
  88. if fh is not None:
  89. fh.close()
  90.  
  91.  
  92. # modify for correct path and Operating System syntax
  93. python_path = "D:/Python31/Python.exe"
  94. initial_dir = "./"
  95.  
  96. app = QApplication(sys.argv)
  97. win = MyWindow(python_path, initial_dir)
  98. win.show()
  99. sys.exit(app.exec_())
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Python GUI Programming

 
1
  #44
Aug 6th, 2009
PyGame isn't the only toolkit you can use for animation. If you use Python 3.1 and still wait for PyGame to be available, you can use Tinter for some simple animations:
  1. # Tk_CanvasMove1.py
  2. # explore Tkinter animation via canvas.move()
  3. # Henri
  4.  
  5. import time
  6. try:
  7. # for Python2
  8. import Tkinter as tk
  9. except ImportError:
  10. # for Python3
  11. import tkinter as tk
  12.  
  13. root = tk.Tk()
  14. root.title("canvas.move() test")
  15.  
  16. canvas = tk.Canvas(root, width=400, height=380)
  17. canvas.pack()
  18.  
  19. # canvas.create_rectangle(x0, y0, x1, y1, option, ... )
  20. # x0, y0, x1, y1 are corner coordinates of ulc to lrc diagonal
  21. rc1 = canvas.create_rectangle(20, 260, 120, 360,
  22. outline='white', fill='blue')
  23. rc2 = canvas.create_rectangle(20, 10, 120, 110,
  24. outline='white', fill='red')
  25.  
  26. # initial delay to view setting
  27. canvas.update()
  28. time.sleep(0.7)
  29.  
  30. for x in range(50):
  31. y = x = 5
  32. time.sleep(0.025)
  33. # canvas.move(obj, xAmount, yAmount)
  34. # objects are rectangles rc1 and rc2
  35. canvas.move(rc1, x, -y)
  36. canvas.move(rc2, x, y)
  37. canvas.update()
  38.  
  39. time.sleep(0.7)
  40.  
  41. # reverse move
  42. for x in range(50, 0, -1):
  43. y = x = -5
  44. time.sleep(0.025)
  45. # canvas.move(obj, xAmount, yAmount)
  46. # objects are rectangles rc1 and rc2
  47. canvas.move(rc1, x, -y)
  48. canvas.move(rc2, x, y)
  49. canvas.update()
  50.  
  51.  
  52. root.mainloop()
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Python GUI Programming

 
1
  #45
Aug 13th, 2009
In the previous example I meant 'Tkinter' rather than 'Tinter'. This however is PyQT example looking at the tooltip widget and some colored text:
  1. # pqt_tooltip1.py
  2. # exploring PyQT's tooltip and color text (html)
  3. # Henri
  4.  
  5. import sys
  6. from PyQt4.QtCore import *
  7. from PyQt4.QtGui import *
  8.  
  9. class Tooltip(QWidget):
  10. def __init__(self, parent=None):
  11. QWidget.__init__(self, parent)
  12. # setGeometry(x_pos, y_pos, width, height)
  13. self.setGeometry(300, 300, 250, 150)
  14. self.setWindowTitle('Tooltip')
  15.  
  16. btn_hello = QPushButton("Push Button")
  17. btn_hello.setToolTip('Press <b>me</b> for the greeting')
  18. self.connect(btn_hello, SIGNAL("clicked()"), self.on_click)
  19.  
  20. self.label = QLabel()
  21.  
  22. grid = QGridLayout()
  23. grid.addWidget(btn_hello, 0, 0, 1, 1)
  24. grid.addWidget(self.label, 1, 0, 1, 3)
  25. self.setLayout(grid)
  26.  
  27.  
  28. def on_click(self):
  29. s = "<font size=6 color='red'>Hello stranger!</font>"
  30. self.label.setText(s)
  31.  
  32.  
  33. app = QApplication(sys.argv)
  34. tooltip = Tooltip()
  35. tooltip.show()
  36. app.exec_()
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Python GUI Programming

 
1
  #46
Aug 13th, 2009
Retrieve the text from Tkinter Text widget line by line:
  1. # explore the Tkinter Text() widget
  2. # getting text 'line by line' from the widget
  3. # use ctrl+c to copy, ctrl+x to cut selected text,
  4. # ctrl+v to paste, and ctrl+/ to select all
  5. # Henri
  6.  
  7. try:
  8. # for Python2
  9. import Tkinter as tk
  10. except ImportError:
  11. # for Python3
  12. import tkinter as tk
  13.  
  14. def get_text(event):
  15. """get text widget contents of given line n (firstline=1)"""
  16. # retrieve the line number from button text
  17. n = int(event.widget.cget("text"))
  18. # set up start and end of the line
  19. start = "%d.0" % n
  20. end = "%d.end" % n
  21. # get the line text
  22. s = text.get(start, end)
  23. # show it in the label
  24. label['text'] = s
  25.  
  26. # test data
  27. mw = """\
  28. Na Sodium 22.98977
  29. Mg Magnesium 24.305
  30. Al Aluminum 26.98154
  31. Si Silicon 28.0855
  32. P Phosphorus 30.97376
  33. S Sulfur 32.066
  34. Cl Chlorine 35.4527
  35. Ar Argon 39.948"""
  36.  
  37. root = tk.Tk()
  38. root.title('get text line by line')
  39.  
  40. # text field, width=width chars, height=lines text
  41. text = tk.Text(root, width=30, height=8, bg='yellow')
  42. text.insert('insert', mw)
  43. text.pack(pady=5)
  44.  
  45. # create buttons to call each line by number
  46. for n, data in enumerate(mw.split('\n')):
  47. # first line in text is 1
  48. s = str(n+1)
  49. btn = tk.Button(root, text=s)
  50. btn.bind("<Button-1>", get_text)
  51. btn.pack(side='left', pady=5, padx=2)
  52.  
  53. # width/height in char size
  54. label = tk.Label(root, text="", width=30, height=2)
  55. label.pack(side='top', pady=5)
  56.  
  57. root.mainloop()
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
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

 
1
  #47
Aug 13th, 2009
Here is an example of a scrolled Tkinter canvas to display a series of drawn objects or pictures:
  1. # example of a Tkinter scrolled canvas class
  2. # snee
  3.  
  4. try:
  5. # for Python2
  6. import Tkinter as tk
  7. except ImportError:
  8. # for Python3
  9. import tkinter as tk
  10.  
  11. class ScrolledCanvas(tk.Frame):
  12. def __init__(self, parent=None, color='brown'):
  13. tk.Frame.__init__(self, parent)
  14. self.pack(expand='yes', fill='both')
  15. self.parent = parent
  16.  
  17. canvas = tk.Canvas(self, bg=color, relief='sunken')
  18. #canvas.config(width=300, height=200)
  19. canvas.config(scrollregion=(0, 0, 300, 1000))
  20. canvas.config(highlightthickness=0)
  21.  
  22. sbar = tk.Scrollbar(self)
  23. # vertical scrollbar
  24. sbar.config(command=canvas.yview)
  25. canvas.config(yscrollcommand=sbar.set)
  26. sbar.pack(side='right', fill='y')
  27. canvas.pack(side='left', expand='yes', fill='both')
  28.  
  29. # put in some text for testing ...
  30. for k in range(10):
  31. s = 'now calling #' + str(k)
  32. canvas.create_text(150, 50+(k*100), text=s, fill='beige')
  33. # for testing canvas (x, y)
  34. canvas.bind('<Double-1>', self.onDoubleClick)
  35. self.canvas = canvas
  36.  
  37. def onDoubleClick(self, event):
  38. """show location (x, y) of double click on canvas"""
  39. #print( event.x, event.y )
  40. x = self.canvas.canvasx(event.x)
  41. y = self.canvas.canvasy(event.y)
  42. s = "x = %s y = %s" % (x, y)
  43. self.parent.title(s)
  44.  
  45. if __name__ == '__main__':
  46. root = tk.Tk()
  47. # use width x height + x_offset + y_offset (no spaces!)
  48. root.geometry("%dx%d+%d+%d" % (350, 250, 100, 80))
  49. root.title('testing the scrolled canvas')
  50. ScrolledCanvas(root).mainloop()
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,972
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: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Python GUI Programming

 
1
  #48
Aug 14th, 2009
The Tkinter version that comes with the Python 3.1 installation includes expansion module ttk. This module includes a couple of widgets missing from Tkinter, like a tabbed notebook. As usual, actual examples are totally missing from the Python manual, so I give you at least a hint of its use ...
  1. # ttk_notebook1.py
  2. # exploring the Tkinter expansion module ttk notebook
  3. # tested with Python 3.1 and Tkinter 8.5 by vegaseat
  4.  
  5. import tkinter as tk
  6. import tkinter.ttk as ttk
  7.  
  8. root = tk.Tk()
  9. # use width x height + x_offset + y_offset (no spaces!)
  10. root.geometry("%dx%d+%d+%d" % (300, 200, 100, 50))
  11. root.title('test the ttk.Notebook')
  12.  
  13. nb = ttk.Notebook(root)
  14. nb.pack(fill='both', expand='yes')
  15.  
  16. # create a child wdget for each page
  17. f1 = tk.Frame(bg='red')
  18. f2 = tk.Frame(bg='blue')
  19. f3 = tk.Frame(bg='green')
  20.  
  21. # create the pages
  22. nb.add(f1, text='page1')
  23. nb.add(f2, text='page2')
  24. nb.add(f3, text='page3')
  25.  
  26. # put a button widget on child f1
  27. btn1 = tk.Button(f1, text='button1')
  28. btn1.pack(side='left', anchor='nw', padx=3, pady=5)
  29.  
  30. root.mainloop()
And here is the combobox, a combination of drop down listbox and entrybox ...
  1. # ttk_combobox2.py
  2. # exploring the Tkinter expansion module ttk combobox
  3. # tested with Python 3.1 and Tkinter 8.5 by vegaseat
  4.  
  5. import tkinter as tk
  6. import tkinter.ttk as ttk
  7.  
  8. def action(event):
  9. """
  10. a combo box item has been selected, do some action
  11. """
  12. label['bg'] = combo.get()
  13.  
  14.  
  15. root = tk.Tk()
  16.  
  17. # create a label
  18. label = tk.Label(text='select a color', bg='white')
  19.  
  20. # create the combo box
  21. combo = ttk.Combobox()
  22. combo.bind('<<ComboboxSelected>>', action)
  23.  
  24. colors = ['red', 'green', 'magenta', 'yellow']
  25. # load the combo box with the colors list
  26. combo['values'] = colors
  27.  
  28. # set the initial color
  29. combo.set('yellow')
  30. label['bg'] = combo.get()
  31.  
  32. # pack the widgets vertically in this order
  33. label.pack(fill='both', expand='yes')
  34. combo.pack()
  35.  
  36. root.mainloop()
Last edited by vegaseat; Aug 14th, 2009 at 8:33 pm. Reason: combobox
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
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

 
1
  #49
Aug 14th, 2009
Here is a basic template of a window, 2 buttons and a label using the pygtk toolkit:
  1. # a very basic pygtk window template
  2. # with two buttons and one label
  3. # snee
  4.  
  5. import pygtk
  6. pygtk.require('2.0')
  7. import gtk
  8.  
  9. class MyWindow(object):
  10. def __init__(self):
  11. # type=gtk.WINDOW_TOPLEVEL is default
  12. self.window = gtk.Window()
  13. # pygtk needs this
  14. # attach destroy signal to terminate application properly
  15. self.window.connect("destroy", lambda w: gtk.main_quit())
  16. # title bar caption
  17. self.window.set_title("gtk.Window template")
  18. # put upper left corner on display coordinates (x, y)
  19. self.window.move(100, 80)
  20. # resize(width, height)
  21. self.window.resize(350, 150)
  22.  
  23. # create 2 button widgets and a label widget
  24. button1 = gtk.Button("Button 1")
  25. # connect mouse click to a callback
  26. button1.connect("clicked", self.callback, "Tom")
  27. button2 = gtk.Button("Button 2")
  28. # connect mouse click to a callback
  29. button2.connect("clicked", self.callback, "Frank")
  30. self.label = gtk.Label("Hello!")
  31.  
  32. # create a vertical box to pack the widgets into
  33. box_v = gtk.VBox(False, 0)
  34. self.window.add(box_v)
  35. # now pack the widgets into the box (top to bottom order)
  36. box_v.pack_start(button1, False, False, 0)
  37. box_v.pack_start(button2, False, False, 0)
  38. box_v.pack_start(self.label, True, True, 0)
  39.  
  40. # now show all the widgets including the window
  41. button1.show()
  42. button2.show()
  43. self.label.show()
  44. box_v.show()
  45. self.window.show()
  46.  
  47. def callback(self, widget, data=None):
  48. s = "You clicked %s's button" % data
  49. self.window.set_title(s)
  50. if data == "Tom":
  51. self.label.set_text("Major Tom")
  52. else:
  53. self.label.set_text("Major Frank Burns")
  54.  
  55.  
  56. MyWindow()
  57. gtk.main()
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
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

 
1
  #50
Aug 19th, 2009
If you have young children at home, or are yourself a young child at heart, there is a delightful Python module called frog, that does turtle like drawings and more:
  1. # frog is a somewhat advanced turtle graphics module
  2. # get frog-1.0.1.zip from:
  3. # http://www.zahlenfreund.de/python/frog.html
  4. # see also:
  5. # http://pypi.python.org/pypi/frog/1.0.0
  6. # unzip and copy frog.py into the Python lib folder
  7. # or use it in the working folder
  8. # works with Python2 and Python3
  9.  
  10. import frog
  11.  
  12. # pond forms the canvas
  13. pond = frog.Pool()
  14. pond.title = "Watch the frog move and draw"
  15. pond.bgcolor = "lightblue"
  16. # kermit forms the drawing pen
  17. kermit = frog.Frog(pond)
  18. kermit.shape = "frog"
  19. kermit.bodycolor = "green"
  20. # kermit draws a square
  21. # starts at the center of the pond
  22. for n in range(4):
  23. kermit.move(100)
  24. kermit.turn(90)
  25. # turn kermit in the other direction
  26. kermit.turn(180)
  27. for n in range(4):
  28. kermit.move(100)
  29. kermit.turn(90)
  30. pond.ready()
Just a little more for the fun of it:
  1. # get frog-1.0.1.zip from:
  2. # http://www.zahlenfreund.de/python/frog.html
  3. # unzip and copy frog.py into the Python lib folder
  4. # or use it in the working folder
  5. # works with Python2 and Python3
  6.  
  7. import frog
  8.  
  9. canvas = frog.Pool()
  10. canvas.setbgcolor('green')
  11. # use the default triangle shape
  12. pen = frog.Frog(canvas)
  13. pen.color = "red"
  14. # pen normally starts in the canvas center
  15. # make it jump (no drawing) left and then down
  16. pen.jump(-50)
  17. pen.turn(90)
  18. pen.jump(-25)
  19. pen.turn(270)
  20. # the pen draws a number of 'pentagon shapes'
  21. # starts at the center and forms a star
  22. for n in range(9):
  23. pen.move(100)
  24. pen.turn(80)
  25. canvas.ready()
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

Tags
examples, gui, pygame, pyglet, pygtk, pyqt, python, tkinter

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC