Python GUI Programming

Reply

Join Date: Oct 2004
Posts: 3,862
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: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Python GUI Programming

 
0
  #21
Jun 30th, 2009
PyGame is a GUI based module used mostly for game programming with Python. It comes with colorful graphics, sprites for animation, sound and responds to the mouse and keyboard ...
  1. # a simple pygame example
  2. # module pygame free from: http://www.pygame.org
  3. # creates nested circles on a yellow background
  4. # vegaseat
  5.  
  6. import pygame
  7.  
  8. pygame.init()
  9.  
  10. # create a 300 x 300 pixel display window
  11. # the default background is black
  12. win = pygame.display.set_mode((300, 300))
  13. # add nice title
  14. pygame.display.set_caption("Bull's Eye!")
  15.  
  16. # pygame uses (r, g, b) color tuples
  17. white = (255, 255, 255)
  18. black = (0, 0, 0)
  19. red = (255, 0, 0)
  20. yellow = (255, 255, 0)
  21.  
  22. # create a canvas (in memory) to draw on
  23. canvas = pygame.Surface(win.get_size())
  24. # fill surface
  25. canvas.fill(yellow)
  26.  
  27. center = (150, 150)
  28. # draw a black border circle
  29. radius = 92
  30. pygame.draw.circle(canvas, black, center, radius)
  31.  
  32. # draw a white circle
  33. radius = 90
  34. pygame.draw.circle(canvas, white, center, radius)
  35.  
  36. # draw a red circle that fits into the white one
  37. radius = 80
  38. pygame.draw.circle(canvas, red, center, radius)
  39.  
  40. # finally the black bull's eye
  41. radius = 10
  42. pygame.draw.circle(canvas, black, center, radius)
  43.  
  44. # transfer canvas to display window at ulc (x=0, y=0)
  45. win.blit(canvas, (0, 0))
  46.  
  47. # update/flip to show on the computer display
  48. pygame.display.flip()
  49.  
  50. # event loop and exit conditions (windows titlebar x click)
  51. while True:
  52. for event in pygame.event.get():
  53. if event.type == pygame.QUIT:
  54. raise SystemExit
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
  #22
Jul 5th, 2009
Here is a general notebook widget class for Tkinter:
  1. # testing a notebook widget class for Tkinter (modified)
  2. # Python 2.5.4 ene
  3.  
  4. import Tkinter as tk
  5.  
  6. class Notebook(object):
  7. """
  8. a notebook widget class for Tkinter applications
  9. """
  10. def __init__(self, parent):
  11. self.active_page = None
  12. self.count = 0
  13. self.selected = tk.IntVar(0)
  14. # orientation of initial tab (can go 'bottom' too)
  15. side = 'top'
  16. # new tabs go
  17. self.side= 'left'
  18. # create notebook's initial page frame
  19. self.tab = tk.Frame(parent)
  20. self.tab.pack(side=side, fill='both')
  21. self.page = tk.Frame(parent)
  22. self.page.pack(fill='both')
  23.  
  24. def __call__(self):
  25. """
  26. parent page reference
  27. """
  28. return self.page
  29.  
  30. def add_page(self, pg, title):
  31. """
  32. add a new page to the notebook
  33. """
  34. rb = tk.Radiobutton(self.tab, text=title, indicatoron=0, variable=self.selected,
  35. value=self.count, command=lambda: self.display_page(pg))
  36. rb.pack(fill='both', side=self.side)
  37. # first page is slected by default
  38. if not self.active_page:
  39. pg.pack(fill='both', expand=True)
  40. self.active_page = pg
  41. self.count += 1
  42. # returns reference
  43. return rb
  44.  
  45. def display_page(self, pg):
  46. """
  47. shows selected page, hides former page
  48. """
  49. self.active_page.forget()
  50. pg.pack(fill='both', expand=True)
  51. self.active_page = pg
  52.  
  53.  
  54. # testing the module
  55. if __name__ == '__main__':
  56. root = tk.Tk()
  57. # use width x height + x_offset + y_offset (no spaces!)
  58. root.geometry("400x200+100+50")
  59. root.title('Testing a Tkinter notebook widget')
  60.  
  61. nb = Notebook(root)
  62.  
  63. # create first page (notice the instance call)
  64. page1 = tk.Frame(nb())
  65. nb.add_page(page1, 'page 1')
  66. # put something on the page
  67. # text entry field, width=width in chars, height=lines text
  68. text1 = tk.Text(page1, width=60, height=12, bg='yellow')
  69. text1.pack(fill='both')
  70. text1.insert(tk.INSERT, ' this is page number 1')
  71.  
  72. # create second page, also has a button
  73. page2 = tk.Frame(nb())
  74. nb.add_page(page2, 'page 2')
  75. # put something on the page
  76. text2 = tk.Text(page2, width=60, height=12, bg='green')
  77. text2.pack(fill='both')
  78. s1 = ' this is page number 2 \n'
  79. s2 = ' Look, I am green! \n'
  80. s3 = ' Also, I have a button to click!'
  81. text2.insert(tk.INSERT, s1+s2+s3)
  82. # just a dummy button
  83. button2 = tk.Button(page2, text=' save text to file ')
  84. button2.pack(side='left')
  85.  
  86. root.mainloop()
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 23
Reputation: Arrorn is an unknown quantity at this point 
Solved Threads: 4
Arrorn Arrorn is offline Offline
Newbie Poster

Re: Python GUI Programming

 
0
  #23
Jul 13th, 2009
I just recently wanted to dabble in GUI programming so I took one of my older programs and added a GUI to it.

By the way: I'm using python 3.1 and the tkinter that comes with it... they changed the module name from Tkinter to tkinter in 3.1...

  1. import random
  2. import tkinter as tk
  3.  
  4. def Coin_Toss():
  5. try:
  6. n = float(enter.get())
  7. if n > 0:
  8. label2.config(text='')
  9. except ValueError:
  10. label2.config(text='Need numeric value')
  11.  
  12. display = ""
  13.  
  14. heads = 0
  15. tails = 0
  16. counter = 0
  17. Heads = "H"
  18. Tails = "T"
  19.  
  20. try:
  21. while (counter < n):
  22. if random.randrange(2):
  23. heads += 1
  24. if heads > 0:
  25. display = str(display + " " + Heads)
  26. label.config(text=display)
  27. counter += 1
  28. else:
  29. tails += 1
  30. if tails > 0:
  31. display = str(display + " " + Tails)
  32. label.config(text=display)
  33. counter += 1
  34. hep = heads * 100 / n
  35. tap = tails * 100 / n
  36. h = repr(heads)
  37. t = repr(tails)
  38. hp = repr(hep)
  39. tp = repr(tap)
  40. display = display + " "
  41. label.config(text=display)
  42. display1 = str("The coin landed on heads " + h + " times.")
  43. label3.config(text=display1)
  44. display2 = str("The coin landed on tails " + t + " times.")
  45. label4.config(text=display2)
  46. display3 = str("The coin landed on heads " + hp + "% of the time.")
  47. label5.config(text=display3)
  48. display4 = str("The coin landed on tails " + tp + "% of the time.")
  49. label6.config(text=display4)
  50. btn.config(text=' End ', command=End)
  51. except UnboundLocalError:
  52. pass
  53.  
  54. def End():
  55. display = "Thank you for running Coin Toss."
  56. label.config(text=display)
  57. display = ''
  58. label.config(text=display)
  59. n = 0
  60. label.config(text='')
  61. label2.config(text='')
  62. label3.config(text='')
  63. label4.config(text='')
  64. label5.config(text='')
  65. label6.config(text='')
  66. btn.config(text=' Run ', command=Coin_Toss)
  67.  
  68.  
  69. root = tk.Tk()
  70. root.title("DevPython Suite")
  71.  
  72. label = tk.Label(root, text='')
  73. label1 = tk.Label(root, text='Number of times to flip the coin: ')
  74. label2 = tk.Label(root, text='')
  75. label3 = tk.Label(root, text='')
  76. label4 = tk.Label(root, text='')
  77. label5 = tk.Label(root, text='')
  78. label6 = tk.Label(root, text='')
  79. label0 = tk.Label(root, text='Welcome to the Coin Toss Program')
  80. enter = tk.Entry(root)
  81. btn = tk.Button(root, text=' Run ', command=Coin_Toss)
  82.  
  83. label0.grid(row=0, column=0)
  84. label1.grid(row=1, column=0)
  85. enter.grid(row=2,column=0)
  86. btn.grid(row=3,column=0)
  87. label2.grid(row=4,column=0)
  88. label.grid(row=5,column=0)
  89. label3.grid(row=6,column=0)
  90. label4.grid(row=7,column=0)
  91. label5.grid(row=8,column=0)
  92. label6.grid(row=9,column=0)
  93.  
  94. enter.focus_set()
  95.  
  96. root.mainloop()

any tips for a guy just starting to program GUI's?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 23
Reputation: Arrorn is an unknown quantity at this point 
Solved Threads: 4
Arrorn Arrorn is offline Offline
Newbie Poster

Re: Python GUI Programming

 
0
  #24
Jul 14th, 2009
I made some minor changes to the program above but i don't feel like posting it with so little changes... the jist of what i changed is i changed:
  1. #insert the lines of code in place of the existing code in the lines mentioned
  2.  
  3. import random, time #line 1
  4. message.config(text=display) #lines 41
  5. label2.config(text=display) #line 56
  6. message.config(text='') #line 60
  7. message = tk.Message(root, text='') #line 72
  8. message.grid(row=5,column=0) #line 88
  9.  
  10. #delete lines 26, 32, 40, 57, 58, & 61

now it can display up to around 3000
Last edited by Arrorn; Jul 14th, 2009 at 2:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,862
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: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Python GUI Programming

 
0
  #25
Jul 17th, 2009
This an example showing the use of ttk that comes with the Python 3.1 installation as part of tkinter ...
  1. '''
  2. Python31 includes the Tkinter Tile extension Ttk.
  3.  
  4. Ttk comes with 17 widgets, 11 of which already exist in Tkinter:
  5. Button, Checkbutton, Entry, Frame, Label, LabelFrame, Menubutton,
  6. PanedWindow, Radiobutton, Scale and Scrollbar
  7.  
  8. The 6 new widget classes are:
  9. Combobox, Notebook, Progressbar, Separator, Sizegrip and Treeview
  10.  
  11. For additional info see the Python27 or Python31 manual:
  12. http://gpolo.ath.cx:81/pydoc/library/ttk.html
  13.  
  14. Here the TreeView widget is configured as a multi-column listbox
  15. with adjustable column width and column-header-click sorting.
  16.  
  17. Tested with Python3.1 and Tkinter8.5 by vegaseat 17jul2009
  18. '''
  19.  
  20. import tkinter as tk
  21. import tkinter.font as tkFont
  22. import tkinter.ttk as ttk
  23.  
  24. class McListBox(object):
  25. """use a ttk.TreeView as a multicolumn ListBox"""
  26. def __init__(self):
  27. self.tree = None
  28. self._setup_widgets()
  29. self._build_tree()
  30.  
  31. def _setup_widgets(self):
  32. s = """\
  33. click on header to sort by that column
  34. to change width of column drag boundary
  35. """
  36. msg = ttk.Label(wraplength="4i", justify="left", anchor="n",
  37. padding=(10, 2, 10, 6), text=s)
  38. msg.pack(fill='x')
  39.  
  40. container = ttk.Frame()
  41. container.pack(fill='both', expand=True)
  42.  
  43. # create a treeview with dual scrollbars
  44. self.tree = ttk.Treeview(columns=element_header, show="headings")
  45. vsb = ttk.Scrollbar(orient="vertical", command=self.tree.yview)
  46. hsb = ttk.Scrollbar(orient="horizontal", command=self.tree.xview)
  47. self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)
  48. self.tree.grid(column=0, row=0, sticky='nsew', in_=container)
  49. vsb.grid(column=1, row=0, sticky='ns', in_=container)
  50. hsb.grid(column=0, row=1, sticky='ew', in_=container)
  51.  
  52. container.grid_columnconfigure(0, weight=1)
  53. container.grid_rowconfigure(0, weight=1)
  54.  
  55. def _build_tree(self):
  56. for col in element_header:
  57. self.tree.heading(col, text=col.title(),
  58. command=lambda c=col: sortby(self.tree, c, 0))
  59. # adjust the column's width to the header string
  60. self.tree.column(col, width=tkFont.Font().measure(col.title()))
  61.  
  62. for item in element_list:
  63. self.tree.insert('', 'end', values=item)
  64.  
  65. # adjust column's width if necessary to fit each value
  66. for ix, val in enumerate(item):
  67. col_w = tkFont.Font().measure(val)
  68. if self.tree.column(element_header[ix], width=None) < col_w:
  69. self.tree.column(element_header[ix], width=col_w)
  70.  
  71.  
  72. def isnumeric(s):
  73. """test if a string is numeric"""
  74. for c in s:
  75. if c in "1234567890-.":
  76. numeric = True
  77. else:
  78. return False
  79. return numeric
  80.  
  81. def change_numeric(data):
  82. """if the data to be sorted is numeric change to float"""
  83. new_data = []
  84. if isnumeric(data[0][0]):
  85. # change child to a float
  86. for child, col in data:
  87. new_data.append((float(child), col))
  88. return new_data
  89. return data
  90.  
  91. def sortby(tree, col, descending):
  92. """sort tree contents when a column header is clicked on"""
  93. # grab values to sort
  94. data = [(tree.set(child, col), child) for child in tree.get_children('')]
  95. # if the data to be sorted is numeric change to float
  96. data = change_numeric(data)
  97. # now sort the data in place
  98. data.sort(reverse=descending)
  99. for ix, item in enumerate(data):
  100. tree.move(item[1], '', ix)
  101. # switch the heading so that it will sort in the opposite direction
  102. tree.heading(col,
  103. command=lambda col=col: sortby(tree, col, int(not descending)))
  104.  
  105.  
  106. # the test data ...
  107. element_header = ['symbol', 'name', 'atomic weight', 'melt (K)', 'boil (K)']
  108. element_list = [
  109. ('H', 'Hydrogen', '1.00794', '13.81', '20.28') ,
  110. ('He', 'Helium', '4.00260', '0.95', '4.216') ,
  111. ('Li', 'Lithium', '6.941', '453.7', '1615') ,
  112. ('Be', 'Beryllium', '9.01218', '1560', '3243') ,
  113. ('B', 'Boron', '10.811', '2365', '4275') ,
  114. ('C', 'Carbon', '12.011', '3825', '5100') ,
  115. ('N', 'Nitrogen', '14.0067', '63.15', '77.344') ,
  116. ('O', 'Oxygen', '15.9994', '54.8', '90.188') ,
  117. ('F', 'Fluorine', '18.99840', '53.65', '85.0')
  118. ]
  119.  
  120. root = tk.Tk()
  121. root.wm_title("ttk.TreeView as multicolumn ListBox")
  122. mc_listbox = McListBox()
  123. root.mainloop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,205
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 130
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Python GUI Programming

 
1
  #26
Jul 19th, 2009
Just bought used Dell XP notebook for class work and installed Python 3.1 and then PyQT4 form:
http://www.riverbankcomputing.co.uk/.../pyqt/download
Windows installer (14JUL2009):
PyQt-Py3.1-gpl-4.5.2-1.exe

I also installed PyScripter IDE from:
http://code.google.com/p/pyscripter/
Windows installer:
PyScripter-v1.9.9.7-Setup.exe

All Python things works charmingly well!

Here is my experiment with the PyQT code:
  1. # simple PyQT window with two buttons and one label
  2. # to test pop-up dialogs for getting name and age
  3. # QInputDialog.getText(parent, title, label, echo=QLineEdit.Normal,
  4. # text=QString(), flags=0)
  5. # QInputDialog.getInt(parent, title, label, value, minValue,
  6. # maxValue, step=1, flags=0)
  7. # http://www.riverbankcomputing.com/static/Docs/PyQt4/html/qinputdialog.html
  8.  
  9. import sys
  10. # use this import option for simplicity
  11. from PyQt4.QtCore import *
  12. from PyQt4.QtGui import *
  13.  
  14. class MyForm(QWidget):
  15. def __init__(self):
  16. QWidget.__init__(self)
  17. # setGeometry(x_pos, y_pos, width, height)
  18. self.setGeometry(100, 150, 320, 100)
  19. self.setWindowTitle("Testing dialog input")
  20.  
  21. self.name = ""
  22. btn_name = QPushButton("Get Name")
  23. self.connect(btn_name, SIGNAL("clicked()"), self.get_name)
  24.  
  25. btn_age = QPushButton("Get Age")
  26. # bind the button click to a function reference
  27. self.connect(btn_age, SIGNAL("clicked()"), self.get_age)
  28.  
  29. self.label = QLabel("------------------")
  30.  
  31. # use grid layout for the widgets
  32. grid = QGridLayout()
  33. # addWidget(widget, row, column, rowSpan=1, columnSpan=1)
  34. grid.addWidget(btn_name, 0, 0)
  35. grid.addWidget(btn_age, 1, 0)
  36. # this will span the label over 3 columns
  37. grid.addWidget(self.label, 2, 0, 1, 3)
  38. self.setLayout(grid)
  39.  
  40. def get_name(self):
  41. self.name, ok = QInputDialog.getText(self,
  42. self.tr("Name"),
  43. self.tr("Enter your name:"))
  44. self.show_result(ok)
  45.  
  46. def get_age(self):
  47. # initial age 0, limit between 1 and 120
  48. self.age, ok = QInputDialog.getInteger(self,
  49. self.tr("Age"),
  50. self.tr("Enter your age:"),
  51. 0, 1, 120)
  52. self.show_result(ok)
  53.  
  54. def show_result(self, ok):
  55. if ok and self.name and str(self.age):
  56. s = "%s you are %s old" % (self.name, self.age)
  57. elif ok and self.name and not str(self.age):
  58. s = self.name
  59. else:
  60. s = str(self.age)
  61. self.label.setText(s)
  62.  
  63.  
  64. app = QApplication(sys.argv)
  65. form = MyForm()
  66. form.show()
  67. app.exec_()
Last edited by bumsfeld; Jul 19th, 2009 at 1:13 pm.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,205
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 130
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Python GUI Programming

 
1
  #27
Jul 19th, 2009
Similar to the PyQT GUI example above, the Thinter GUI toolkit also has input dialogs that validate the input:
  1. # Python GUI toolkit Tkinter has 3 different data input dialogs:
  2. # tkSimpleDialog.askstring(title, prompt [,options])
  3. # tkSimpleDialog.askinteger(title, prompt [,options])
  4. # tkSimpleDialog.askfloat(title, prompt [,options])
  5. # does error trapping with int and float, also optional max/min
  6. # there is also an initialvalue=parameter
  7.  
  8. try:
  9. # for Python2
  10. import Tkinter as tk
  11. import tkSimpleDialog as tksd
  12. except ImportError:
  13. # for Python3
  14. import tkinter as tk
  15. import tkinter.simpledialog as tksd
  16.  
  17.  
  18. root = tk.Tk()
  19.  
  20. # parent=root needed to put dialog window on top of parent root window
  21. mystr = tksd.askstring("Dialog (String)", "Enter your name:", parent=root)
  22. print(mystr)
  23.  
  24. age = tksd.askinteger("Dialog (Integer)", "Enter your age:", parent=root,
  25. minvalue=0, maxvalue=120)
  26. print(age)
  27.  
  28. pay = tksd.askfloat("Dialog (Float)", "Enter your annual pay:",
  29. parent=root, minvalue=1000)
  30. print(pay)
  31.  
  32. root.mainloop()
  33.  
  34. # optional help
  35. help(tksd)
Last edited by bumsfeld; Jul 19th, 2009 at 3:36 pm.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,205
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 130
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Python GUI Programming

 
1
  #28
Jul 20th, 2009
The PyQT GUI toolkit's QLabel widget can actually display HTML formatted text directly. This allows for relatively easy formatting of text:
  1. # PyQT's QLabel widget can display html formatted text
  2.  
  3. import sys
  4. # use this import option for simplicity
  5. from PyQt4.QtCore import *
  6. from PyQt4.QtGui import *
  7.  
  8. class MyForm(QWidget):
  9. def __init__(self, html_text):
  10. QWidget.__init__(self)
  11. # setGeometry(x_pos, y_pos, width, height)
  12. self.setGeometry(100, 150, 320, 100)
  13. self.setWindowTitle("html formatted text")
  14.  
  15. self.label = QLabel(html_text)
  16.  
  17. # use grid layout
  18. grid = QGridLayout()
  19. # addWidget(widget, row, column, rowSpan=1, columnSpan=1)
  20. grid.addWidget(self.label, 0, 0)
  21. self.setLayout(grid)
  22.  
  23. # color is in hex format "#RRGGBB"
  24. html_text = """\
  25. <font face="Times New Roman" size=7 color="#0000FF">
  26. Example of <b>bold</b> html formatted text in blue
  27. </font>
  28. """
  29. app = QApplication(sys.argv)
  30. form = MyForm(html_text)
  31. form.show()
  32. app.exec_()
  33.  
Should you find Irony, you can keep her!
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: 174
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Python GUI Programming

 
1
  #29
Jul 25th, 2009
Since Pygame does not have a file dialog, we can use Tkinter's file dialog and withdrawing the root so it doesn't clash with Pygame's eventloop:
  1. # experiments with module pygame
  2. # free from: http://www.pygame.org/
  3. # load and display an image using pygame and Tkinter's file dialog
  4.  
  5. import pygame as pg
  6.  
  7. # initialize pygame
  8. pg.init()
  9.  
  10. #---------- uses Tkinter to open an image filename ------------------
  11. import Tkinter as tk
  12. import tkFileDialog as tkfd
  13. root = tk.Tk()
  14. root.withdraw()
  15. dirname = tkfd.askdirectory()
  16. mask = [("GIF and JPEG files","*.gif *.jpg")]
  17. image_file = tkfd.askopenfilename(initialdir=dirname, filetypes=mask)
  18. #--------------------------------------------------------------------
  19.  
  20. # RGB color tuple used by pygame
  21. white = (255, 255, 255)
  22.  
  23. # create a 300x300 white screen
  24. screen = pg.display.set_mode((300,300))
  25. screen.fill(white)
  26.  
  27. # load the image from a file
  28. image = pg.image.load(image_file)
  29.  
  30. # draw image, position the image ulc at x=50, y=20
  31. screen.blit(image, (50, 20))
  32.  
  33. # nothing gets displayed until one updates the screen
  34. pg.display.flip()
  35.  
  36. # start event loop and wait until
  37. # the user clicks on the window corner x
  38. while True:
  39. for event in pg.event.get():
  40. if event.type == pg.QUIT:
  41. raise SystemExit
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,205
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 130
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Python GUI Programming

 
1
  #30
Jul 25th, 2009
Looking at PyQT's grid layout manager:
  1. # pqt_gridlayout1.py
  2. # start of small calculator
  3.  
  4. import sys
  5. # simple general import
  6. from PyQt4.QtCore import *
  7. from PyQt4.QtGui import *
  8.  
  9. class GridLayout(QWidget):
  10. def __init__(self, parent=None):
  11. QWidget.__init__(self, parent)
  12. self.setWindowTitle('grid layout')
  13.  
  14. bt_names = ['Cls', 'Bck', '', 'Close',
  15. '7', '8', '9', '/',
  16. '4', '5', '6', '*',
  17. '1', '2', '3', '-',
  18. '0', '.', '=', '+']
  19.  
  20. grid = QGridLayout()
  21.  
  22. pos = [(0, 0), (0, 1), (0, 2), (0, 3),
  23. (1, 0), (1, 1), (1, 2), (1, 3),
  24. (2, 0), (2, 1), (2, 2), (2, 3),
  25. (3, 0), (3, 1), (3, 2), (3, 3 ),
  26. (4, 0), (4, 1), (4, 2), (4, 3)]
  27.  
  28. j = 0
  29. for b in bt_names:
  30. button = QPushButton(b)
  31. if j == 2:
  32. # addWidget(QWidget, row, column, rowSpan, columnSpan)
  33. grid.addWidget(QLabel(''), 0, 2)
  34. else: grid.addWidget(button, pos[j][0], pos[j][1])
  35. j = j + 1
  36.  
  37. self.setLayout(grid)
  38.  
  39.  
  40. app = QApplication(sys.argv)
  41. qb = GridLayout()
  42. qb.show()
  43. sys.exit(app.exec_())
Should you find Irony, you can keep her!
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