Posts: 6
Threads: 4
Joined: Sep 2021
Sep-30-2021, 03:21 AM
(This post was last modified: Sep-30-2021, 06:58 PM by Yoriz.
Edit Reason: Added prefix
)
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()
Posts: 1,139
Threads: 114
Joined: Sep 2019
Sep-30-2021, 05:57 PM
(This post was last modified: Sep-30-2021, 05:57 PM by menator01.)
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]
|