我在框架 中有一個標籤和文本小部件,用作狀態欄。當光標移動 (通過按任意鍵或回車鍵),狀態 欄顯示位置。但更新速度較慢 並且當快速或重複按下「鍵」時,光標位置編號停止位顯示新的光標位置 。這裏是我的代碼...當光標移動時,光標位置統計信息更新緩慢
更新:
我張貼一些代碼,有人問我要發佈更多的代碼。 這裏是整個程序。希望它不多。我也有 文字部件(txt1)隨着所選 增加的字體大小而增大的問題。
請忽略搜索文本的功能,我只是盯着今天與 玩弄它的亂七八糟的一點點,
謝謝,韋恩。
代碼:
from tkinter import *
from tkinter import Menu
from tkinter import filedialog
from tkinter import messagebox
from tkinter import ttk
from tkinter import Tk
from tkinter import font
class Mainfun:
def __init__(self, master):
super(Mainfun, self).__init__()
self.master = master
fnt1=''
pnt1=''
wh1=''
def mkfrm():
self.topframe=Frame(self.master)
self.topframe.pack(side=TOP, fill=X)
self.frame1=Frame(master, padx=15, pady=10)
self.frame1.pack(fill=BOTH)
self.bottomframe=Frame(master)
self.bottomframe.pack(side=BOTTOM, fill=X)
def mktxt():
self.txt1=Text(self.frame1, padx=10, pady=14, height=22, width=79, bg="white", fg="black", wrap=WORD)
self.txt1.pack(side=LEFT, fill=BOTH)
def mkscr():
self.scr=Scrollbar(self.frame1, command=self.txt1.yview)
self.scr.pack(side=RIGHT, fill=Y)
def cpupdated(event):
self.cp1=self.txt1.index(CURRENT)
self.txt2.delete(1.0, END)
self.txt2.insert(INSERT, self.cp1)
self.txt2.update()
def mkstat():
self.cp1=''
self.status=Label(self.bottomframe, anchor=W, padx=15, bd=2, text="CURSOR: (Row):/(Col): ", relief=SUNKEN)
self.status.pack(side=BOTTOM, fill=X)
self.txt2=Text(self.status, height=1, width=5, bd=1, fg="black",bg="light grey")
self.txt2.pack(anchor=W, padx=150)
self.txt1.bind('<KeyPress>', cpupdated)
def mkmnu():
self.mnu=Menu(self.topframe)
self.master.config(menu=self.mnu)
self.filmenu=Menu(self.mnu, tearoff=0)
self.filmenu.add_command(label="New", command=filnew)
self.filmenu.add_command(label="Open", command=flopen)
self.filmenu.add_command(label="Save As", command=filsaveas)
self.filmenu.add_separator()
self.filmenu.add_command(label="Print", command=mkprnt)
self.filmenu.add_separator()
self.filmenu.add_command(label="Exit", command=filexit)
self.mnu.add_cascade(label=" File ", menu=self.filmenu)
self.editmenu=Menu(self.mnu, tearoff=0)
self.editmenu.add_command(label="Cut", command=filcut)
self.editmenu.add_command(label="Copy", command=filcopy)
self.editmenu.add_command(label="Paste", command=filpaste)
self.editmenu.add_separator()
self.editmenu.add_command(label="Select All", command=filsel)
self.mnu.add_cascade(label=" Edit ", menu=self.editmenu)
self.helpmenu=Menu(self.mnu, tearoff=0)
self.helpmenu.add_command(label="Find", command=fndtxt)
self.helpmenu.add_command(label="Change Font", command=mkfont)
self.helpmenu.add_command(label="About Us", command=aboutus)
self.mnu.add_cascade(label=" Tools ", menu=self.helpmenu)
def filnew():
self.txt1.event_generate('<Control-n>')
self.txt1.delete(1.0, END)
def flopen():
self.fil=filedialog.askopenfile()
self.txt1.delete(1.0, END)
self.txt1.insert(INSERT, self.fil.read())
self.fil.close()
def filsaveas():
self.fil=filedialog.asksaveasfile()
self.text2=str(self.txt1.get(1.0, END))
self.fil.write(self.text2)
self.fil.close()
def mkprnt():
self.txt1.event_generate('<Control-P>')
def filexit():
self.msg1=messagebox.askquestion("Exit:", "Do you realy want to exit?")
if self.msg1=="yes":
self.master.quit()
def filcut(*event):
self.txt1.event_generate('<Control-x>')
def filcopy():
self.txt1.event_generate('<Control-c>')
def filpaste():
self.txt1.event_generate('<Control-v>')
def filsel():
self.txt1.event_generate('<Control-a>')
def aboutus():
self.msg2=messagebox.showinfo("About", "Text Edit: v1.0\nPython: v3.4.4\nTkinter GUI: v8.6\nCompiler: Py2Exe v0.9.9.2\nAuthor - Wayne Lampiasi\nEmail: [email protected]")
def fndselect():
self.text3=str(self.txt3.get(1.0, END))
self.text4=str(self.txt1.get(1.0, END))
if self.text3==self.text4:
print("found")
else:
print("not found")
def fndquit():
self.fndwin.destroy()
def fndtxt():
self.fndwin=Tk()
self.fndwin.title("- Find -")
self.fndwin.geometry("225x100")
self.fndwin.resizable(0,0)
self.label1=Label(self.fndwin)
self.label1.grid(row=1, column=1)
self.label2=Label(self.fndwin, text="Enter Search:")
self.label2.grid(row=2, column=1, padx=15)
self.txt3=Text(self.fndwin, fg="black", bg="white", height=1, width=12)
self.txt3.grid(row=2, column=2)
self.label4=Label(self.fndwin)
self.label4.grid(row=4, column=1)
self.button1=Button(self.fndwin, text="Go", command=fndselect)
self.button1.grid(row=5, column=1)
self.button2=Button(self.fndwin, text="Close", command=fndquit)
self.button2.grid(row=5, column=2)
self.label5=Label(self.fndwin)
self.label5.grid(row=6, column=1)
def mkfont():
self.subwin=Tk()
self.subwin.title("- Fonts -")
self.subwin.geometry("225x260")
self.subwin.resizable(0,0)
self.label1=Label(self.subwin)
self.label1.grid(row=1, column=1)
self.label2=Label(self.subwin, text="Choose Font:")
self.label2.grid(row=2, column=1)
self.cb1=ttk.Combobox(self.subwin, width=25, state='readonly')
self.Stringval1=self.cb1
self.cb1.grid(row=3, column=1)
self.cb1['values']=['Arial',
'Helvetica',
'Courier New',
'Comic Sans MS',
'Fixedsys',
'MS Sans Serif',
'MS Serif',
'Symbol',
'System',
'Times New Roman',
'Verdana']
self.label3=Label(self.subwin)
self.label3.grid(row=4, column=1)
self.label4=Label(self.subwin, text="Choose Size:")
self.label4.grid(row=5, column=1)
self.cb2=ttk.Combobox(self.subwin, width=25, state='readonly')
self.Intval2=self.cb2
self.cb2.grid(row=6, column=1)
self.cb2['values']=[" 8",
" 9",
" 10",
" 11",
" 12",
" 13",
" 14",
" 15",
" 16",
" 17",
" 18"]
self.label5=Label(self.subwin)
self.label5.grid(row=7, column=1)
self.label6=Label(self.subwin, text="Choose Weight")
self.label6.grid(row=8, column=1)
self.cb3=ttk.Combobox(self.subwin, width=25, state='readonly')
self.Sringval3=self.cb3
self.cb3.grid(row=9, column=1, padx=20)
self.cb3['values']=["normal",
"bold"]
self.label7=Label(self.subwin)
self.label7.grid(row=11, column=1)
self.button1=Button(self.subwin, text="Apply Fonts", command=selected)
self.button1.grid(row=12, column=1)
def subwinquit():
self.subwin.destroy()
def selected():
self.Stringval1=self.cb1.get()
fnt1 = self.Stringval1
self.Intval2=self.cb2.get()
pnt1 = self.Intval2
self.Stringval3=self.cb3.get()
wh1 = self.Stringval3
font.Font(family=(fnt1, pnt1, wh1))
self.txt1.config(font=(fnt1, pnt1, wh1))
self.subwin.eval('ttk::CancelRepeat')
self.subwin.destroy()
mkfrm()
mktxt()
mkscr()
mkstat()
mkmnu()
root=Tk()
root.title("Text Edit")
root.resizable(0,0)
obj1=Mainfun(root)
root.update()
root.mainloop()
你能顯示更多的代碼嗎?例如無論創建「主人」。足以實際運行某些東西會更可取。 –
我們不需要_whole_代碼。請刪除與問題無關的代碼。如果問題是在綁定上顯示信息,則只需要文本小部件,綁定語句,綁定使用的函數以及幾行代碼將它們粘合在一起。請閱讀並遵循[如何創建最小,完整和可驗證示例]的建議(http://stackoverflow.com/help/mcve) –