3 beginner questions!

Rune Hansen rune.hansen at viventus.no
Wed May 7 10:20:43 EDT 2003


Ali Dada wrote:
> hi all:
> 
> first question (PyQt): 
> i am using the Qt designer that comes with RedHat9 to design a simple .ui form,
> then i am saying:
> 
> $ pyuic form.ui > form.py
> 
> which successfully generates python code which includes a class definition that
> is supposed to generate the form. i am adding a line to the code to create an
> instance of the class so that i get a window or form or anything useful. when i
> execute the modified code i get the following line on stdout:
>  
> QPaintDevice: Must construct a QApplication before a QPaintDevice

First of all you should use pyuic like this:
$ pyuic -x -o form.py form.ui
$ python form.py - should start your GUI. If not, you may have a pyuic 
version with the "image" bug or an otherwise defective PyQt instalation.
Secondly, as Alex pointed out, use form.py by inheritance since it's 
more than likely you'll want to edit form.ui in designer at some point. 
If the problem persists after updating PyQt you'll have to post some code.

> 
> 
> 
> second question (Tkinter and Pmw)
> 
> i wrote the file below, called ScrolledListBox.py, and it works
> fine when run directly from the terminal (ie it brings up a ScrolledListBox with
> three entries so that when you select any of them it performs it function
> properly, writing 'hi' to stdout)
> 
> 
> #!/usr/bin/env python
> from Tkinter import *
> import Pmw
> 
> class ImageSelection(Frame ):
>   
>  def __init__( self):
>   root = Tk()
>   Frame.__init__( self )
>   Pmw.initialise()
>   self.pack( expand = YES, fill=BOTH)
>   self.suites=['a', 'b', 'c']
>   self.listBox = Pmw.ScrolledListBox( root, items = self.suites, listbox_height
> = 5, vscrollmode = "static", selectioncommand  = self.switchSuite)
>   self.listBox.pack( side = LEFT, expand = YES, fill= BOTH, padx = 5, pady=5)
>   
>  def switchSuite(self):
>   print 'hi'
>   
> def main():
>  ImageSelection().mainloop()
>  
>  
> if __name__ == "__main__":
>  ImageSelection().mainloop()
> 
> 
> now the problem comes. i need to call the  above from a new file called
> Index.py, shown below. Index.py imports ScrolledListBox and makes a Button that
> calls ScrolledListBox. surprisingly (for me at least) the three entries in the
> ListBox don't perform as expected. 
> 
> 
> 
> #!/usr/bin/env python
> from Tkinter import *
> import ScrolledListBox
> 
> class Index(Frame):
>  def __init__(self, parent=None):
>   
>   Frame.__init__(self, parent)
>   Button(self, text='Regression', command=ScrolledListBox.main).pack(side=TOP,
> fill=BOTH)
>   
>  
> if __name__ == '__main__':
>  i = Index()
>  i.pack()
>  i.mainloop()
> 
Yet again a question of inheritance. Personally I would have written it 
along these lines:
--
--ScrolledListBox.py
#!/usr/bin/env python
from Tkinter import *
import Pmw

class ImageSelection(Frame ):
     def __init__( self):
         Frame.__init__( self )
         root = Tk()
         Pmw.initialise()

         self.pack( expand = YES, fill=BOTH)
         self.suites=['a', 'b', 'c']
         self.listBox = Pmw.ScrolledListBox( root, items = self.suites, 
listbox_height= 5, vscrollmode = "static", selectioncommand  = 
self.switchSuite)
         self.listBox.pack( side = LEFT, expand = YES, fill= BOTH, padx 
= 5, pady=5)

     def switchSuite(self):
         print 'hi

--Start.py
#!/usr/bin/env python
from Tkinter import *
from ScrolledListBox import ImageSelection

class ImageSel(ImageSelection):
      def __init__(self):
          ImageSelection.__init__(self)

class Index(Frame):
     def __init__(self, parent=None):
         Frame.__init__(self, parent)
         Button(self, text='Regression', 
command=self.buttonOpensImageSelectionWindow).pack(side=TOP,fill=BOTH)

     def buttonOpensImageSelectionWindow(self):
         _w = ImageSel()
         _w.mainloop()

if __name__ == '__main__':
  i = Index()
  i.pack()
  i.mainloop()

Not necessarily the best or most correct way, but it should at least get 
you facing in the right general direction.
> 
> 
> third question (Tkinter):
> 
> i am programming a menu bar that should appear alone in the window and should
> span the width of the screen and should be displayed at the top of the screen.
> any idea how this should be implemented? if i am not expressing myself properly,
> i pasted my trial below.
> 
> 
> #!/usr/bin/env python
> 
> from Tkinter import *                              # get widget classes
> from tkMessageBox import * 
> import sys
> import OpenWorkArea
>                         # get standard dialogs
> class CMmenu(Frame):
> 	def __init__(self,parent=None) :
> 		Frame.__init__(self, parent)
> 		self.pack()
> 		widget = Button(self, text='Code Manage', command= self.makemenu)
> 		widget.pack(side=LEFT)
>  
> 
> 	def makemenu(self):
> 		root = Tk()                                       
> 		root.title('TAGZ -- Code Manage')
> 		top = Menu(root)                                # win=top-level window
> 		root.config(menu=top) 
> 		WorkArea = Menu(top, tearoff=0)
> 		WorkArea.add_command(label='Open...',  command =
> OpenWorkArea.WorkAreaSelection,  underline=0)
> 		WorkArea.add_separator()
> 		WorkArea.add_command(label='Close',    command=root.quit, underline=0)
> 		WorkArea.add_command(label='Release...',  command=self.notdone,  underline=0)
> 		WorkArea.add_separator()
> 		WorkArea.add_command(label='Commit Files...',  command=self.notdone,  underline=0)
> 		WorkArea.add_command(label='Update Files...',  command=self.notdone,  underline=0)
> 		WorkArea.add_command(label='Restore Version...',  command=self.notdone, 
> underline=0)
> 		WorkArea.add_command(label='Tag Files...',  command=self.notdone,  underline=0)
> 		WorkArea.add_separator()
> 		WorkArea.add_command(label='Refresh',  command=self.notdone,  underline=0)
> 		top.add_cascade(label='Work Area',     menu=WorkArea,        underline=0)
> 
> 		file = Menu(top, tearoff=0)
> 		file.add_command(label='Open',  command=self.notdone,  underline=0)
> 		file.add_separator()
> 		file.add_command(label='Commit...', command=self.notdone,  underline=0)
> 		file.add_command(label='Update',    command=self.notdone,  underline=0)
> 		file.add_command(label='Restore Version...',    command=self.notdone, 
> underline=0)
> 		file.add_command(label='Tag...',    command=self.notdone,  underline=0)
> 		file.add_command(label='Open Differences',    command=self.notdone,  underline=0)
> 		file.add_separator()
> 		file.add_command(label='Delete and Update...',    command=self.notdone, 
> underline=0)
> 		file.add_separator()
> 		file.add_command(label='Add to Work Area',    command=self.notdone,  underline=0)
> 		file.add_command(label='Add to Work Area as Binary',    command=self.notdone,
>  underline=0)
> 		file.add_command(label='Remove from Work Area',    command=self.notdone, 
> underline=0)
> 		file.add_separator()
> 		file.add_command(label='Refresh',    command=self.notdone,  underline=0)
> 		top.add_cascade(label='File',     menu=file,        underline=0)
> 
> 		edit = Menu(top, tearoff=0)
> 		edit.add_command(label='Undo',     command=self.notdone,  underline=0)
> 		edit.add_command(label='Redo',   command=self.notdone,  underline=0)
> 		edit.add_separator()
> 		edit.add_command(label='Cut',     command=self.notdone,  underline=0)
> 		edit.add_command(label='Copy',   command=self.notdone,  underline=0)
> 		edit.add_command(label='Paste',     command=self.notdone,  underline=0)
> 		edit.add_command(label='Delete',   command=self.notdone,  underline=0)
> 		edit.add_command(label='Select All',     command=self.notdone,  underline=0)
> 		edit.add_separator()
> 		top.add_cascade(label='Edit',     menu=edit,        underline=0)
> 
> 		find = Menu(edit, tearoff=0)
> 		find.add_command(label='Find...', command=self.notdone,  underline=0)
> 		find.add_command(label='Find Next', command=self.notdone,  underline=0)
> 		find.add_command(label='Find Previous', command=self.notdone,  underline=0)
> 		find.add_command(label='Use Selection for Find', command=self.notdone, 
> underline=0)
> 		find.add_command(label='Jump to Selection', command=self.notdone,  underline=0)
> 		edit.add_cascade(label='Find',   menu=find,     underline=0)
> 		tools = Menu(top, tearoff=0)
> 		inspector = Menu(tools, tearoff=0)
> 		inspector.add_command(label='Status', command=self.notdone,  underline=0)
> 		inspector.add_command(label='Logs', command=self.notdone,  underline=0)
> 		inspector.add_command(label='Differences', command=self.notdone,  underline=0)
> 		inspector.add_command(label='Tags', command=self.notdone,  underline=0)
> 		inspector.add_command(label='Contents', command=self.notdone,  underline=0)
> 		tools.add_cascade(label='Inspector',   menu=inspector,     underline=0)
> 		repositories = Menu(tools, tearoff=0)
> 		repositories.add_command(label='??', command=self.notdone,  underline=0)
> 		repositories.add_command(label='???', command=self.notdone,  underline=0)
> 		repositories.add_command(label='????', command=self.notdone,  underline=0)
> 		repositories.add_command(label='?????', command=self.notdone,  underline=0)
> 		repositories.add_command(label='??????', command=self.notdone,  underline=0)
> 		tools.add_cascade(label='Repositories',   menu=repositories,     underline=0)
> 		tools.add_command(label='Processes', command=self.notdone,  underline=0)
> 		tools.add_command(label='Comparisons...', command=self.notdone,  underline=0)
> 		console = Menu(tools, tearoff=0)
> 		console.add_command(label='??', command=self.notdone,  underline=0)
> 		console.add_command(label='???', command=self.notdone,  underline=0)
> 		console.add_command(label='????', command=self.notdone,  underline=0)
> 		console.add_command(label='?????', command=self.notdone,  underline=0)
> 		console.add_command(label='??????', command=self.notdone,  underline=0)
> 		tools.add_cascade(label='Console',   menu=console,     underline=0)
> 		top.add_cascade(label='Tools',     menu=tools,        underline=0)
> 
> 		window= Menu(top, tearoff=0)
> 		window.add_command(label='Close Window',    command=root.quit, underline=0)
> 		top.add_cascade(label='Window',     menu=window,        underline=0)
> 
> 		help= Menu(top, tearoff=0)
> 		help.add_command(label='About...',  command=self.notdone,  underline=0)
> 		help.add_command(label='TAGZ Manual',   command=self.notdone,  underline=0)
> 		top.add_cascade(label='Help',     menu=help,        underline=0)
> 	def notdone(self):  
> 		showerror('Not implemented', 'Not yet available')
> if __name__ == '__main__':
>                               
>     #makemenu(root)                                     
>     CMmenu().mainloop()
> 

/rune





More information about the Python-list mailing list