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
- Code: Select all
stub_menu.add_command(label=item.get("label", "x_label"), command=lambda self=caller: eval(item.get("command", "x_command"), locals()))
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()
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()
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()
So yeah, i need stuff to do.
