-1

Just started work on some GUI. I am an absolutely new to tkinter.

I can set the label width and font size using .config for individual labels.

I would like to make this the default for a specific column.

I tried:

Label.config(width=70, font=('Courier',15))

But keep getting errors:

Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 3331, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 8, in Label.config(width=70, font=('Courier',15)) TypeError: configure() missing 1 required positional argument: 'self'

What is the correct way to do this?

Full Code:

from tkinter import *
from tkinter import ttk

reasons_window = Tk()
ttk.Style.configure('TLabel',width=70,font=('Courier',15))
reasons_window.geometry("500x200")

# Create rows for the reasons to be entered.
label1 = ttk.Label(reasons_window, text="Qty")
label1.grid(row=0,column=0)

# for field in fields
e1 = Entry(reasons_window)
e1.grid(row=0,column=1)
def eval_click():
    if int(e1.get()) == 100:
        print('GO AHEAD')

eval_button = Button(reasons_window, text="Evaluate", command=eval_click)
eval_button.grid(row=3, column=0)


reasons_window.mainloop()
Sid
  • 3,749
  • 7
  • 29
  • 62
  • 1
    Read up on [Tutorial - 9.3.5. Class and Instance Variables](https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables), Does this answer your question? [is-it-possible-to-have-a-standard-style-for-a-widget](https://stackoverflow.com/questions/52210391) – stovfl Jun 19 '20 at 12:55
  • @stovfl yes it answers almost everything I could think of. Didn't know how to ask the right question I guess. – Sid Jun 19 '20 at 12:57

1 Answers1

3

You can do this with the ttk submodule. Adapted from the ttk docs:

from tkinter import ttk
import tkinter

root = tkinter.Tk()

ttk.Style().configure('TLabel', width=70, font=('Courier',15))

btn = ttk.Label(text="Sample")
btn.pack()

root.mainloop()
mousetail
  • 7,009
  • 4
  • 25
  • 45
  • Getting this error: TypeError: configure() missing 1 required positional argument: 'style' – Sid Jun 19 '20 at 10:35
  • Oops, should be fixed now – mousetail Jun 19 '20 at 10:38
  • Thanks. Your example works. But when I try to do this in my code it throws the same error. Updating the question with the full code, please have a look. TIA – Sid Jun 19 '20 at 11:24
  • 1
    You are missing the parenthesis after `Style`, you have to make an instance of the class before you can call methods – mousetail Jun 19 '20 at 11:28