3 Replies - 1251 Views - Last Post: 29 August 2011 - 05:04 PM Rate Topic: -----

#1 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

GUI in Python

Posted 09 March 2011 - 10:59 AM

Hey every one. I am taking a Human Computer Interfaces class and we have to create a GUI to test people episodic
memory. Soo essentialy the program is going to consist of 3 major pages, a start page, 10 question pages, and a finish
page. What im asking is if any one know of a good tutorial to do this GUI in Python?

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: GUI in Python

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: GUI in Python

Posted 09 March 2011 - 11:03 AM

You should consider any of the following:

Was This Post Helpful? 0
  • +
  • -

#3 Vblaster  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 16-January 11

Re: GUI in Python

Posted 25 August 2011 - 09:38 PM

Using easygui: http://www.ferg.org/...i/tutorial.html
Was This Post Helpful? 0
  • +
  • -

#4 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: GUI in Python

Posted 29 August 2011 - 05:04 PM

Not sure if this will be helpful to you, or not, but it is something I'm whipping up for work to avoid using Agent Ransack for everything.

since I'm a newb to both Tkinter AND threading, you get a nice blatant use of some of their features.

This tool lets you look for files either by regular expressions matching file names, regular expressions matching the contents, or both.

It lets you do this in (so far) up to 6 directories at once, with one thread per directory.

The textbox fills with the absolute path, the time it took to find it, and the contents of the file. (because in the work I do, copying/pasting data from files is pretty common place, and so is editing them) In addition to that, found-files get recorded in a string that will launch an external editor with the file paths as arguments.

You can clear the textbox, scroll through it, edit it, or copy/paste from it.



#!/usr/bin/python

import os
import sys
import re
import time
import math
from Tkinter import *
import threading
       

class MainUI(object):
    class ThreadContainer(object):
        def __init__(self):
            thread1 = 0
            thread2 = 0
            thread3 = 0
            thread4 = 0
            thread5 = 0
            thread6 = 0
            self.threads = [thread1, thread2, thread3, thread4, thread5, thread6]
            
        
    stop = False
    filename_list = []


    def push_message(self, message):
        self.results_box.insert(END,message)
            
            
    def file_accessor(self, directory, file_pattern = re.compile("[a-zA-Z0-9-_.]*"), content_pattern = re.compile("\S*")):
        start_time = time.clock()
        assert directory is not str, "Directory is " + str(type(directory))
        for filename in os.listdir(directory):
            if(self.stop):
                print "Stopped process!"
                exit()
                
            if file_pattern.search(filename):
                if(os.path.isfile(directory+os.path.sep+filename)):
                    file_path = (directory + os.path.sep + filename)
                    file_contents = re.sub("\n","",open(file_path, 'r').read())
                    if(content_pattern.search(file_contents)):
                        elapsed_time = time.clock() - start_time
                        message = "".join("FOUND FILE IN: %.4f seconds" % elapsed_time + "\n")
                        message +=  directory+ os.path.sep + filename + "\n\n"
                        message +=  file_contents + "\n"
                        message += '-'*100
                        message += "\n\n\n\n"
                        self.push_message(message)
                        self.filename_list.append(directory + os.path.sep + filename)
                    else:
                        pass
            else:
                pass
            
    def __init__(self):
        self.master = Tk()
        
        
        self.filename_label = Label(self.master, text="Filename:")
        self.filename_label.grid(row=0, column=0)
        self.filename_entry = Entry(self.master, width=50)
        self.filename_entry.grid(row=0, column=1, stick=W+E)
        
        
        self.contents_label = Label(self.master, text="Contents:")
        self.contents_label.grid(row = 1, column = 0)     
        self.contents_entry = Entry(self.master, width=50)
        self.contents_entry.grid(row=1, column=1, stick=W+E)
        
        
        self.directory_label = Label(self.master, text="Directories:")
        self.directory_label.grid(row = 2, column = 0)
        self.directory_entry = Entry(self.master, width=50)
        self.directory_entry.grid(row=2, column=1, stick=W+E)
        
        
        self.search_button = Button(self.master, text="Search", width=10, command=self.search_callback)
        self.search_button.grid(row=0, column=5)
        
        self.stop_button = Button(self.master, text="Stop", width=10, command=self.stop_search)
        self.stop_button.grid(row=1, column=5)
        
        self.clear_button = Button(self.master, text="Clear", width=10, command=self.clear_textbox)
        self.clear_button.grid(row=2, column=5)
        
        self.nppp_open_button = Button(self.master, text="Edit", width=10, command=self.open_file_in_nppp)
        self.nppp_open_button.grid(row = 3, column = 5)
        
        self.results_box = Text(self.master, width = 100)
        self.results_box.grid(row=6, column=1)
        self.results_scrollbar = Scrollbar()
        scrollBar = Scrollbar(self.master)
        scrollBar.grid(row=6, column=2, sticky=N+S)
        scrollBar.config(command=self.results_box.yview)
        
        self.results_box.config(yscrollcommand=scrollBar.set)
        
        
        mainloop()
        
        
    def search_callback(self):
        tc = self.ThreadContainer()
        self.stop = False
        self.filenames = re.compile(self.filename_entry.get())
        self.contents = re.compile(self.contents_entry.get())
        self.directories = [entry.strip() for entry in self.directory_entry.get().split(",")]
        print self.directories
        for thread_no in xrange(len(self.directories)):
            tc.threads[thread_no] = threading.Thread(target=self.file_accessor, args=[self.directories[thread_no], self.filenames, self.contents])
            tc.threads[thread_no].start()
                 
    def stop_search(self):
        print "Stopping process!"
        self.stop = True
        
    def clear_textbox(self):
        self.filename_list = []
        self.results_box.delete("%d.%d" % (1, 0), END)
    
    def open_file_in_nppp(self):
        print "C:\\program files\\notepad++\\notepad++.exe " + "".join([filename + "," for filename in self.filename_list]).rstrip(",")
        os.system("\"C:\\program files\\notepad++\\notepad++.exe\" " + "".join([filename + "," for filename in self.filename_list]).rstrip(","))
                
            
MainUI()
    



This post has been edited by Python_4_President: 29 August 2011 - 05:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1