带有笔记本小部件python ttk

时间:2019-04-06 18:55:02

标签: python-3.x tkinter tkinter-canvas ttk

我正在使用python 3.4.4和tkinter ttk ..当前尝试使用我的ttk line number小部件实现notebook小部件,但没有给我错误,也没有打印行号..使用单个Text小部件就可以了...我确实犯了一个错误,但是我无法修复它,感谢您的帮助。

main.py

import tkinter.ttk as ttks
from tkinter import LEFT,RIGHT,X,Y,BOTH
class MainUI:
    def __init__(self,master):
        self.master = master
        self.txs = []
        self.linenumber = ttks.tkinter.Canvas(self.master,border=0,width=25,bg="#808b96")
        self.linenumber.pack(side=LEFT,fill=Y)
        self.nb = ttks.Notebook(self.master)
        self.nb.pack(fill='both',expand=1)
        self.add_tabs()
        self._orig = self.txs[self.nb.index('current')]._w +"_orig"
        self.master.call("rename",self.txs[self.nb.index('current')]._w,self._orig)
        self.master.createcommand(self.txs[self.nb.index('current')]._w,self._proxy)
        self.master.bind('<Control-n>',self.add_tabs)
    def add_tabs(self,event=None):
        self.page_name = ttks.Frame(self.nb)
        self.tx = ttks.tkinter.Text(self.page_name)
        self.txs.append(self.tx)
        self.tx.pack(fill=BOTH,expand=1)
        self.nb.add(self.page_name,text="pagename")
    def redraw(self):
        self.linenumber.delete("all")
        i = self.txs[self.nb.index('current')].index("@0,0")
        while True:
            dline = self.txs[self.nb.index('current')].dlineinfo(i)
            if dline is None: break
            y = dline[1]
            linenum = str(i).split(".")[0]
            self.linenumber.create_text(2,y,anchor="nw",text=linenum)
            i = self.txs[self.nb.index('current')].index("%s+1line"%i)
    def _proxy(self,*args):
        try:
           cmd = (self._orig,)+args
           result = self.master.call(cmd)
           if (args[0] in ("insert","replace","delete") or
               args[0:3] in ("mark","set","insert") or
               args[0:2] in ("xview","moveto") or
               args[0:2] in ("xview","scroll") or
               args[0:2] in ("yview","moveto") or
               args[0:2] in ("yview","scroll")
           ):
               self.txs[self.nb.index('current')].event_generate("<<Change>>",when="tail")
           return result
        except:
            pass
    def Onchange(self,event):
        self.redraw() 

1 个答案:

答案 0 :(得分:1)

我忘记了绑定Onchange方法。 self.txs[self.nb.index('current')].bind("<<Change>>",self.Onchange) self.txs[self.nb.index('current')].bind("<Configure>",self.Onchange)