1

I am trying to make GUI with Tkinter, but I have occured a problem. My GUI is going to have a lot of very similar buttons with a lot of options (font, width, height, command etc.) and I would rather like to write name of variable, which store repeating options than repeat all commands over and over again.

I don´t even know if this possible. I tried save options as string in variable and then pass it into variable, but it raises: AttributeError: 'str' object has no attribute 'tk'

This is example of my buttons:

Num3 = Tk.Button(main, text="3", width = 2, height = 2, font = "Arial 16", command=lambda: nex("3")) Num4 = Tk.Button(main, text="4", width = 2, height = 2, font = "Arial 16", command=lambda: nex("4"))

And I would like that it look like this:

Var = 'main, width = 2, height = 2, font = "Arial 16",' Num3 = Tk.Button(Var, text="3",command=lambda: nex("3")) Num4 = Tk.Button(Var, text="4",command=lambda: nex("4"))

But it raises that AttributeError: 'str' object has no attribute 'tk'

Thanks for answers, people.

John
  • 43
  • 1
  • 7
  • Don't use variable names that start with with Capital letters. See [PEP 8 - Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/). – martineau Apr 11 '19 at 17:25

1 Answers1

1

Save them inside a dictionary, like this

import tkinter as tk

main = tk.Tk()

options = {"text": "Hello!", "font": "Arial 16", "width": 2, "height": 2}

Num4 = tk.Button(main, **options)
Num4.pack()

main.mainloop()

See this question How to pass dictionary items as function arguments in python?.

Mat.C
  • 1,379
  • 2
  • 21
  • 43