Create your own Python IDE development tool with Tkinter and use HP_tka module designs its own Chinese code editor

Posted by cornick on Tue, 01 Feb 2022 15:24:30 +0100

Create your own Python IDE development tool with Tkinter (5) using HP_tka module designs its own Chinese code editor
Earlier, we introduced the method of using exec() function to run user programs in Tkinter. Exec () uses Python code for multithreading, which is easy to crash the editor program. In addition, if the user also develops Tkinter application, it will have an impact on the editor software developed with Tkinter. Therefore, the previously adopted external running Python code mode.
We designed a batch file [run_py.bat], which is as follows.

@python.exe -i %1 %2  %3 %4

We design the corresponding external operation button, and the main code is as follows.

    def runuc2(self):
        try:
            f = open('temppy.py','w',encoding='utf-8',errors='ignore')
            msg = self.textPad.get(1.0,tk.END)
            f.write(msg)
            f.close()
            runpy='run_py.bat temppy.py'
            os.system(runpy)
        except Exception as e:
            pass

The previous operation mode described above.
During the recent Spring Festival holiday, take advantage of leisure to HP_tk.py and HP_tk2.py module is improved and several new Tkinter controls are designed. For example, the new control allows user code to run user programs in a multi process manner. Bugs in user programs no longer affect the operation of editor programs.
Our newly designed myide Py all source code is as follows, with detailed comments. [running module] is multi-threaded running, and [running program] is multi process running.

# -*- coding: utf-8 -*-
import  tkinter  as  tk   #Import Tkinter
import  tkinter.ttk  as  ttk   #Import Tkinter ttk
import HP_global as g   #Establish global data field g
import  HP_tka  as  htk   #Import advanced htk
from tkinter.messagebox import *
from tkinter.filedialog import *
import PIL
"""
#Function: Python little white code editor
#Version: ver1 00
#Designer: single wolf Hepu
#Tel: 18578755056
#QQ: 2775205/2886002
#Xiaobai quantitative Chinese Python Tkinter group: 983815766
#Baidu: Hepu index, Xiaobai quantification
#Design start date: January 30, 2022
#Last revision date: January 30, 2022
#Main program: myide py
"""
mytitle='Xiaobai Chinese Python Program editor'
#Create main window
root=htk.MainWindow(title=mytitle,x=100,y=200,w=1200, h=700)
root.iconbitmap('ico/cp64.ico')  #Set application icon
root.SetCenter()  #Move to the center of the screen
##Get window information
screenwidth = root.winfo_screenwidth()  #Gets the screen width in pixels
screenheight = root.winfo_screenheight()  #Gets the screen height in pixels
winw= root.winfo_width()   #Gets the window width in pixels
winh = root.winfo_height()  #Gets the window height in pixels

#Create window menu
mainmenu=htk.windowMenu(root) #Create a window menu and use the default menu

#Create window toolbar
toolsbar=htk.ToolsBar(root,5,bg='#1E488F') #Create toolbar
toolsbar.pack(side=tk.TOP, fill=tk.X)   #Place the toolbar at the top of the window
toolsbar.demo()  #Toolbar change picture presentation

#The split window is divided into left and right parts
#Create divisible area paned
paned= tk.PanedWindow(root,orient=tk.HORIZONTAL,showhandle=True, \
                    sashrelief=tk.SUNKEN,sashwidth=1)  #The default is left-right distribution
paned.pack(fill=tk.BOTH, expand=1) #Put it in the main window and zoom up, down, left and right

#Select directory
def selectdirectory():
    path_ = askdirectory()
    tree.clear()
    tree.load_path(path_ ) #Loads the specified tree for the tree control


#Tree mouse double click event
def treeDoubleClick(event):
    #print(event)
    item = tree.tree.selection()[0]
    i2=tree.tree.parent(item)
    s2=""
    while i2!="":
        s2=tree.tree.item(i2, "text")+'\\'+s2
        i2=tree.tree.parent(i2)
    txt2=s2+tree.tree.item(item, "text")
    if txt2[-4:]=='.txt' or txt2[-3:]=='.py':
        ucode.loadfile(txt2)
        tabControl.tab(0, text=ucode.filename)

def setlabel():
    tabControl.tab(0, text=ucode.filename)

#Left picture design
ttabControl = ttk.Notebook(paned)          # Create Tab Control
ttab1 = ttk.Frame(ttabControl)            # Add a third tab
ttabControl.add(ttab1, text='Project catalogue')      # Make second tab visible
paned.add(ttabControl)
paned.paneconfig(ttabControl,width=200)

tree = htk.Tree(ttab1,width=200)
tree.load_path('./') #Loads the specified tree for the tree control
tree.pack(expand = 1, fill = tk.BOTH)
tree.usepop=treeDoubleClick  #Bind double click event

#It is created in the left and right adjustable areas, and the area paned2 can be adjusted up and down
paned2 = tk.PanedWindow(paned,orient=tk.VERTICAL, showhandle=True, sashrelief=tk.SUNKEN,width=int((winw-300)/2),sashwidth=1)
paned.add(paned2)

#tabControl = htk.ClosableNotebook(paned2)  #Create a Notebook
tabControl = ttk.Notebook(paned2) 
tab = tk.Frame(tabControl,bd=0,background='black')
tabControl.add(tab, text='newfile.py')
#Create a code editor in the tab
ucode=htk.Codeedit(tab,fontsize=12,bg='black')   #Code edit box
tabControl.pack(fill=tk.BOTH, expand=1)
ucode.setlabel=setlabel
paned2.add(tabControl)
paned2.paneconfig(tabControl,heigh=400)

#Create partition area paned3
paned3 = tk.PanedWindow(paned2,orient=tk.VERTICAL, showhandle=True, sashrelief=tk.SUNKEN,width=int((winw-300)/2),bg='black',sashwidth=1)
paned2.add(paned3)

##Set a container for the message box
frame2=tk.LabelFrame(paned3,text='Information box',height=100)
frame2.pack(fill=tk.BOTH, expand=1)


umess=htk.PythonCMD(frame2,ipy="ipython",py="ipython ",fontsize=12,bg='#5a5a5a')
umess.pack(expand=1,fill=tk.BOTH)
paned3.paneconfig(frame2,heigh=200)

htk.ttmsg=umess.text  #Binding information output variables,
ucode.outmess=htk.ttmsg   #Set code output information box
htk.ttmsg['fg']='white'    #Output box font color
htk.ttmsg['insertbackground']='magenta'    #Output box cursor color

##Function of setting menu function
mainmenu.set('file','Execution procedure',command=ucode.runpy)
mainmenu.set('file','newly build',command=ucode.newfile)
mainmenu.set('file','open',command=ucode.openfile)
mainmenu.set('file','function',command=ucode.runuc)
mainmenu.set('file','preservation',command=ucode.savefile)
mainmenu.set('file','Save as',command=ucode.saveas)
mainmenu.set('edit','revoke',command=ucode.undo)
mainmenu.set('edit','redo',command=ucode.redo)
mainmenu.set('edit','shear',command=ucode.cut)
mainmenu.set('edit','copy',command=ucode.copy)
mainmenu.set('edit','paste',command=ucode.paste)
mainmenu.set('edit','Select all',command=ucode.selectall2)
mainmenu.set('program','function',command=ucode.runuc)
mainmenu.set('program','Transfer to Chinese',command=ucode.pyetoc)
mainmenu.set('program','Transfer to English',command=ucode.pyctoe)
mainmenu.set('help','Item thumbnail',command=root.save)
mainmenu.set('project','Project directory',command=selectdirectory)

#Set button
toolsbar.config(0,command=ucode.newfile)
toolsbar.config(1,command=ucode.openfile)
toolsbar.config(2,command=ucode.savefile)
toolsbar.config(3,command=ucode.runuc)
toolsbar.config(4,command=root.callback_quit)

#Create status bar
status=htk.StatusBar(root)    #Create status bar
status.pack(side=tk.BOTTOM, fill=tk.X)  #Put the status bar at the bottom
status.demo()  #Toolbar presentation

root.mainloop()      #Enter Tkinter message loop

The running results of the program are as follows.
Run in CMD window.

c:\xbpython>python  myide.py

The following figure appears.

We load a stock analysis program [KDJ index demonstration 6.py], and the code is as follows.

#K-line diagram and indicator demonstration
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import HP_tdx as htdx
from HP_formula import *
import HP_global as g  #Small white quantization global variable library
import HP_plt as hplt   #Small white quantitative index drawing module
plt.rcParams['font.sans-serif']=['SimHei'] #Used to display Chinese labels normally
plt.rcParams['axes.unicode_minus']=False #Used to display negative signs normally
#White background
g.ubg='w';g.ufg='b';g.utg='b';g.uvg='#1E90FF';

#Connect the market master station
htdx.TdxInit(ip='40.73.76.10',port=7709)
code='600080'
#Get daily data, 800 pieces of data
df = htdx.get_k_data(code,ktype='D',index=False,autype='qfq')
mydf=initmydf(df)  ##Initialize mydf table

mydf['K'],mydf['D'],mydf['J']=KDJ(9,3,3)

mydf['S80']=80  #Add track line of upper rail 80
mydf['X20']=20  #Add 20 tracks of lower rail

#Data reduction
m=1
mydf=mydf.tail(150*m).head(150).copy()

#Draw graphics
plt.figure(1,figsize=(10,8), dpi=80)

#Main drawing index
ax1=plt.subplot(211)
hplt.ax_K(ax1,mydf,t=code,n=0)

#Draw sub map index
ax2=plt.subplot(212)

mydf=mydf.tail(100)  #Display the last 100 data lines 
#The following is the line drawing statement
mydf.S80.plot.line()
mydf.X20.plot.line()
mydf.K.plot.line(legend=True)
mydf.D.plot.line(legend=True)
mydf.J.plot.line(legend=True)
plt.show()

Both [run module] and [run program] can display the graphic window correctly.

With future development iterations, I believe this editor module will become more and more perfect.
The following is the [little white quantitative Chinese Python research, learning and actual control system Vre 3.00] developed by us with this code editor, which will be a system that I can learn and actually control and quantify the real disk.
Let's run the following Chinese Python code to see the running results.

# -*- coding: utf-8 -*-
#Print 99 multiplication table through while loop print 99 multiplication table through while loop
#Execute with [run]
output()
variable j = 1
 Conditional cyclic variable j <= 9:
    variable i = 1
    Conditional cyclic variable i <= variable j:
        output('%d*%d=%d' % (variable i, variable j, variable i*variable j), end='\t')
        variable i += 1
    output()
    variable j += 1
 output('output')


Support interactive data input and output.

The following is a demonstration picture of Mt5 firm offer transaction.

Well, welcome to follow my blog.
Surpassing myself is my every step! My progress is your progress!

Topics: Python IDE Tkinter