Welcome
Welcome to dinksoftware

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

Bored! And some stuff...

Offline application programming with languages such as C, C++, Java, MatLab, Fortran etc...

Bored! And some stuff...

Postby Holiverh » Wed Jan 06, 2010 3:06 pm

I'm bored i need something to work on, any suggestions welcome, preferably not sockets as i've done quite a bit with them over the past month or two, so anything with a GUI would be good because i want to keep adding stuff to my Tkinter extension module. So far i've got a first draft of a function for generating menu widgets with XML, although it needs a rewrite really because as it's current design predicts it can only got 2 levels deep: ie. File > New > From template. So i need to whack it with some recursion. :)

You'll need to adapt the import of ElementTree to suit you're Python distribution as i have mine in a directory with XTK(The extension module).
Code: Select all
import Tkinter as tk
from elementtree import ElementTree as xml
import os

## Nothing to see here...

def xmlMenu(parent, source, caller):
   
   ## Prepare XML
   if type(source) == str:
      if os.path.isfile(source):
         xml_data = open(source, "rb").read()
      else: xml_data = source
   else: xml_data = source.read()
   
   
   # Base widget   
   widget = tk.Menu(parent)
   menu = xml.XML(xml_data)
   
   
   
   for stub in menu.findall("stub"):
      
      stub_menu = tk.Menu(widget, tearoff=int(stub.get("tearoff", 0)))
      widget.add_cascade(label=stub.get("label", "x_label'<<"), menu=stub_menu)
      
      for item in stub.findall("item"):
         if item.get("spacer", "false") == "false":
            stub_menu.add_command(label=item.get("label", "x_label"), command=lambda self=caller: eval(item.get("command", "x_command"), locals()))
         elif item.get("spacer", "false") == "true":
            stub_menu.add_separator()
            
      for cascade in stub.findall("cascade"):
         stub_menu.add_cascade(label=item.get("label", "x_label"))
            
   return widget
Most proud of:
Code: Select all
stub_menu.add_command(label=item.get("label", "x_label"), command=lambda self=caller: eval(item.get("command", "x_command"), locals()))
Because i spent ages trying to get converting a string to a callable object workable, and i did it with no help --because no help was available. :P All my previous attempts were so close yet so far but eventaully it clicked in my head that i could predefine the argument self as the caller object. The only downside is that the caller as to pass it self as a argument... Well i think it's a downside.

Caller below, you might want to adapt the import of "xtk" if you saved the above as something different. Also make sure this and XTK are in the same directory.
Code: Select all
import Tkinter as tk
import xtk

class UI(object):
   
   def __init__(self, parent, **config):
      self.parent = parent
      
      self.parent.title(config.get("title", None))
      self.parent.geometry(config.get("geometry", None))
      
      self.parent.config(menu=xtk.xmlMenu(self.parent, "example.menu", self))
      
      return None
      
   def hello(self): print "Hi..."
      
MyUI = UI(tk.Tk(), title="XTK test application")
tk.mainloop()
Whilst writing the extension module also building this "UI" class as a template to use at a later date.

XML menu, name it "example.menu" and once again place it in the same directory as the caller.
Code: Select all
<menu>
   <stub label="File" tearoff="1">
      <item label="Has command" command="self.hello()"/>
      <item spacer="true" />
      <cascade label="New">
         <item label="Stuff" />
      </cascade>
   </stub>
   
   <stub label="Edit">
      <item label="" />
      <item label="" />
      <item label="" />
   </stub>
   
   <stub label="View">
      <item label="" />
      <item label="" />
   </stub>
</menu>


Also got a lovely piece of malicious code:
Code: Select all
import sqlite3 as __
import time as t8t8xc
_a=__.connect(":memory:") ; c__caar=_a.cursor() ; c__caar.execute("create table chunk (size, time)") ; ml=_a
for o_oO__ in range(144^12):c__caar.execute("insert into chunk values (?,?)",(o_oO__,t8t8xc.strftime("%x && %X", t8t8xc.localtime())))
   ml.commit()
ml.close()
Doubt it still works after i had to FUBAR it. :lol:

Concurrent drawing!!! Tends to cause "Segmentation Faults".
Code: Select all
#!/usr/bin/env python

import pygame
import sys
from pygame.locals import *
import random
import threading
import time
   
pygame.init()
lock = threading.Lock()

mouse = pygame.mouse
display = pygame.display
event = pygame.event
draw = pygame.draw

screenx = 400
screeny = 400

screen = display.set_mode((screenx, screeny))
screen.fill((0,0,0))

display.set_caption("Threaded drawing")
display.toggle_fullscreen()
   
class frame(threading.Thread):
   def __init__(self, screen, sx, sy, id):
      threading.Thread.__init__(self, name="Draw.Thread.%(id)s" % {"id": id})
      
      random.seed(random.random())
      
      self.screenx = sx
      self.screeny = sy

      self.screen = screen

      self.cpa = (self.screenx/2,self.screeny/2)
      
      self.move = ""
      self.step = 10
      
      self.count = 0
      self.origin = self.cpa
      return None
   def events(self):
      for e in event.get():
         if e.type == QUIT:
            display.toggle_fullscreen()
            pygame.quit()
            sys.exit()
            
         if e.type == KEYDOWN:
            if e.key == K_ESCAPE:
               display.toggle_fullscreen()
               pygame.quit()
               sys.exit()
               
      return None
   def math(self):
      
      options = ["up", "right", "down", "left", "stay"]
      
      self.move = random.choice(options)
      if self.move == "up":
         self.cpa = (self.cpa[0], self.cpa[1]-self.step)
      elif self.move == "down":
         self.cpa = (self.cpa[0], self.cpa[1]+self.step)
         
      elif self.move == "left":
         self.cpa = (self.cpa[0]-self.step, self.cpa[1])
      elif self.move == "right":
         self.cpa = (self.cpa[0]+self.step, self.cpa[1])
         
      elif self.move == "stay":
         pass
      return None
   def render(self, spacing=int):
      
      ## Using a lock only allows one to draw at a time.
      
      #with lock:
      screen.lock()
      
      if self.count > spacing:
         draw.aaline(self.screen, (0,255,255), self.origin, self.cpa)
         
         self.origin = self.cpa
         self.count = 0
      else:
         self.count += 1
         
      # CPA circle
      if self.move == "stay":
         draw.circle(self.screen, (0,255,0), self.cpa, 10, 1)
      else:
         draw.circle(self.screen, (255,0,0), self.cpa, 10, 1)
      
      # Top line
      if self.move == "up":
         draw.line(self.screen, (0,255,0), self.cpa, (self.cpa[0],0))
      else:
         draw.line(self.screen, (255,0,0), self.cpa, (self.cpa[0],0))
      
      # Right line
      if self.move == "right":
         draw.line(self.screen, (0,255,0), self.cpa, (self.screenx,self.cpa[1]))
      else:
         draw.line(self.screen, (255,0,0), self.cpa, (self.screenx,self.cpa[1]))
         
      # Bottem line
      if self.move == "down":
         draw.line(self.screen, (0,255,0), self.cpa, (self.cpa[0],self.screeny))
      else:
         draw.line(self.screen, (255,0,0), self.cpa, (self.cpa[0],self.screeny))
         
      # Left line
      if self.move == "left":
         draw.line(self.screen, (0,255,0), self.cpa, (0,self.cpa[1]))
      else:
         draw.line(self.screen, (255,0,0), self.cpa, (0,self.cpa[1]))
         
      screen.unlock()
      
      display.flip()
      return None
   def reseed(self):
      proceed = random.choice([True,False])
      
      if proceed == True:
         random.seed(random.random())
      else:
         pass
      return None
      
   def run(self):
      while True:
         self.math()
         self.events()
         
         self.render(1)
         self.reseed()
         
      
for id in range(2):
   frame(screen, screenx, screeny, id).start()
Press ESCAPE to... well... escape. :)



So yeah, i need stuff to do. :geek:
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

 

Re: Bored! And some stuff...

Postby Aurel » Thu Nov 25, 2010 5:05 pm

What kind of language is this?
Is this Python?
Everything is contained like UDT....
User avatar
Aurel
Active member
 
Posts: 48
Joined: Thu Nov 25, 2010 4:30 pm

Re: Bored! And some stuff...

Postby Dink » Thu Nov 25, 2010 5:22 pm

Yes, its Python.
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia


Return to Offline application programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron
suspicion-preferred