0
votes

The editor works perfectly but there is very small problem is that when I select 'Bold' font-style 'italic' is automatically un-selected and when I "UNDERLINE" text bold and italic are un-selected, In short, one function is working only at each time. I tried all possible ways those are commented with code

#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)

This image is a look of my text editor

image

Underline

image

Bold

image

Italic

image

1
Welcome to StackOverflow! What is your question? Also, you may find the help center page on creating minimal, reproducible examples helpful for crafting questions on this site. - Brian

1 Answers

0
votes
> #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)

I know this is an old post. But wanted to tackle it anyway since I don't use TKinter. So! This is just the way my brain forms the process w/ pseudo code.

#this is part of a toggle button class or the like, or just passes the button state. def toggle_Button(btnProperty): #Info: btnProperty = 'weight' here, but could be 'slant', 'underline, 'overstrike', or any other two state array property. #Thought Prep: we presume that the text starts in normal style until this is clicked so we only need to flip it.

#We don't need to reassign the font family just change weight value. text_property.config(weight = 'bold' if text_property.cget('weight') == 'normal' else 'normal')

#Step 0: if this button has been clicked. Toggle this button state. this.toggleState = not this.toggleState

Now the switching is less complicated...

$$ compounding the font options: $$

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()

it isn't as clean as i would like it but I couldn't figure how to access the slant and weight array to just reverse the index " int(not weight/slant) "

edit- My bad forgot to directly answer OP question/issue

#1 You make redundant calls with " text_property.actual()['weight'] " checking states, but we don't care about the state since it is binary. And we should only consider the state IF we are switching it. We assume the state is normal to start or "default".

#2 You recreate the font after every check type. THIS is your main problem you are asking about. You overwrite any change or compounded font styles on the text.

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.

#again quick simplified solution:

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)

this will enable you to keep your font styles as you enable/disable them on the text.

#And with that your question should be answered.