简体   繁体   English

tkinter tag_config不起作用

[英]tkinter tag_config does not work

I am building a notepad like application in tkinter-python. 我正在tkinter-python中构建类似应用程序的记事本。 There is an option to change the font of the text writen in the text field of the application. 有一个选项可以更改在应用程序的文本字段中写入的文本的字体。

I have created a Font Chooser popup screen to be called from main window on clicking 'font' menu, which basically creates a FontChooser class object and passes to the main window, which sets the font in man window. 我创建了一个“字体选择器”弹出屏幕,单击“字体”菜单后可从主窗口调用该屏幕,该屏幕基本上会创建一个FontChooser类对象并传递到主窗口,该主窗口将字体设置为手动窗口。

A sample of the code where font is getting set in main window is, 在主窗口中设置字体的代码示例如下:

root = Tix.Tk(className="Notepad")
notepad = ScrolledText(root, width=100, height=100)

def open_font():
    font = MyFont.askChooseFont(root)
    notepad.tag_add("bt", "sel.first", "sel.last")
    notepad.tag_config("bt", font=font.getFontTuple())

Now when I first run the application and select a portion of text and change the font, it works correctly. 现在,当我第一次运行该应用程序并选择一部分文本并更改字体时,它可以正常工作。 But after that, whatever portion of text I am selecting and changing the font, it is ignoring the selection and applying the font on the whole text. 但是在那之后,无论我选择文本的哪一部分并更改字体,它都会忽略选择内容并将字体应用于整个文本。 Can anyone let me know what is the problem here? 谁能告诉我这里是什么问题?

IDLE uses tag_config to syntax color python code and it works on all Python versions and major OSes for the last 15 years. IDLE使用tag_config来对彩色python代码进行语法化处理,并且在过去15年中可在所有Python版本和主要操作系统上使用。

To have some idea of why it seems to fail for you, you need to find an MCVE that fails. 要了解为什么它似乎对您失败,您需要找到一个失败的MCVE Start without tix and scrollbars. 开始时没有tix和滚动条。 (Tix is deprecated in 3.6 and bugs are not being fixed.) Also notice that your code uses the same tag for each selection, so that when you change the configuration, it applies to all previous selections. (Tix在3.6中已弃用,并且未修复错误。)另请注意,您的代码对每个选择都使用相同的标记,因此,当您更改配置时,它将应用于所有先前的选择。

Here is simplified code that works as intended and expected. 这是可以按预期和预期运行的简化代码。

import tkinter as tk
import time
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.insert('1.0', "line 1\nline 2\nline 3\n")
text.tag_add('bg', '1.0', '1.4')
text.tag_config('bg', background='red')
root.update()
time.sleep(1)
text.tag_add('bg', '2.0', '2.4')
text.tag_config('bg', background='blue')
root.update()

You could try modifying it step by step until it either reproduces your problem or does what you want. 您可以尝试逐步修改它,直到它重现您的问题或您想要的为止。

EDIT with example modification: use 'sel.first' and 'sel.last' instead of hard-coded indexes. 编辑并进行示例修改:使用“ sel.first”和“ sel.last”代替硬编码的索引。

import tkinter as tk
import time
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.insert('1.0', "line 1\nline 2\nline 3\n")
root.update()  # make text visible for selection
input('select some text')
text.tag_add('bg', 'sel.first', 'sel.last')
text.tag_config('bg', background='red')
root.update()  # make change visible
input('select some text')
text.tag_add('bg', 'sel.first', 'sel.last')
text.tag_config('bg', background='blue')
root.update()  # make 2nd change visible
input('look at result')

Run in console. 在控制台中运行。 Move tk window so console and GUI are both visible. 移动tk窗口,使控制台和GUI均可见。 Make selection as prompted. 根据提示进行选择。 Click on console* and hit return to allow input statement to return. 单击控制台*,然后按回车键以允许输入语句返回。 Repeat. 重复。 The result for me is that both selections, but not everything, turns blue. 对我来说,结果是两个选择(但不是全部)都变成蓝色。 I suggest changing font instead of bg color for the next experiment. 我建议在下一个实验中更改字体而不是bg颜色。

  • On Windows, the selection highlighting in the tk windows disappears when one clicks on the console because Windows only allows visible selection in one window at a time. 在Windows上,单击一次控制台时,tk窗口中突出显示的选择消失,因为Windows一次只允许一个窗口中的可见选择。 However, the select markers are still present in the text widget so that tag_add still works. 但是,文本窗口小部件中仍然存在选择标记,因此tag_add仍然有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM