Custom Python functions to help query widgets

  • 2020-04-02 09:28:27
  • OfStack

For example, when learning list, tuple, dict, STR, OS, sys and other modules, the Python documentation can be used to quickly and comprehensively learn those functions. So this built-in documentation feature is a great convenience for scholars, and it's great for short development.

However, when you leave CMD to do "block programming" with IDLE or with software like Komodo Edit, it's a bit of a stretch. For example, the wx library is so large that the help file is 10MB long that if you open it in CMD, you can think about how long it will take you to see the help you want. Once you've become familiar with Python's various apis on a large scale, you'll find that this isn't as handy as you might think. Press any key repeatedly to turn the page, and the page can contain a limited number of pages. So it's very inconvenient.

There are probably two ways around this.

First, use the Module Docs tool

This tool is a method of querying help files that comes with Python GUI IDLE. In the form of web pages, the use of the local function of the machine, to provide a simulation of online a query method. It can open a web page, above can show all the functions, and with a standard classification, more clear, but it is not convenient to use, after all, there is no search function. If you save links to the contents of the page you are looking up, it is not sustainable. So this is a big problem, at the same time, it generates a large web file, memory is too small will put pressure on the system. So this is not a very convenient method to use.

Second, develop your own tools

In fact, I don't want to develop tools by myself, because it takes time after all, a few days or a few weeks to say less, say more, and give their own internal pressure is not small, after all, need a lot of distraction to do this thing. For wx, I found an English document of wxPython API, which was very unclear and vague. It listed functions and specific parameters directly, and how to use them was rarely mentioned. Moreover, various styles of many controls were not listed in detail. So it is very difficult to use, if the name is forgotten, it can also look up the full word and detailed parameter list. Other features are rarely covered at all.

With this in mind, I decided to make my own gadget that would allow me to easily query functions and modules at a low system cost. Only one version is available. Open source code to everyone, code style and control design for beginners to imitate. Master willing to criticize me, I am all ears. Later versions will also be released here, and then it will probably be packaged and then released, and now this is the source file, and as you know, the Python source file can be double-clicked to execute.

 
#coding=utf-8 
# Function description: the software is initially only used for module and function usage of the query, for quick display  
# Extension: you can save the successful result of the query locally,  
#  Save the results after some of the functions are annotated in Chinese  
#  Place the saved keywords in the list on the right  
# Deep extension: use the database to save the results and provide an interface to add, delete, and change  

from Tkinter import * 
from StringIO import StringIO 
from tkSimpleDialog import * 
import sys 
import Pmw 
import ConfigParser 
import os 
import wx 

class Finder(Frame): 

def OnFind(self): 
# Execute and get the result  
info = self.inputStr.get() 
if len(info)==0: 
return True 
buff =StringIO() 
temp = sys.stdout # Save the standard I/O flow  
sys.stdout = buff # The standard I/O The stream redirects to buff object  
self.text.delete(1.0, END) 
try: 
fmt = 'help('+info+')' 
result = eval(fmt) 
self.text.insert(1.0, buff.getvalue()) 
self.savebtn.config(state=NORMAL) 
except: 
try: 
__import__(info) 

fmt = 'help('+self.inputStr.get()+')' 
result = eval(fmt) 
self.text.insert(1.0, buff.getvalue()) 
except: 
self.text.insert(1.0,"ERROR.") 
sys.stdout =temp # Restore the standard I/O flow buff.getvaue() 
self.helpbtn.config(state=NORMAL) 


def save(self): 
# Search, save if not found, use ini File to save data  
# Save the original  
tofind = self.inputStr.get() 
if len(tofind)==0: 
return 
filename='s_'+tofind+'.ini' 
fout = open(filename,'w') 
fout.write(self.text.get(1.0, END).encode('utf-8')) 
fout.close() 

self.items.append(tofind) 
self.items.sort() 
self.config.add_section(tofind) 
self.config.write(open('data.ini', 'r+')) 

nindex = self.items.index(tofind) 
self.box.delete(0,END) 
self.box.insert(0, *self.items) 
self.box.see(nindex) 
self.box.selection_set(nindex) 

self.savebtn.config(state=DISABLED) 

def saveas(self): 
# Save the changes  
index = self.box.curselection() 
if index<0: 
return 
tofind = self.box.get(index) 
if len(tofind)==0: 
return 
strinfo = self.text.get(1.0, END) 

filename='s_'+tofind+'.ini' 
fout = open(filename,'w') 
fout.write(strinfo.encode("UTF-8")) 
fout.close() 

self.saveasbtn.config(state=DISABLED) 

def __init__(self): 
Frame.__init__(self) 
self.option_add('*Font', 'Verdana 12 bold') 
self.pack(expand=YES, fill=BOTH) 
self.master.title(u'Python Function querier ') 
self.master.iconname("calc1") 

# List on the left, placing the saved entries in alphabetical order  
infoF = Frame(self) 
infoF.pack(side=LEFT,expand=NO, fill=BOTH) 

listF = Frame(infoF) 
listF.pack(side=TOP,expand=YES, fill=BOTH) 

# Access to project  
self.config = ConfigParser.ConfigParser() 
self.config.read('data.ini') 
self.items = self.config.sections() 
self.items.sort() 
self.box = Listbox(listF,width=15,selectmode=SINGLE) 
self.box.insert(0, *self.items) 
self.box.bind('<ButtonRelease-1>',self.selectionCommand)# Use the mouse to release the message  
self.box.bind('<ButtonRelease-3>',self.boxrightmenu)# Use the right-click menu to delete the item  

self.PopupMenu=Menu(listF) 
self.PopupMenu.add_command(label=u' delete ',command=self.deleteitem) 
self.PopupMenu.add_command(label=u' rename ',command=self.renameitem) 
self.box.pack(side=LEFT,expand=YES,fill=BOTH) 

self.slbar = Scrollbar(listF, orient=VERTICAL, command=self.box.yview) 
self.slbar.pack(side=RIGHT, expand=NO, fill=BOTH) 
self.box.configure(yscrollcommand=self.slbar.set) 

btnf = Frame(infoF) 
btnf.pack(side=BOTTOM, fill=BOTH) 
self.savebtn = Button(btnf, text=u' Save the new ',state=DISABLED, command=self.save) 
self.savebtn.pack(side=LEFT, expand=YES, fill=BOTH) 
self.saveasbtn = Button(btnf, text=u' Save the changes ',state=DISABLED, command=self.saveas) 
self.saveasbtn.pack(side=RIGHT, expand=YES, fill=BOTH) 


# Includes list information and display information  
twoF = Frame(self) 
twoF.pack(side=BOTTOM, expand=YES, fill=BOTH) 

# Display information, scroll bar  
showF = Frame(twoF, relief=SUNKEN) 
self.text = Text(showF,height=25, width =65) 
self.text.insert(1.0,'information...') 
self.text.pack(side=LEFT, expand=YES, fill=BOTH) 
self.text.bind("<Key>", self.modify) 
self.text.bind("<Double-Button-1>", self.tomodify) 
self.ismodified = False 
showF.pack(side=TOP,expand=YES, fill=BOTH) 

self.scrollbar = Scrollbar(showF, orient=VERTICAL, command=self.text.yview) 
self.scrollbar.pack(side=RIGHT, expand=NO, fill=BOTH) 
self.text.configure(yscrollcommand=self.scrollbar.set) 

# Provides input interface, and functions such as: find  
inputF = Frame(twoF) 
inputF.pack(side=BOTTOM, fill=BOTH) 
self.inputStr = StringVar() 
self.inputStr.set('') 
self.info = StringVar() 
self.info.set('infomation...') 
self.entry = Entry(inputF, relief=SUNKEN, textvariable=self.inputStr) 
self.entry.bind("<Return>", self.inputreturn) 
self.entry.pack(side=LEFT, expand=YES, fill=BOTH) 

self.findbtn = Button(inputF,text=u' To find the ',command=self.OnFind) 
self.findbtn.pack(side=LEFT, expand=YES, fill=BOTH) 
self.helpbtn = Button(inputF,text=u' help ',command=self.OnHelp) 
self.helpbtn.pack(expand=NO, fill=Y) 

def OnHelp(self): 
fp = open('readme.txt') 
buff = fp.read() 
fp.close() 

self.text.delete(1.0, END) 
self.text.insert(1.0, buff) 
self.helpbtn.config(state=DISABLED) 

def deleteitem(self): 
# Right-click menu ,  Delete function  
sels = self.box.curselection() 
if len(sels) == 0: 
pass #print 'no selection' 
else: 
sec = self.items[int(sels[0])] 
self.config.remove_section(sec) 
self.config.write(open('data.ini', 'w')) 
self.box.delete(sels[0]) 
# self.items.remove(sels[0]) # Is the reference effect  
self.text.delete(1.0, END) 
self.text.insert(1.0,'delete success.') 


def renameitem(self,event=None,en=None): 
# The mail menu ,  Rename function  
retval = askstring("input", 
"input the new name:") 
if len(retval)==0: 
return 
sels = self.box.curselection() 
if len(sels) == 0: 
pass #print 'no selection' 
else: 
# An array of / table / The configuration file  
sec = self.items[int(sels[0])] 

self.box.delete(0, END) 
self.items[int(sels[0])] = retval # An array of  
self.items.sort() 
self.box.insert(0, *self.items) # table  

self.config.remove_section(sec) 
self.config.add_section(retval) 
self.config.write(open('data.ini', 'w')) # The configuration file  

self.text.delete(1.0, END) 
self.text.insert(1.0,'rename success.') 


def boxrightmenu(self,event=None,en=None): 
# Right click menu  
self.PopupMenu.tk_popup(*self.winfo_pointerxy()) 

def tomodify(self,event=None,en=None): 
if self.ismodified==True: 
self.saveasbtn.config(state=DISABLED) 
self.ismodified = False 
else: 
self.saveasbtn.config(state=NORMAL) 
self.ismodified = True 
return True 

def modify(self,event=None,en=None): 
self.saveasbtn.config(state=NORMAL) 
return True 

def inputreturn(self,event=None,en=None): 
self.OnFind() 
return True 

def selectionCommand(self,event=None,en=None): 
#  When I select the list ,  Display details  
sels = self.box.curselection() 
if len(sels) == 0: 
pass 
else: 
filename='s_'+self.box.get(sels[0])+'.ini' 
fp = open(filename) 
strinfo = fp.read() 
fp.close() 
self.text.delete(1.0, END) 
self.text.insert(1.0,strinfo) 
self.helpbtn.config(state=NORMAL) 

if __name__ == '__main__': 
Finder().mainloop() 

Note, the source code file if you want to execute correctly, please create your own readme.txt file, and the data.ini file. Otherwise there will be an error because the two files cannot be opened. Why not use exception handling? I haven't perfected it yet. This version is now available, so try it out.
Want to complete all of the files, please click: (link: http://xiazai.jb51.net/201103/tools/PythonfunFinderTool.rar) to download.
[note] : after opening the software, please enter list, tuple, dict and other types to query, click new save to save to the list, for the next time quickly open.
Some packages are not included in the source code, such as codecs. At this time, you need to manually add this package to the beginning of the source file to be able to query, such as import codecs.
If you modify the text in the display box, please click save to save, so that you can open the next time to see the effect of your modification.
This version is not perfect, and the function is limited, itself is based on Python, so its value is still as a Python development application.
Please leave a message if you have any comments.
The 2011-03-10 12:15:05
Some netizens have not been able to correctly execute the source code. After thinking about it, I think it should have nothing to do with the version, these are very basic functions and usage. If you can't, try to install Pmw and wx correctly.
1. Installation method of Pmw:
Download: pmw.1.3.2. tar.gz. This thing is easy to find, please check it. After downloading, unzip it in place, find the "Pmw" directory, copy the directory to your Pyhton installation directory, no need for other specific directory. Just C:\Python27\, other Python versions please adjust accordingly.
2. Installation method of wx:
Download: wxpython 2.8-win32-unicode-2.8.11.0-py27.exe. This thing please baidu, estimated to the official English website of wxPython download best, a lot of Chinese website also provided download, so it is not difficult to find. Double-click to install the default mode, you do not have to change the directory or other. It will automatically install to your installation directory, my directory is: C:\Python27\Lib\site-packages\wx-2.8-msw-unicode.
3. The Tkinter library has been integrated with the system, so there is no need to install it. Mainly is the above two libraries can.
4. How to check whether the installation is correct?
After installation, enter import wx or import Pmw into Python's COMMAND LINE to see if it is correct. No prompt message is correct. You can also take a closer look at the information in the package by typing dir (wx) or dir (Pmw). The help () function is not recommended. As I said above, wx has up to 10MB of information, which you will never be able to display.

Related articles: