【问题标题】:Text Editor on Python-3.7.4 using tkinter使用 tkinter 在 Python-3.7.4 上的文本编辑器
【发布时间】:2020-03-31 23:04:24
【问题描述】:

编辑器工作完美,但有一个很小的问题是,当我选择“粗体”时,字体样式“斜体”会自动取消选中,而当我“下划线”时,粗体和斜体文本会被取消选中,简而言之,一个功能只在每次工作。 我尝试了所有可能的用代码注释的方式

#BOLD BUTTON FUNCTIONALITY 
def change_bold():
    text_property = tk.font.Font(font = text_editor['font'])
    if text_property.actual()['weight'] == 'normal':
        text_editor.configure(font= (current_font_family, current_font_size, 'bold'))
    if text_property.actual()['weight'] == 'bold':
        text_editor.configure(font= (current_font_family, current_font_size, 'normal'))   
    if text_property.actual()['slant'] == 'italic':
        text_editor.configure(font= (current_font_family, current_font_size, 'bold','italic')) 

bold_button.configure(command = change_bold)


# ITALIC BUTTON FUNCTIONALITY 
def change_italic():
    text_property = tk.font.Font(font = text_editor['font'])
    if text_property.actual()['slant'] == 'roman':
        text_editor.configure(font= (current_font_family, current_font_size, 'italic'))
    if text_property.actual()['slant'] == 'italic':
        text_editor.configure(font= (current_font_family, current_font_size, 'normal'))
    if text_property.actual()['weight'] == 'bold':
        text_editor.configure(font= (current_font_family, current_font_size, 'bold','italic'))


italic_button.configure(command = change_italic)


# UNDERLINE BUTTON FUNCTIONALITY 
def change_underline():
    text_property = tk.font.Font(font = text_editor['font'])
    if text_property.actual()['underline'] == 0:
        text_editor.configure(font= (current_font_family, current_font_size, 'underline'))
    if text_property.actual()['underline'] == 1:
        text_editor.configure(font= (current_font_family, current_font_size, 'normal'))

    # if text_property.actual()['weight'] == 'normal' and text_property.actual()['slant'] == 'roman':
    #     text_editor.configure(font= (current_font_family, current_font_size, 'normal','roman',1))

    # if text_property.actual()['weight'] == 'bold' and text_property.actual()['slant'] == 'roman':
    #     text_editor.configure(font= (current_font_family, current_font_size, 'bold','roman',1))

    # if text_property.actual()['weight'] == 'bold' and text_property.actual()['slant'] == 'italic':
    #     text_editor.configure(font= (current_font_family, current_font_size, 'bold','italic',1))

    # if text_property.actual()['weight'] == 'normal' and text_property.actual()['slant'] == 'italic':
    #     text_editor.configure(font= (current_font_family, current_font_size, 'normal','italic',1))    

underline_button.configure(command = change_underline)

这张图片是我的文本编辑器的外观

下划线

粗体

斜体

【问题讨论】:

标签: python tkinter


【解决方案1】:
> #BOLD BUTTON FUNCTIONALITY  def change_bold():
>     text_property = tk.font.Font(font = text_editor['font'])
>     if text_property.actual()['weight'] == 'normal':
>         text_editor.configure(font= (current_font_family, current_font_size, 'bold'))
>     if text_property.actual()['weight'] == 'bold':
>         text_editor.configure(font= (current_font_family, current_font_size, 'normal'))   
>     if text_property.actual()['slant'] == 'italic':
>         text_editor.configure(font= (current_font_family, current_font_size, 'bold','italic')) 
> 
> bold_button.configure(command = change_bold)

我知道这是一篇旧帖子。但无论如何我都想解决它,因为我不使用 TKinter。所以!这正是我的大脑使用伪代码形成过程的方式。

#this 是切换按钮类等的一部分,或者只是传递按钮状态。 def 切换按钮(btnProperty): #Info: btnProperty = 'weight' 这里,但可以是 'slant'、'underline、'overstrike' 或任何其他两个状态数组属性。 #思想准备:我们假设文本以正常样式开始,直到单击它,所以我们只需要翻转它。

#我们不需要重新分配字体系列,只需更改权重值即可。 text_property.config(weight = 'bold' if text_property.cget('weight') == 'normal' else 'normal')

#Step 0:如果该按钮已被点击。切换此按钮状态。 this.toggleState = 不是 this.toggleState

现在切换不那么复杂了……

$$ 复合字体选项:$$

def toggleButton():
this.toggleState = not this.toggleState

def toggle_slant():
text_property.config(slant = 'italic' if text_property.cget('slant') == 'roman' else 'roman')
toggleButton()

def toggle_bold():
text_property.config(weight= 'bold' if text_property.cget('weight') == 'normal' else 'normal')
toggleButton()

def toggle_underline():
text_property.config(underline = 1 if text_property.cget('underline') == 0 else 0)
toggleButton()

它不像我想要的那样干净,但我不知道如何访问倾斜和权重数组来反转索引“int(not weight/slant)”

编辑- 我忘了直接回答 OP 问题/问题

#1 您使用“ text_property.actual()['weight'] ”检查状态进行冗余调用,但我们不关心状态,因为它是二进制的。我们应该只考虑我们正在切换的状态。我们假设状态是正常启动或“默认”。

#2 在每次检查类型后重新创建字体。这是您要问的主要问题。您覆盖文本上的任何更改或复合字体样式。

text_property = tk.font.Font(font = text_editor['font'])
    if text_property.actual()['weight'] == 'normal': #K so we now know it is normal
        text_editor.configure(font= (current_font_family, current_font_size, 'bold')) #and now we have set it to ONLY bold. you don't need to change the whole font to toggle the bold option on and off.

#再次快速简化解决方案:

te = text_editor
te.configure(weight = 'bold') or te.configure(weight = 'normal')
te.configure(slant = 'italic') or te.configure(slant = 'roman')
te.configure(underline = 1) or te.configure(underline = 0)
te.configure(overstrike = 1) or te.configure(overstrike = 0)

这将使您能够在启用/禁用文本时保留您的字体样式。

#这样你的问题就应该得到回答了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-07
    • 2011-09-22
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    相关资源
    最近更新 更多