python用Tkinter做自己的中文代碼編輯器
前面我們給了Tkinter接管Python輸入和輸出的介紹,我們不難可以想到,能用Tkinter來開發(fā)自己的Python代碼編輯器.例如可以使用Text控件作代碼編輯器.
實際上我在HP_tk2中已經(jīng)封裝好了現(xiàn)成的中文Python代碼編輯器組件和防Ipython功能的組件,另用這2個組件很容易搭建出自己的代碼編輯器.
下面直接給出完整演示源代碼.
#中文可視化Python開發(fā)系統(tǒng).py
import tkinter as tk #導(dǎo)入Tkinter
import tkinter.ttk as ttk #導(dǎo)入Tkinter.ttk
import tkinter.tix as tix #導(dǎo)入Tkinter.tix
from tkinter.filedialog import *
from tkinter.messagebox import *
import PIL
from PIL import Image, ImageTk, ImageDraw, ImageFont
import HP_tk2 as htk #導(dǎo)入htk
import webbrowser
import os
import sys
import threading
import time
#建立應(yīng)用窗口
root=htk.MainWindow(title='中文Python代碼編輯器',x=0,y=0,w=1200, h=800,picture='',zoom=True,center=True)
root.iconbitmap('ico/cp64.ico') #設(shè)置應(yīng)用程序圖標(biāo)
root.SetCenter() #移動到屏幕中央
#建立菜單
menus = [['文件',['執(zhí)行程序','-','新建','打開','運行','-','保存','另存為']],\
['編輯',['撤銷','重做','-','剪切','復(fù)制','粘貼','清除','-','全選']],\
['顯示',['繪圖','表格']],\
['程序',['運行','編譯']],\
['項目',['工程設(shè)置','系統(tǒng)設(shè)置']],\
['數(shù)據(jù)',['連接行情服務(wù)器','斷開行情服務(wù)器','下載股票代碼表','下載財務(wù)數(shù)據(jù)',\
'下載板塊數(shù)據(jù)']],\
['幫助',['關(guān)于軟件','退出']]]
mainmenu=htk.windowMenu(root,menus) #窗口菜單
toolsbar=htk.ToolsBar(root,6,bg='yellow') #創(chuàng)建工具欄,參數(shù)1-20
toolsbar.pack(side=tk.TOP, fill=tk.X)
#改變工具條圖標(biāo)
png1= PIL.ImageTk.PhotoImage(PIL.Image.open('ico/New2.ico'))
png2= PIL.ImageTk.PhotoImage(PIL.Image.open('ico/APS0.ico'))
png3= PIL.ImageTk.PhotoImage(PIL.Image.open('ico/class.ico'))
png4= PIL.ImageTk.PhotoImage(PIL.Image.open('ico/clxokcnhlp1.ico'))
png5= PIL.ImageTk.PhotoImage(PIL.Image.open('ico/Table.ico'))
toolsbar.config(0,image=png1)
toolsbar.config(1,image=png2)
toolsbar.config(2,image=png3)
toolsbar.config(3,image=png4)
toolsbar.config(4,image=png5)
#創(chuàng)建狀態(tài)欄
status=htk.StatusBar(root) #建立狀態(tài)欄
status.pack(side=tk.BOTTOM, fill=tk.X)
status.clear() #清空狀態(tài)欄信息
status.text(0,'狀態(tài)欄') #在狀態(tài)欄0輸出信息
status.text(1,'超越自我!') #在狀態(tài)欄2輸出信息
status.text(2,'人生苦短,學(xué)習(xí)中文Pyhthon3 !') #在狀態(tài)欄2輸出信息
status.text(3,'設(shè)計:獨狼')
status.text(4,'版權(quán)所有!')
status.text(5,'侵權(quán)必究!')
status.config(1,color='red') #改變狀態(tài)欄2信息顏色
status.config(0,color='blue') #改變狀態(tài)欄0信息顏色
status.config(3,width=14) #改變狀態(tài)欄3寬度
#分割窗口為左右兩部分,m1左,m2右
m1 = tk.PanedWindow(root,showhandle=True, sashrelief=tk.SUNKEN,sashwidth=1,width=200) #默認是左右分布的
m1.pack(fill=tk.BOTH, expand=1)
m2 = tk.PanedWindow(orient=tk.VERTICAL, showhandle=True, sashrelief=tk.SUNKEN,height=500)
m1.add(m2)
#t2是右上畫面
t2=tk.Frame(m2,bg='blue',heigh=500)
m2.add(t2)
ucode=htk.useredit(t2,fontsize=12) #代碼編輯框
ucode.fontsize=12
m2.paneconfig(t2,heigh=500)
#T3是右下畫面
t3=tk.Frame(m2,bg='yellow',heigh=150)
m2.add(t3)
umess=htk.useredit2(t3,fontsize=12) #信息輸出框
m2.paneconfig(t3,heigh=3150)
htk.ttmsg=umess.textPad #綁定信息輸出變量,
ucode.outmess=htk.ttmsg #設(shè)置代碼輸出信息框
label3 = tk.Label(umess.statusbar ,width=5, text='AI對話:')
label3.pack(side=tk.LEFT)
us=tk.StringVar(value='')
us2=tk.Entry(umess.statusbar,width=110, textvariable=us)
us2.pack(side=tk.LEFT)
path='./guide'
ucode.loadfile(path+'/軟件說明.txt')
global timer
def fun_timer2():
global timer
def fun_timer():
global timer
dt=time.strftime(' %Y-%m-%d %H:%M:%S',time.localtime(time.time()))
status.text(1,dt) #在狀態(tài)欄2輸出信息
timer = threading.Timer(1, fun_timer)
timer.start()
timer = threading.Timer(1, fun_timer)
timer.start()
htk.thread_it(fun_timer2())
def udestroy():
global timer
timer.cancel()
root.udestroy=udestroy
root.mainloop() #開啟tk主循環(huán)
程序運行結(jié)果如下:

上面給出的是部分演示代碼,如果繼續(xù)深入開發(fā),完全可以實現(xiàn)IDEL編輯器的功能.
上面的Python代碼編輯器模塊,我們已經(jīng)用在商業(yè)化的小白量化軟件中了.
到此這篇關(guān)于python用Tkinter做自己的中文代碼編輯器的文章就介紹到這了,更多相關(guān)Tkinter 中文代碼編輯器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
從訓(xùn)練好的tensorflow模型中打印訓(xùn)練變量實例
今天小編就為大家分享一篇從訓(xùn)練好的tensorflow模型中打印訓(xùn)練變量實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
利用Python實現(xiàn)Excel的文件間的數(shù)據(jù)匹配功能
這篇文章主要介紹了利用Python實現(xiàn)Excel的文件間的數(shù)據(jù)匹配,本文通過一個函數(shù)實現(xiàn)此功能,通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
在Python中構(gòu)建增廣矩陣的實現(xiàn)方法
今天小編就為大家分享一篇在Python中構(gòu)建增廣矩陣的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
教你怎么用Python實現(xiàn)GIF動圖的提取及合成
今天教大家一個Python有趣好玩的小功能:將多張圖片轉(zhuǎn)為GIF,同時也可以將一個GIF動圖提取出里面的圖片,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06

