python用Tkinter做自己的中文代碼編輯器
更新時間:2020年09月07日 11:04:20 作者:荷蒲
這篇文章主要介紹了python用Tkinter做自己的中文代碼編輯器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
前面我們給了Tkinter接管Python輸入和輸出的介紹,我們不難可以想到,能用Tkinter來開發(fā)自己的Python代碼編輯器.例如可以使用Text控件作代碼編輯器.
實際上我在HP_tk2中已經封裝好了現成的中文Python代碼編輯器組件和防Ipython功能的組件,另用這2個組件很容易搭建出自己的代碼編輯器.
下面直接給出完整演示源代碼.
#中文可視化Python開發(fā)系統.py import tkinter as tk #導入Tkinter import tkinter.ttk as ttk #導入Tkinter.ttk import tkinter.tix as tix #導入Tkinter.tix from tkinter.filedialog import * from tkinter.messagebox import * import PIL from PIL import Image, ImageTk, ImageDraw, ImageFont import HP_tk2 as htk #導入htk import webbrowser import os import sys import threading import time #建立應用窗口 root=htk.MainWindow(title='中文Python代碼編輯器',x=0,y=0,w=1200, h=800,picture='',zoom=True,center=True) root.iconbitmap('ico/cp64.ico') #設置應用程序圖標 root.SetCenter() #移動到屏幕中央 #建立菜單 menus = [['文件',['執(zhí)行程序','-','新建','打開','運行','-','保存','另存為']],\ ['編輯',['撤銷','重做','-','剪切','復制','粘貼','清除','-','全選']],\ ['顯示',['繪圖','表格']],\ ['程序',['運行','編譯']],\ ['項目',['工程設置','系統設置']],\ ['數據',['連接行情服務器','斷開行情服務器','下載股票代碼表','下載財務數據',\ '下載板塊數據']],\ ['幫助',['關于軟件','退出']]] mainmenu=htk.windowMenu(root,menus) #窗口菜單 toolsbar=htk.ToolsBar(root,6,bg='yellow') #創(chuàng)建工具欄,參數1-20 toolsbar.pack(side=tk.TOP, fill=tk.X) #改變工具條圖標 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,'人生苦短,學習中文Pyhthon3 !') #在狀態(tài)欄2輸出信息 status.text(3,'設計:獨狼') status.text(4,'版權所有!') status.text(5,'侵權必究!') 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 #設置代碼輸出信息框 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)
程序運行結果如下:
上面給出的是部分演示代碼,如果繼續(xù)深入開發(fā),完全可以實現IDEL編輯器的功能.
上面的Python代碼編輯器模塊,我們已經用在商業(yè)化的小白量化軟件中了.
到此這篇關于python用Tkinter做自己的中文代碼編輯器的文章就介紹到這了,更多相關Tkinter 中文代碼編輯器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!