当前位置: 移动技术网 > IT编程>脚本编程>Python > python用Tkinter做自己的中文代码编辑器

python用Tkinter做自己的中文代码编辑器

2020年09月08日  | 移动技术网IT编程  | 我要评论
前面我们给了tkinter接管python输入和输出的介绍,我们不难可以想到,能用tkinter来开发自己的python代码编辑器.例如可以使用text控件作代码编辑器.实际上我在hp_tk2中已经封

前面我们给了tkinter接管python输入和输出的介绍,我们不难可以想到,能用tkinter来开发自己的python代码编辑器.例如可以使用text控件作代码编辑器.

实际上我在hp_tk2中已经封装好了现成的中文python代码编辑器组件和防ipython功能的组件,另用这2个组件很容易搭建出自己的代码编辑器.

下面直接给出完整演示源代码.

#中文可视化python开发系统.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 = [['文件',['执行程序','-','新建','打开','运行','-','保存','另存为']],\
   ['编辑',['撤销','重做','-','剪切','复制','粘贴','清除','-','全选']],\
   ['显示',['绘图','表格']],\
   ['程序',['运行','编译']],\
   ['项目',['工程设置','系统设置']],\
   ['数据',['连接行情服务器','断开行情服务器','下载股票代码表','下载财务数据',\
    '下载板块数据']],\
   ['帮助',['关于软件','退出']]]
   
mainmenu=htk.windowmenu(root,menus) #窗口菜单


toolsbar=htk.toolsbar(root,6,bg='yellow') #创建工具栏,参数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)


#创建状态栏
status=htk.statusbar(root) #建立状态栏
status.pack(side=tk.bottom, fill=tk.x)
status.clear() #清空状态栏信息
status.text(0,'状态栏') #在状态栏0输出信息
status.text(1,'超越自我!') #在状态栏2输出信息
status.text(2,'人生苦短,学习中文pyhthon3 !') #在状态栏2输出信息
status.text(3,'设计:独狼')
status.text(4,'版权所有!')
status.text(5,'侵权必究!')
status.config(1,color='red') #改变状态栏2信息颜色
status.config(0,color='blue') #改变状态栏0信息颜色
status.config(3,width=14) #改变状态栏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) #在状态栏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主循环

程序运行结果如下:


上面给出的是部分演示代码,如果继续深入开发,完全可以实现idel编辑器的功能.

上面的python代码编辑器模块,我们已经用在商业化的小白量化软件中了.

到此这篇关于python用tkinter做自己的中文代码编辑器的文章就介绍到这了,更多相关tkinter 中文代码编辑器内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网