Week #4 Challenge: Python

  • (8 Pages)
  • +
  • « First
  • 6
  • 7
  • 8

108 Replies - 19276 Views - Last Post: 06 September 2011 - 12:05 PM

#106 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: Week #4 Challenge: Python

Posted 11 March 2010 - 06:22 AM

If it isn't to late, I would like to enter this week (even though its over). I'm gonna post my text-editor called PyPad.
#!/usr/bin/env python
import sys
import pygtk
import gtk
TextBox = gtk.TextView()
MenuBar = gtk.MenuBar()
StatusBar = gtk.Statusbar()
Window = gtk.Window(gtk.WINDOW_TOPLEVEL)
class MainForm:
    _File = ""
    def open_file(menuitem, user_param1):
	chooser = gtk.FileChooserDialog(title="Open a file",action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
        chooser.set_default_response(gtk.RESPONSE_OK)
	filter = gtk.FileFilter()
        filter.set_name("Text Files")
        filter.add_mime_type("text/data")
        filter.add_pattern("*.txt")
        chooser.add_filter(filter)
	filter2 = gtk.FileFilter()
	filter2.set_name("All Files")
	filter2.add_pattern("*.*")
	chooser.add_filter(filter2)
	response = chooser.run()
	if response == gtk.RESPONSE_OK:
	    global _File
	    filename = chooser.get_filename()
	    _File = filename
	    textbuffer = TextBox.get_buffer()
	    print "Opened File: " + filename
	    StatusBar.push(0,"Opened File: " + filename)
	    index = filename.replace("\\","/").rfind("/") + 1
	    window.set_title(filename[index:] + " - PyPad")
	    file = open(filename, "r")
	    text = file.read()
	    textbuffer.set_text(text)
	    file.close()
	elif response == gtk.RESPONSE_CANCEL:
	    chooser.destroy()
        chooser.destroy()
    def save_file_as(menuitem,user_param):
	chooser = gtk.FileChooserDialog(title="Save file",action=gtk.FILE_CHOOSER_ACTION_SAVE, buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK))
        chooser.set_default_response(gtk.RESPONSE_OK)
	filter = gtk.FileFilter()
        filter.set_name("Text Files")
        filter.add_mime_type("text/data")
        filter.add_pattern("*.txt")
        chooser.add_filter(filter)
	filter2 = gtk.FileFilter()
	filter2.set_name("All Files")
	filter2.add_pattern("*.*")
	chooser.add_filter(filter2)
	response = chooser.run()
	if response == gtk.RESPONSE_OK:
	    global _File
	    filename = chooser.get_filename()
	    _File = filename
	    textbuffer = TextBox.get_buffer()
	    print "Saved File: " + filename
            StatusBar.push(0,"Saved File: " + filename)
	    index = filename.replace("\\","/").rfind("/") + 1
	    text = textbuffer.get_text(textbuffer.get_start_iter() , textbuffer.get_end_iter())
	    window.set_title(filename[index:] + " - PyPad")
	    file = open(filename, "w")
	    file.write(text)
	    file.close()
	elif response == gtk.RESPONSE_CANCEL:
	    chooser.destroy()
        chooser.destroy()
    def reset_new(menuitem,user_param):
        StatusBar.push(0,"Ready")
	_File = ""
        print "PyPad has been reset!"
	window.set_title("Untitled - PyPad")
	textbuffer = TextBox.get_buffer()
	textbuffer.set_text("")
    def save_file(menuitem,user_param):
	if _File is not "":
	    textbuffer = TextBox.get_buffer()
	    print "Saved File: " + _File
            StatusBar.push(0,"Saved File: " + filename)
	    index = filename.replace("\\","/").rfind("/") + 1
	    text = textbuffer.get_text(textbuffer.get_start_iter() , textbuffer.get_end_iter())
	    window.set_title(_File[index:] + " - PyPad")
	    file = open(_File, "r+")
	    file.write(text)
	    file.close()
    def __init__(self):
        window.set_title("Untitled - PyPad")
	window.set_default_size(750,450)
	window.connect('destroy', gtk.main_quit)
        TextBox.set_wrap_mode(gtk.WRAP_WORD)
        TextBox.set_editable(True)
        TextBox.set_cursor_visible(True)	
        TextBox.set_border_window_size(gtk.TEXT_WINDOW_LEFT,1)
        TextBox.set_border_window_size(gtk.TEXT_WINDOW_RIGHT,1)
        TextBox.set_border_window_size(gtk.TEXT_WINDOW_TOP,1)
        TextBox.set_border_window_size(gtk.TEXT_WINDOW_BOTTOM,1)
	vbox = gtk.VBox(False,0)
	box1 = gtk.VBox(False, 10)
	box2 = gtk.VBox(False, 20)
	box3 = gtk.VBox(False, 70)
	vbox.pack_start(box2, False, False, 0)
	vbox.pack_start(box1, True, True, 0)
	vbox.pack_start(box3, False, False, 0)
	sw = gtk.ScrolledWindow()
	sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
	sw.add(TextBox)
	box1.pack_start(sw)
	box2.pack_start(MenuBar, True, False, 0)
	box3.pack_start(StatusBar, True, False, 0)
	filemenu = gtk.Menu()
        filem = gtk.MenuItem("File")
	newm = gtk.MenuItem("New")
	openm = gtk.MenuItem("Open")
	savem = gtk.MenuItem("Save")
	saveasm = gtk.MenuItem("Save As")
	openm.connect("activate",self.open_file)
	saveasm.connect("activate",self.save_file_as)
	newm.connect("activate",self.reset_new)
	savem.connect("activate",self.save_file)
	filemenu.append(newm)
	filemenu.append(openm)
	filemenu.append(savem)
	filemenu.append(saveasm)
        filem.set_submenu(filemenu)
	MenuBar.append(filem)
        StatusBar.push(0,"Ready")
	window.add(vbox)
	window.show_all()
def main():
    gtk.gdk.threads_enter()
    gtk.main()
    gtk.gdk.threads_leave() 
    
if __name__ == "__main__":
    Initialize = MainForm()
    main()

Was This Post Helpful? 0
  • +
  • -

#107 Whitellama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-May 10

Re: Week #4 Challenge: Python

Posted 25 June 2010 - 06:00 PM

This simulates dice. You can choose the number of sides on the die and the program generates a random number. Here it is:

print "Welcome to the Python Dice Simulator"
sides = raw_input("How many sides would you like on your die? ")
sides = int(sides)
import random
print "You rolled", random.randint(1, sides)


Was This Post Helpful? 0
  • +
  • -

#108 Blackllama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-June 10

Re: Week #4 Challenge: Python

Posted 26 June 2010 - 10:12 AM

This is really basic. It checks if they have enough slurpees XD.

Banana = 0
Coke = 1
Mystery = 2
Cherry = 0

if Banana or Coke or Mystery or Cherry > 0 
print 'Yah, we got Slurpees!'
else print 'Nope.  All out, sorry.'


Was This Post Helpful? 0
  • +
  • -

#109 lionaneesh  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-April 10

Re: Week #4 Challenge: Python

Posted 06 September 2011 - 12:05 PM

More than a year , Since i used Python! :D , Nice challenge will try and complete it!
Was This Post Helpful? 0
  • +
  • -

  • (8 Pages)
  • +
  • « First
  • 6
  • 7
  • 8