chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


GUI Regex Tester filter_list
Author
Message
GUI Regex Tester #1
I'm trying to familiarize myself with the Tkinter and ttk libraries, so I built a simple regex tester with them. Cool bonus feature of a button that opens the docpage on regex, too.
Only command line option is a --debug flag to, well, enable debugging.

Tip: use chmod +x regextester.py to run the program as an executeable

Source:
Code:
import re
import ttk
from sys import argv
from Tkinter import *
from webbrowser import open as redir

def popup():
    if debug: print 'DEBUG:\tOpening regex doc in default browser'
    redir('https://docs.python.org/2/library/re.html',new=2,autoraise=True)

class Main():
    def __init__(self):
        #document root
        self.root=Tk()
        self.root.title('Python Regex tester')
        
        #frames
        self.in_frame=ttk.Frame(self.root,padding='3 3 3 3')
        self.in_frame.grid(column=0,row=0,sticky=(N,W,E,S))
        self.in_frame.columnconfigure(0,weight=1)
        self.in_frame.rowconfigure(0,weight=1)
        
        self.out_frame=ttk.Frame(self.root,padding='3 3 3 3')
        self.out_frame.grid(column=0,row=1,sticky=(N,W,E,S))
        self.out_frame.columnconfigure(0,weight=1)
        self.out_frame.rowconfigure(0,weight=1)
        
        #variables
        self.sampleText=StringVar()
        self.pattern=StringVar()
        
        #pattern label
        pattern_label=ttk.Label(self.in_frame,text='regex pattern')
        pattern_label.grid(column=1,row=1,sticky=(E,N,W))
        
        #sample label
        sample_label=ttk.Label(self.in_frame,text='sample input')
        sample_label.grid(column=3,row=1,sticky=(N,W))
        
        #pattern entry
        self.pattern_entry=ttk.Entry(self.in_frame,width=15,textvariable=self.pattern)
        self.pattern_entry.grid(column=1,row=2,sticky=(N,W))
        
        #spacers
        ttk.Label(self.in_frame,text='   ').grid(column=2,row=2)
        
        #sample entry
        sample_entry=ttk.Entry(self.in_frame,width=15,textvariable=self.sampleText)
        sample_entry.grid(column=3,row=2,sticky=(N,W))
        
        #execute button
        ex_button=ttk.Button(self.out_frame,text='test',command=self.test)
        ex_button.grid(column=0,row=1,sticky=(N))
        
        #output entry
        self.output_entry=ttk.Entry(self.out_frame)
        self.output_entry.grid(column=0,row=2,sticky=(N,E,W,S))
        
        #refernce button
        ref_button=ttk.Button(self.out_frame,text='refernce',command=popup)
        ref_button.grid(column=0,row=3,sticky=(E))

        #final operations
        self.pattern_entry.focus()
        self.root.bind('<Return>',self.test)

    #test method
    def test(self,*args):
        try:
            p=re.compile(self.pattern.get())
            r=p.findall(self.sampleText.get())
            
            if debug: print 'DEBUG:\tFound %s for %s in %s'%(r,self.pattern.get(),self.sampleText.get())

            self.output_entry.delete(0,END)
            self.output_entry.insert(0,', '.join(r))
        except ValueError:
            pass
    
    def run(self):
        self.root.mainloop()

debug=False

if len(argv)>1:
    if argv[1]=='--debug':
        debug=True
        print 'DEBUG:\tDebugging enabled'

window=Main()
window.run()

Reply






Users browsing this thread: 1 Guest(s)