在尝试使用复合tkinter小部件制作编辑器时,我偶然发现了错误或错误? MyTextWidget类中定义的方法:fl()和fh()用于设置属于同一类的文本窗口小部件的字体大小。
我的理解是这应该有效,但是当我使用create_window()方法在画布上有三个相同的MyTextWidget类实例时,按下fl按钮和fh按钮,所有三个texttidgets中的文本大小会同时发生变化。我首先使用一个小部件测试它,当一切都工作到我满意时,我添加了两个相同类的实例,但现在它没有按照我的预期工作。
如果这有帮助,那么版本是Python 2.7,而Tkinter版本是版本:81008,debian linux。
我们非常感谢您的帮助,特别是如果您可以指导我阅读有关相关信息的书籍或文档。请启发。
import Tkinter as tk
import tkFont
class MyTextWidget(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent)
self.text = tk.Text(self, *args, **kwargs)
self.tbar = tk.Frame(self, *args, **kwargs)
self.tbar.pack(side=tk.TOP, padx=2, pady=2, fill="x", expand=True)
self.fl = tk.Button(self.tbar)
self.fl.pack(side=tk.LEFT, padx=2, pady=2)
self.fh = tk.Button(self.tbar, *args, **kwargs)
self.fh.pack(side=tk.LEFT, padx=2, pady=2)
def fl():
print "fl called"
self.text.configure(font=tkFont.Font(family="mytsmc", size=7), spacing1=2,spacing2=22,spacing3=2)
def fh():
print "fh called"
self.text.configure(font=tkFont.Font(family="mytsmc", size=9), spacing1=2,spacing2=22,spacing3=2)
self.fl.config(text="fl", width=1, command=fl)
self.fh.config(text="fh", width=1, command=fh)
self.vsb = tk.Scrollbar(self, orient="vertical", command=self.text.yview)
self.text.configure(yscrollcommand=self.vsb.set,
font=tkFont.Font(family="mytsmc", size=8),
spacing1=2,spacing2=32,spacing3=2)
self.vsb.pack(side="right", fill="y")
# self.text.pack(side="left", fill="both", expand=True)
self.text.pack(side="left", fill="both", expand=False)
self.insert = self.text.insert
self.delete = self.text.delete
self.mark_set = self.text.mark_set
self.get = self.text.get
self.index = self.text.index
self.search = self.text.search
class myEditor(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.w = tk.Canvas(self, width=1320,
height=740,
borderwidth=1,
background='white',
relief='raised')
self.w.pack(anchor='center')
#One
self.scrolled_text1 = MyTextWidget(self)
self.firstwidget = self.w.create_window(10, 10,
anchor=tk.NW,
width=400,
height=400,
window=self.scrolled_text1)
with open("/home/username/datafiles/1.txt", "r") as f:
self.scrolled_text1.insert("1.0", f.read())
#Two
self.scrolled_text2 = MyTextWidget(self)
self.firstwidget = self.w.create_window(420, 10,
anchor=tk.NW,
width=400,
height=400,
window=self.scrolled_text2)
with open("/home/username/datafiles/2.txt", "r") as f:
self.scrolled_text2.insert("1.0", f.read())
#Three
self.scrolled_text3 = MyTextWidget(self)
self.firstwidget = self.w.create_window(830, 10,
anchor=tk.NW,
width=400,
height=400,
window=self.scrolled_text3)
with open("/home/username/datafiles/3.txt", "r") as f:
self.scrolled_text3.insert("1.0", f.read())
def switchtob(event=None):
self.scrolled_text1.text.focus()
print "switched to b"
def switchton(event=None):
self.scrolled_text2.text.focus()
print "switched to n"
def switchtom(event=None):
self.scrolled_text3.text.focus()
print "switched to m"
root.bind('<Control-b>',switchtob)
root.bind('<Control-n>',switchton)
root.bind('<Control-m>',switchtom)
root = tk.Tk()
myEditor(root).pack(side="top", fill="both", expand=True)
def exit(event=None):
quit()
root.bind('<Control-q>',exit)
root.mainloop()
答案 0 :(得分:1)
简单的解决方法是创建一次字体,保存对它们的引用,然后在每次单击按钮时使用它们而不是实例化新字体。
class MyTextWidget(tk.Frame):
def __init__(self, parent, *args, **kwargs):
...
self.font1 = tkFont.Font(family="mytsmc", size=7)
self.font2 = tkFont.Font(family="mytsmc", size=9)
...
def fl():
self.text.configure(font=self.font1, spacing1=2,spacing2=22,spacing3=2)
def fh():
self.text.configure(font=self.font2, spacing1=2,spacing2=22,spacing3=2)