neues _tkinter wrapper

Du hast eine Idee für ein Projekt?
Antworten
Costi
User
Beiträge: 544
Registriert: Donnerstag 17. August 2006, 14:21

Sonntag 3. August 2008, 02:06

ein neuer ansatz um tk in python zu benutzen:

Code: Alles auswählen

import _tkinter


class Tk():
	def __init__(self, screenName=None, baseName=None, className='Tk', useTk=1, sync=0, use=None, interactive=0, wantobjects=1):
		if baseName == None:
			import sys, os
			baseName = os.path.basename(sys.argv[0])
			baseName, ext = os.path.splitext(baseName)
			if ext not in ('.py', '.pyc', '.pyo'):
				baseName = baseName + ext
		self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
		self.widgets = {}
	
	def mainloop(self, n=0):
		self.tk.mainloop(n)

	def _call(self, *args, **kw):
		tk_args = []
		for arg in args:
			tk_args.append(self._pyvar2tk(arg))
		for key, val in kw.iteritems():
			tk_args.append("-" + key)
			tk_args.append(self._pyvar2tk(val))
		#print "calling:", tk_args
		return self.tk.call(*tk_args)
	def _pyvar2tk(self, var):
		if callable(var):
			self.tk.createcommand(str(var), var)
			var = '"%s"' % var
		return str(var)
	
	def __getattr__(self, attr):
		"responsable for calling self._do"
		tk_self = self
		if attr.startswith("__"):
			raise AttributeError()
		class Widget:
			def __repr__(self):
				return "." + ".".join(self._attrs)
			def __getattr__(self, attr):
				if attr.startswith("__"):
					raise AttributeError()
				self._attrs.append(attr)
				return widget
			def __call__(self, *args, **kw):
				tk_self._do(self._attrs[:-1], self._attrs.pop(), args, kw)
		widget = Widget()
		widget._attrs = [attr]
		return widget
	
	#def widget_path2widget(self, widget_path):
	#	#print "widget_path2widget called with", widget_path
	#	retval = self
	#	while widget_path != []:
	#		retval = getattr(retval, widget_path.pop(-1))
	#	#print "widget_path2widget will return:", retval
	#	return retval
	
	def _do(self, widget_path, method, args, kw):
		#print "_do got:", widget_id, method, args, kw
		widget_id = "." + ".".join(widget_path)
		
		if method.startswith("create_"):
			widget_type = method[7:]
			return self._call(widget_type, widget_id, *args, **kw)
			
		elif method == "create":
			return self._call(args[0], widget_id, *args[1:], **kw)
			
		elif method in ("pack", "grid", "place", "destroy", "pack_forget", "grid_forget", "place_forget", "bind"):
			return self._call(method, widget_id, *args, **kw)
			
		else:
			return self._call(widget_id, method, *args, **kw)
	

if __name__ == "__main__":
	
	def quit():
		tk.destroy()
	def on_lb_return():
		#does not work
		print tk.lb.curselection()
	

	tk =Tk()
	
	tk.lb.create_listbox()
	lb = tk.lb
	lb.config(selectmode="multiple")
	lb.pack(expand=True, fill="both")
	tk.lb.bind("<Return>", on_lb_return)
	
	for i in range(10):
		lb.insert("end", i)
		
	
	print 'create_frame return value:',  tk.frm.create_frame(bg="red", relief="sunken")
	tk.frm.lbl.create_label(text="i am in a frame")
	tk.frm.pack()
	frm = tk.frm
	frm.lbl.pack()
	
	tk.btn.create_button(text="quit", command=quit)
	print "pack retval", repr(tk.btn.pack())
	
	#compile _tkinter.c against tk8.5
	if _tkinter.TK_VERSION == "8.5":
		tk.pb.create("ttk::progressbar")
		tk.pb.pack()
		tk.pb.step(55)
	
	tk.mainloop()


cp != mv
Antworten