'tkinter "unknown option "-padx" "

I am working on a project using tkinter and I wanted to change button styles to make it look better, but when I start import tkinter.tkk the code starts messing up.

I could not find any clear solution on the Internet and also when I start another code with both tkinter and tkinter.tkk imported it works fine.

Here is the working code:

from tkinter import *
from tkinter.ttk import *
 
 
root = Tk()
 

root.geometry('100x100')
 

style = Style()
 
 
style.configure('TButton', font =
               ('calibri', 10, 'bold', 'underline'),
                foreground = 'red')

btn1 = Button(root, text = 'Quit !',
                  style = 'TButton',
             command = root.destroy)
 
btn1.grid(row = 0, column = 3, padx = 100)
 
btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
 
root.mainloop()

And here is the code that has a problem:

from tkinter import *
from tkinter.ttk import *


window= Tk()
window.title('customers information')


e = Entry(window, width=50)

button1 = Button(window,text='1',padx=40,pady=40)
button2 = Button(window,text='2',padx=40,pady=40)
button3 = Button(window,text='3',padx=40,pady=40)

button4 = Button(window,text='4',padx=40,pady=40)
button5 = Button(window,text='5',padx=40,pady=40)
button6 = Button(window,text='6',padx=40,pady=40)

button7 = Button(window,text='7',padx=40,pady=40)
button8 = Button(window,text='8',padx=40,pady=40)
button9 = Button(window,text='9',padx=40,pady=40)

button0 = Button(window,text='0',padx=40,pady=40)

buttoneq = Button(window,text='=',padx=40,pady=40,fg='black',bg="silver")
buttonplus = Button(window,text='+',padx=40,pady=40,fg='black',bg='silver')
buttonminus = Button(window,text='-',padx=40,pady=40,fg='black',bg='silver')
buttonclear = Button(window,text='C',padx=40,pady=40,fg='black',bg='silver')


button1.grid(row=1,column=1)
button2.grid(row=1,column=2)
button3.grid(row=1,column=3)

button4.grid(row=2,column=1)
button5.grid(row=2,column=2)
button6.grid(row=2,column=3)

button7.grid(row=3,column=1)
button8.grid(row=3,column=2)
button9.grid(row=3,column=3)

button0.grid(row=4,column=1)

buttonplus.grid(row=4,column=2)
buttonminus.grid(row=4,column=3)
buttoneq.grid(row=5,column=2)
buttonclear.grid(row=5,column=1)



e.grid(row=0,column=0,columnspan=5,padx=10,pady=10)

window.mainloop()

And the exception :

    Traceback (most recent call last):
  File "c:\Users\lcc_zarkos\Desktop\UKIYO-dataframer\gui-tkinter.py", line 12, in <module>
    button1 = Button(window,text='1',padx=40,pady=40)
  File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\ttk.py", line 612, in __init__
    Widget.__init__(self, master, "ttk::button", kw)
  File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\ttk.py", line 557, in __init__
    tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2569, in __init__
    self.tk.call(
_tkinter.TclError: unknown option "-padx"

Please let me know if you have any solutions.



Solution 1:[1]

You should know, Tkinter Entry widget can have width property only AS if you want to position the entry widget then use padx and pady however if you want to resize the entry widget then use ipadx and ipady.

from tkinter import *

#Create an instance of tkinter window
root= Tk()
root.geometry("600x450")

e= Entry(root, width= 20)
e.pack(ipadx=30, ipady= 30)

root.mainloop()

Solution 2:[2]

You must pad the elements in the grid() method.

This:

button1 = Button(window, text='1', padx=40, pady=40)
...
button1.grid(row=1, column=1)

Must be done like this:

button1 = Button(window, text='1')
...
button1.grid(row=1, column=1, padx=40, pady=40)

Also you should avoid importing tkinter and tkinter.ttk at the same time using from ... import *: when you type Button(...), this calls the tkinter.ttk.Button class instead of the tkinter.Button class. And this class doesn't support the fg and bg arguments. So prefer removing the line

from tkinter.ttk import *

This will avoid this error:

Traceback (most recent call last):
  File "C:\Users\lcc_zarkos\Desktop\UKIYO-dataframer\gui-tkinter.py", line 25, in <module>
    buttoneq = Button(window,text='=',fg='black',bg="silver")
  File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\ttk.py", line 612, in __init__
    Widget.__init__(self, master, "ttk::button", kw)
  File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\ttk.py", line 557, in __init__
    tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2567, in __init__
    self.tk.call(
_tkinter.TclError: unknown option "-fg"

If you still want to import tkinter.ttk, import it, for example, like that:

import tkinter.ttk as ttk

Solution 3:[3]

for button you can't add padding to it using confige(padx = 10)

but you need to add it to the grid function. but for label you can .

button = tkinter.Button(text = "add", command = calculte)
button.grid(column = 3, row =  0, pady = (5,5))

result = tkinter.Label(text = "= ")
# you can add in gride function 
result.grid(column = 4, row = 0, padx = (10,0))
# and you can also add it here 
result.config(padx = 10)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 codewithdev
Solution 2
Solution 3 Sar ibra