Python Forum
[Tkinter] Trouble changing Font within tkinter frame title
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Trouble changing Font within tkinter frame title
#1
   
Hello all,
I am trying to change the font of within the tkinter's frame title. I've come across a few methods on the internet but none of the methods work as expected. The most common answer was to add 2 lines of code:
s = ttk.Style()
s.configure('TNotebook.Tab', font=('URW Gothic L','11','bold') )
However, this code doesn't work. It resets all my other fonts, it also gives me an error when I try to use an image. Also, a second tkinter window opens, which i've never seen before.
   
Below is the code for the entire window. If i uncomment lines 36 and 37 I get an error and nothing happens. I am not sure what is happening, if anyone can explain a bit more what those 2 lines of code are doing that would be much appreciated. Thanks in advance!
error:
Traceback (most recent call last):
  File "C:\Users\Sami Hawasli\Documents\Python\LMX2820_GUI_Tabs.py", line 36, in <module>
    logo = Label(win, image = img)
  File "C:\Program Files (x86)\Thonny\lib\tkinter\__init__.py", line 2766, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Program Files (x86)\Thonny\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist
from tkinter import *
from tkinter import ttk
import tkinter.font
from PIL import ImageTk, Image

s = ttk.Style()
s.configure('TNotebook.Tab', font=('URW Gothic L','11','bold') )

def close():
    win.destroy()
    
#Initialize Output A and 2 on/off state
RFA = 1
RFB = 0
ErrorMessage = "Please Initialize"

### Gui definitions ###
win = Tk()
win.resizable(False, False)
#win.geometry("1000x1000")
win.title("Part Number: DGRF2820 Rev1      Software ver 1.0")
titleFont = tkinter.font.Font(family="Garamond", size = 18, weight = "bold")          #Garamond
titleFontSmall = tkinter.font.Font(family="Garamond", size = 14, weight = "bold")
bodyFont = tkinter.font.Font(family="Garamond", size = 14)
errorFont = tkinter.font.Font(family="Garamond", size = 12)
win.iconbitmap("DGRF_Icon.ico")

###   Header Row   ###
TitleLabel = Label(win, text = ('DGRF Frequency Synthesizer 45 MHz - 22 GHz'),font=titleFont)
TitleLabel.grid(row=0,column=1,columnspan=8)
#InitButton = Button(win,text='Initialize',font=bodyFont,command= lambda: LMX2820Init(RefDoublerVar.get(),int(RefPreDiv.get()),int(RefMultiVar.get()),int(RefPostDiv.get())) , bg = "bisque2") #,bg='grey')
#InitButton.grid(row=0, column=8)

###  Footer Row ###
img = ImageTk.PhotoImage(Image.open("DGRF_Logo_Smaller.png"))
#logo = Label(win, image = img)
#logo.grid(row=100,column=1)
ErrorLabel = Label(win, text = ('   Error messages:'),font=titleFontSmall)
ErrorLabel.grid(row=100,column=2)
ErrorMessageLabel = Label(win, text = ErrorMessage,font=errorFont)
ErrorMessageLabel.grid(row=100,column=3, columnspan=4)
exitbutton = Button(win,text='EXIT',font=bodyFont,command=close,bg='red',height=1,width=10)
exitbutton.grid(row=100, column=8)



RFnotebook = ttk.Notebook(win)
RFnotebook.grid(row = 1, column = 1, columnspan=8)

RFframe1 = Frame(RFnotebook,width = 1500, height = 500) #bg = "blue")
RFframe2 = Frame(RFnotebook,width = 1500, height = 500) #bg = "red")

RFnotebook.add(RFframe1, text="Reference Chain")
RFnotebook.add(RFframe2, text="CW output")

win.mainloop()
Reply
#2
I did a version of your code. You can look at the way I did the styling and maybe it will help.
#! /usr/bin/env python3

import tkinter as tk
from tkinter import ttk
from ttkbootstrap import Style
import sys

class Window:
    def __init__(self, parent):
        self.parent = parent
        self.parent.rowconfigure(0, weight=1)
        self.parent.columnconfigure(0, weight=1)

        # Create some styles
        exit_btn = Style()
        exit_btn.map('danger.TButton', background = [
        ('disabled', 'tomato'), ('active', 'red')])
        exit_btn.configure('danger.TButton', font=('verdana', 12, 'bold'))

        nb = ttk.Style()
        cur_nb_theme = nb.theme_use()
        nb.theme_settings(cur_nb_theme, {'TNotebook.Tab': {'configure':
                                        {'font': ('comic sans ms', 12, 'bold'),
                                         'foreground': 'blue', 'background': 'lightblue'}
                                        }})


        # Container frame to hold everything
        container = tk.Frame(self.parent)
        container.grid(column=0, row=0, sticky='news')
        container.grid_columnconfigure(0, weight=3)

        # Configure the header
        label = tk.Label(container)
        label['text'] = 'DGRF Frequency Synthesizer 45 MHz - 22 GHz'
        label['font'] = ('comic sans ms', 18, 'bold')
        label['foreground'] = 'steel blue'
        label['relief'] = 'groove'
        label['pady'] = 3
        label.grid(column=0, row=0, sticky='new')

        # Create the notebook
        notebook = ttk.Notebook(container)
        notebook.grid(column=0, row=1, sticky='new')

        # Create and add the tabs
        tab1 = ttk.Frame(notebook)
        tab1.grid(column=0, row=0, sticky='new')

        tab2 = ttk.Frame(notebook)
        tab2.grid(column=1, row=0, sticky='new')

        notebook.add(tab1, text='Reference Chain')
        notebook.add(tab2, text='CW Output')

        # Create a container frame for the two info frames and button
        btm_container = tk.Frame(container)
        btm_container.grid(column=0, row=2, sticky='new', pady=3)
        btm_container.grid_columnconfigure(0, weight=1)
        btm_container.grid_columnconfigure(1, weight=3)
        btm_container.grid_columnconfigure(2, weight=1)
        # for i in range(3):
        #     btm_container.grid_columnconfigure(i, weight=3, uniform='btm')

        # Create error message label
        error_label = ttk.Label(btm_container, anchor='w', background='lightblue')
        error_label['text'] = 'Error Messages:'
        error_label['padding'] = [10, 0, 0, 0]
        error_label['font'] = ('tahoma', 10, 'bold')
        error_label['foreground'] = 'navy'
        error_label.grid(column=0, row=0, sticky='news')

        # Create message label
        message = ttk.Label(btm_container, anchor='w', background='lightblue')
        message['text'] = 'Please Initialize'
        message['font'] = ('tahoma', 10, 'italic bold')
        message.grid(column=1, row=0, sticky='news')

        btn = ttk.Button(btm_container, text='Exit', cursor='hand2')
        btn['command'] = self.exit
        btn['style'] = 'danger.TButton'
        btn.grid(column=2, row=0, sticky='new', padx=2)

        # Create some data for the tabs
        for i in range(20):
            label = tk.Label(tab1, text=f'Line {i} text for tab1')
            label.grid(column=0, row=i, sticky='news')

        for i in range(20):
            label = tk.Label(tab2, text=f'Line {i} text for tab2')
            label.grid(column=0, row=i, sticky='news')

    # Create the method/function for the exit button
    def exit(self):
        sys.exit()

def main():
    root = tk.Tk()
    # root.geometry('800x600+250+250')
    # root.resizable(0, 0)
    root['padx'] = 5
    root['pady'] = 3
    root.title('Part Number: DGRF2820 Rev1      Software ver 1.0')
    Window(root)
    root.mainloop()

if __name__ == '__main__':
    main()
[attachment=1291]
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Tkinter Window Has no Title Bar gw1500se 4 573 Nov-07-2021, 05:14 PM
Last Post: gw1500se
  tkinter frame camera opencv Nick_tkinter 9 2,051 Mar-21-2021, 06:41 PM
Last Post: Nick_tkinter
  Tkinter menu font size -method to change tonycat 2 3,159 Oct-11-2020, 02:43 AM
Last Post: tonycat
  [Tkinter] How to Print a list = ['a','b','c'], using tkinter along with a custom font? Pleiades 2 1,078 Sep-15-2020, 03:54 PM
Last Post: Pleiades
  [Tkinter] Trying to change font size w/o changing button size python63 3 6,385 Aug-05-2020, 01:04 AM
Last Post: Larz60+
  [Tkinter] Tkinter delete values in Entries, when I'm changing the Frame robertoCarlos 11 2,245 Jul-29-2020, 07:13 PM
Last Post: deanhystad
  [Tkinter] changing the frame colour nick123 4 12,044 Jul-24-2020, 07:32 AM
Last Post: LegacyCoding
  Why is wx.NO_BORDER changing panels within a frame MeghansUncle2 4 1,115 Jul-12-2020, 12:32 PM
Last Post: Yoriz
  [Tkinter] tkinter, dropdowns with changing options Sheepykins 4 7,517 Jul-03-2020, 06:06 AM
Last Post: jdos
  changing tkinter label from thread nanok66 3 3,345 Jun-07-2020, 01:37 AM
Last Post: nanok66

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020